// Michael Chladil // 2007 11 08 // // sonify_transactions_v1 // // takes my gas purchase data and sonifies it final int __DEBUG__ = 1; class PriceRecords { String Dates[]; float RawPrices[]; float ScaledPrices[]; PriceRecords(String Filename) { String tmpLines[] = loadStrings(Filename); String tmpLine[]; // temporary storage while splitting a line into its respective data fields if (__DEBUG__ == 1) println ("Read " + tmpLines.length + " lines from " + Filename); Dates = new String[tmpLines.length - 1]; RawPrices = new float[tmpLines.length - 1]; ScaledPrices = new float[tmpLines.length - 1]; /* load in raw records. These will be separated by spaces. Data fields are comma-separated. */ for (int i=0; i < tmpLines.length - 1; i++) { tmpLine = split(tmpLines[i], ","); if ((tmpLine.length - 1) > 0) // is there an empty line? { Dates[i] = tmpLine[0]; RawPrices[i] = float (tmpLine[1]); } else { // not necessary for this data set } } } float MinPrice () { if (__DEBUG__ == 1) println ("min price=" + min(RawPrices)); return (min(RawPrices)); } float MaxPrice () { if (__DEBUG__ == 1) println ("max price=" + max(RawPrices)); return (max(RawPrices)); } float PriceRange () { return (max(RawPrices) - min(RawPrices)); } void ComputeScaledPrices (float fMin, float fRange) { // assuming array has already been initialized for (int i=0; i < ScaledPrices.length - 1; i++) { ScaledPrices[i] = (RawPrices[i] - fMin) / fRange; if (__DEBUG__ == 1) println ("ScaledPrices[i]=" + ScaledPrices[i]); } } } // PriceRecords PrintWriter output; // a "generic" filestream object for Processing PriceRecords MyData; float PriceMin; float PriceMax; MyData = new PriceRecords("gas one year.txt"); PriceMin = MyData.MinPrice(); PriceMax = MyData.MaxPrice(); MyData.ComputeScaledPrices (PriceMin, PriceMax - PriceMin); output = createWriter("sonify.txt"); output.println("f 1 0 1024 10 1. 0.4 0.3 0.2 0.01"); // write out the CSound function table first for (int i=0; i < MyData.ScaledPrices.length - 1; i++) { if (MyData.ScaledPrices[i] > 0) output.println("i 1 " + (i * 0.05) + " 0.1 6.0 " + (MyData.ScaledPrices[i] * 8000)); // put a sonic marker every seven days... ideally this will be different // I'd like to divide up the week into weekdays and weekends output.println ("i 1 " + (i * 0.05) + " 0.01 12.0 5000"); if (i % 7 == 0 ) output.println("i 1 " + (i * 0.05) + " 0.05 10.0 6000"); } output.println("e"); // ends the cSound score file output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file exit(); // Stops the program