I have been trying to determine how to create audio from pixels. There are many near-answers for questions, but very little useful code.
This is an example using the CreateSample method in Minim. It is derived from examples on the Minim site. It fails after a certain number of plays, probably due to the same problems discussed on this page: http://code.compartmental.net/tools/minim/manual-audiosample/ The solution was to make a FilePLayer player... but it doesn't work for samples.
- /// this is intended to mimic finding pixels on an image and sending them to an audio output for other uses
- import ddf.minim.*;
- import ddf.minim.signals.*;
- // we must import this package to create an AudioFormat object
- import javax.sound.sampled.*;
- Minim minim;
- AudioSample wave;
- // we'll use a sine wave signal to generate a buffer of floats
- // that will be used to create an AudioSample.
- SineWave sine;
- // when we create a sample we need to provide an AudioFormat so
- // the sound will be played back correctly.
- AudioFormat format = new AudioFormat( 44100, // sample rate
- 16, // sample size in bits
- 1, // channels
- true, // signed
- true // bigEndian
- );
- // we'll make a MONO sample, but there is also a version
- // of createSample that you can pass two float arrays to:
- // which will be used for the left and right channels
- // of a stereo sample.
- float[] samples = new float[1024*8];
- void setup()
- {
- size(512, 200, P3D);
- // always start Minim before you do anything with it
- minim = new Minim(this);
- // make a sine wave!
- sine = new SineWave( 220, // frequency in Hz
- 0.5, // amplitude
- 44100 // sample rate
- );
- /// array should mimic a line of pixels being read by whatever algorithm I use.
- for (int i=0;i<(1024*8);i++){
- samples[i]=random(-200,200);
- }
- // finally, create the AudioSample
- wave = minim.createSample( samples, // the samples
- format, // the format
- 1024 // the output buffer size
- );
- frameRate(10);
- minim.debugOn();
- }
- void draw()
- {
- background(0);
- /// array should mimic a line of pixels being read by whatever algorithm I use.
- for (int i=0;i<(1024*8);i++){
- samples[i]=random(-2,2);
- }
- // create the AudioSample
- wave = minim.createSample( samples, // the samples
- format, // the format
- 1024 // the output buffer size
- );
- wave.trigger();
- stroke(255);
- // use the mix buffer to draw the waveforms.
- for (int i = 0; i < wave.bufferSize() - 1; i++)
- {
- line(i, 100 - wave.left.get(i)*50, i+1, 100 - wave.left.get(i+1)*50);
- }
- }
- void stop()
- {
- // always close Minim audio classes when you are done with them
- wave.close();
- minim.stop();
- super.stop();
- }
I know there may be better ways to do this, but the idea of creating an audio buffer from an array that is constantly changing as it is read from pixels would be incredibly useful (I've spent weeks trying to find a solution for doing this with pure data and chuck, but this solution would be incredibly useful) This particular version has a tendency to go silent, and I think it is a buffer issue since there is no real way of clearing an audio sample.
Could someone help me with this, please?
I've checked the following ideas but with no luck (don't understand the code, or solutions never posted)
http://code.google.com/p/processing/source/browse/trunk/processing/java/libraries/minim/examples/frequencyModulation/frequencyModulation.pde?r=10251 (the fm.offset, fm.amplitude and fm.frequency don't seem to work)
https://github.com/ddf/Minim/blob/master/examples/Synthesis/frequencyModulation/frequencyModulation.pde (same problems, different name endings)
http://processing.org/discourse/beta/num_1200904097.html ... It might work, but it isn't commented very well, and I have no idea how the class MuzikMaker works... (it's also from 2008, so I don't know if it was ever resolved, or if other, better solutions exist. )
Thank you, if you can help me figure this out.
PS: I tried to incorporate an 'isPlaying' check, but those don't exist for AudioSamples...