How do I close and open a new mp3 stream in ESS?

edited August 2014 in Library Questions

How do I close and open a new mp3 stream in ESS?

The ESS lib is at: http://www.tree-axis.com

The following code freezes when I click the mouse and attempt to close or destroy an audio stream:

import krister.Ess.*;

AudioStream myStream = null;
AudioFile myFile = null;

boolean playing;
String audio_file_url = "http://vprclassical.streamguys.net/vprclassical128.mp3";

void setup() {
  playing = false;
  size(256,200);

  // start up Ess
  Ess.start(this);

  // start Stream
  startStream(audio_file_url);

  frameRate(30);
}

void draw() {
  background(0,0,255);

  // draw waveform
  int interp=(int)max(0,(((millis()-myStream.bufferStartTime)/(float)myStream.duration)*myStream.size));

  for (int i=0;i<256;i++) {
    int top=50;

    if (i+interp<myStream.buffer2.length) top-=(int)(myStream.buffer2[i+interp]*50.0);

    int j=0;
    for (int k=top;k<height;k++) {
      set(i,k,color(j,j,255));
      j=min(j+2,255);
    }
  }
}

void mousePressed()
{
  startStream(audio_file_url);
}

void audioStreamWrite(AudioStream theStream) {
  if (playing)
  {
  // read the next chunk
  int samplesRead=myFile.read(myStream);
  if (samplesRead==0) {
    // start over
    myFile.close();
    myFile.open(audio_file_url,0,Ess.READ);  
  }
  }
}

public void startStream(String audio_stream_url)
{
    playing = false;
    println("a");
    if (myStream != null)
    {
      println("b");
      myStream.destroy();
    }
    println("c");
    if (myFile != null)
    {
      myFile.close();
    }
    myFile = new AudioFile(audio_stream_url,0,Ess.READ);
    myStream = new AudioStream();
    myStream.sampleRate(myFile.sampleRate);
    int samplesRead = myFile.read(myStream);
    myStream.start();
    playing = true;
}

// we are done, clean up Ess

public void stop() {
  Ess.stop();
  super.stop();
}

You can see that "b" is the last output printed. "c" does not get printed after myStream.destroy() is called in startStream().

How can I fix this code so it works?

Tagged:
Sign In or Register to comment.