Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

MInim AudioSample to AudioPlayer to allow looping

$
0
0
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.
  1. /// this is intended to mimic finding pixels on an image and sending them to an audio output for other uses

  2. import ddf.minim.*;
  3. import ddf.minim.signals.*;
  4. // we must import this package to create an AudioFormat object
  5. import javax.sound.sampled.*;

  6. Minim minim;
  7. AudioSample wave;
  8. // we'll use a sine wave signal to generate a buffer of floats
  9. // that will be used to create an AudioSample.
  10. SineWave sine;

  11.   // when we create a sample we need to provide an AudioFormat so 
  12.   // the sound will be played back correctly.
  13. AudioFormat format = new AudioFormat( 44100, // sample rate
  14.                                         16,    // sample size in bits
  15.                                         1,     // channels
  16.                                         true,  // signed
  17.                                         true   // bigEndian
  18.                                       );
  19.   
  20.   // we'll make a MONO sample, but there is also a version
  21.   // of createSample that you can pass two float arrays to:
  22.   // which will be used for the left and right channels
  23.   // of a stereo sample.
  24.   float[] samples = new float[1024*8];
  25.   

  26. void setup()
  27. {
  28.   size(512, 200, P3D);
  29.   // always start Minim before you do anything with it
  30.   minim = new Minim(this);
  31.   
  32.   // make a sine wave!
  33.   sine = new SineWave( 220,  // frequency in Hz
  34.                        0.5,  // amplitude
  35.                        44100 // sample rate
  36.                      );
  37.   

  38. /// array should mimic a line of pixels being read by whatever algorithm I use.
  39. for (int i=0;i<(1024*8);i++){
  40.   samples[i]=random(-200,200);
  41. }
  42.                               
  43.   // finally, create the AudioSample
  44.   wave = minim.createSample( samples, // the samples
  45.                              format,  // the format
  46.                              1024     // the output buffer size
  47.                             );
  48.   frameRate(10);
  49.   minim.debugOn();
  50.   
  51. }

  52. void draw()
  53. {
  54.   background(0);

  55. /// array should mimic a line of pixels being read by whatever algorithm I use.
  56.   for (int i=0;i<(1024*8);i++){
  57.     samples[i]=random(-2,2);
  58.   }
  59.     // create the AudioSample
  60.   wave = minim.createSample( samples, // the samples
  61.                              format,  // the format
  62.                              1024     // the output buffer size
  63.                             );
  64.   wave.trigger();
  65.   

  66.   stroke(255);
  67.   // use the mix buffer to draw the waveforms.
  68.   for (int i = 0; i < wave.bufferSize() - 1; i++)
  69.   {
  70.     line(i, 100 - wave.left.get(i)*50, i+1, 100 - wave.left.get(i+1)*50);
  71.   }
  72. }


  73. void stop()
  74. {
  75.   // always close Minim audio classes when you are done with them
  76.   wave.close();
  77.   minim.stop();
  78.   super.stop();
  79. }

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://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...


Viewing all articles
Browse latest Browse all 1768

Trending Articles