Creating audio file from text file array
in
Programming Questions
•
3 years ago
I am trying to create a program that will read a list of values from a text file and make an audio stream out of it.
I generated a text file of sine wave values (1 cycle, 360 points), and now want to read them into the stream. I would ultimately also like to have the option of saving it to a .wav
EDIT: I have come up with the following code, but there are some errors. The float array has all the right values, but I think the main problem is spitting out the wavetable values gets me way fewer values, looking at the audio output of my speakers, one cycle of a sine wave became 4 cycles of a square wave. Can anyone see any obvious corrections that need to be made?
I generated a text file of sine wave values (1 cycle, 360 points), and now want to read them into the stream. I would ultimately also like to have the option of saving it to a .wav
EDIT: I have come up with the following code, but there are some errors. The float array has all the right values, but I think the main problem is spitting out the wavetable values gets me way fewer values, looking at the audio output of my speakers, one cycle of a sine wave became 4 cycles of a square wave. Can anyone see any obvious corrections that need to be made?
- import ddf.minim.*;
import ddf.minim.ugens.*;
Minim minim = new Minim( this );
AudioOutput out = minim.getLineOut( Minim.STEREO, 360 );
void setup() {
size(512, 200, P2D);
String[] strLines = loadStrings("in.txt"); //load wave values
for (int i = 0 ; i < strLines.length ; i++)
{
// use the split array with character to isolate each component
String[] linesArray = split(strLines[i], ",");
// cast string value to a float values
float[] linesArrayFloat = float(linesArray);
// create new wave tables from float array values
Wavetable table = new Wavetable(linesArrayFloat);
table.setWaveform(linesArrayFloat);
// create an oscillator with frequency 100 and 0.5 amp using the vales from the table
Oscil osc = new Oscil(100, 0.5, table);
// output to speakers
osc.patch( out );
//test values -> println(table);
}
}
1