Minim, load 2nd file into sample

edited November 2014 in Library Questions

Hi, I've try to load 2nd source file into minim sample. Because I need to change the sound effect in my program at runtime. With first loading source file into sample, every is OK. But with loading 2nd one, it's failed and there is no sound. So the question is: What I missed?

Example code:

import ddf.minim.*;

Minim minim;
AudioSample sample;

void setup()
{
  size(512, 200, P3D);
  minim = new Minim(this);
  sample = minim.loadSample( "BD.mp3", 512);
  sample = minim.loadSample( "SD.wav", 512); //this doesn't work
}

void draw()
{
}

void keyPressed() 
{
  if ( key == 's' )
  {
    sample.trigger();
  }
}

Error message:

==== JavaSound Minim Error ==== ==== Couldn't open the line: line with format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian not supported.

==== JavaSound Minim Error ==== ==== Unable to return a SourceDataLine: unsupported format - PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian

Tagged:

Answers

  • Apparently, Minim doesn't like the file format of the second file. You can try and convert it to another format.

  • Dunno what the problem is, but reassigning a newly initialized variable w/ another object got no much logic!

  • Yeah, this too, but this is "Example code", made to avoid showing a full project, which is a good thing. Perhaps it was simplified a bit too much... :-)

  • edited November 2014

    The problem is, that I have a triggered "play" function somewhere in project and I need change source file to play at runtime. Both of these audio files presented here are from minim example "TriggerASample", so these files are OK for loading and playing. If PhiLho want see project, go here: https://github.com/laabicz/HomeLESS/tree/master/HomeLESS_Hit_Analyzer, but "Example code" is OK for this problem.

    GoToLoop: There is no logic, this is just example :).

  • @Laabicz, you can always keep them all in some data structure, an array for example, and then assign 1 of them to sample when it's time to change! O:-)

  • Hi, I tried your advice. But there is the same problem... :( There must be something else at this library... :-?

    import ddf.minim.*;
    
    Minim[] minim;
    AudioSample sample;
    
    void setup()
    {
      size(512, 200, P3D);
        minim = new Minim[2];
        minim[0] = new Minim(this);
        minim[1] = new Minim(this);
        sample = minim[0].loadSample( "BD.mp3", 512);
        sample = minim[1].loadSample( "SD.wav", 512); //doesn't work
    }
    
    void draw()
    {
    }
    
    void keyPressed() 
    {
      if ( key == 's' )
      {
        sample.trigger();
      }
    } 
    
  • edited December 2014

    Uh? You don't need two instances of Minim, but you need two instances of sample.

    import ddf.minim.*;
    
    AudioSample[] sample = new AudioSample[2];
    
    void setup()
    {
        size(512, 200, P3D);
        Minim minim = new Minim(this);
        sample[0] = minim.loadSample( "BD.mp3", 512);
        sample[1] = minim.loadSample( "SD.wav", 512);
    }
    
    void draw()
    {
    }
    
    void keyPressed()
    {
      if ( key == 'r' )
      {
        sample[0].trigger();
      }
      if ( key == 's' )
      {
        sample[1].trigger();
      }
    } 
    

    I just tested the above code (replacing (without saving!) the code in the TriggerASample sketch), and it works fine on my Windows 7 computer, with Processing 2.2.1.

  • edited December 2014

    1 Minim instance is all we need! And variables can only hold 1 single value at a time.
    We need an object to hold more than 1 value at a time. Simplest 1 is a regular array[].
    W/ an array[], the only extra value we need is the index we wanna access. Let's call it idx.
    I've adapted the code above to have a samples[] array rather than 2 separate AudioSample variables:

    P.S.: Oops! Didn't see @PhiLho's answer! :-\"

    // forum.processing.org/two/discussion/8357/
    // minim-load-2nd-file-into-sample
    
    import ddf.minim.Minim;
    import ddf.minim.AudioSample;
    
    static final String[] SONGS = {
      "BD.mp3", "SD.wav"
    };
    
    final AudioSample[] samples = new AudioSample[SONGS.length];
    
    int idx;
    
    void setup() {
      size(512, 200, JAVA2D);
    
      smooth(4);
      noLoop();
      frameRate(10);
    
      stroke(-1);
      strokeWeight(1.5);
      clear();
    
      Minim m = new Minim(this);
      for (String s : SONGS)  samples[idx++] = m.loadSample(s, 512);
    }
    
    void draw() {
      print(idx + 1, '\t');
      if (idx >= 0 & idx < samples.length)  samples[idx].trigger();
    }
    
    void keyPressed() {
      idx = keyCode - '1';
      redraw();
    }
    
Sign In or Register to comment.