I’m writing code for an arcade game which involves taking in sensor input and playing sound during certain stages of the game. I’m using the Minim library to handle all of the sound. I’ve run into an issue when trying to use more than 6 audio clips.
I started by creating FilePlayer instances for each of my sound files. There are 8 in total. It seems all is fine when I call 6 instances of loadFileSream(), but on the 7th, I get this error:
=== =JavaSound Minim Error ====
==== Unable to return a SourceDataLine: unsupported format - PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Things I’ve tried:
-Tested code on Windows machine. Code works fine with all 8 sound files.
- Googled error and found solutions for Ubuntu, not Raspbian
- I’ve tripled checked all the sound files to make sure they are in the correct format. I imported all sound clips into Audacity via raw data and exported making sure all settings were what Minim is expecting.
- For the 7th instance of loadFileStream(), I tried using the same sound file as the 1st. Still get error.
- I tried putting all sound files into an FilePlayer array. Get error when I try to fill the 7th element of this array.
- AudioPlayer gets an error after the 3rd call of loadFile().
All of this makes me think it has to do with how the Raspberry Pi handles sound files. I’ve read about moving of files around inside the JavaSound folders, but again, that was for Ubuntu. Raspbian does not seem to have the same files/folders.
What I did as a work around is to call loadFileStream(), setup the patch out, play the file then close the file only where I need the sound to play. It works but I’m curious to know why I need to do this on the RPi. Below are the sketches for the first try and the working code.
Code which gives error:
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Minim minim = new Minim(this);
AudioOutput out;
FilePlayer file1;
FilePlayer file2;
FilePlayer file3;
FilePlayer file4;
FilePlayer file5;
FilePlayer file6;
FilePlayer file7;
FilePlayer file8;
//For the wav files, I used the groove.mp3 from the AudioPlayer example
//I converted it to wav using Audacity
String file1String = "file1.wav";
String file2String = "file2.wav";
String file3String = "file3.wav";
String file4String = "file4.wav";
String file5String = "file5.wav";
String file6String = "file6.wav";
String file7String = "file7.wav";
String file8String = "file8.wav";
void setup() {
size(400, 200);
file1 = new FilePlayer(minim.loadFileStream(file1String));
file2 = new FilePlayer(minim.loadFileStream(file2String));
file3 = new FilePlayer(minim.loadFileStream(file3String));
file4 = new FilePlayer(minim.loadFileStream(file4String));
file5 = new FilePlayer(minim.loadFileStream(file5String));
file6 = new FilePlayer(minim.loadFileStream(file6String));
file7 = new FilePlayer(minim.loadFileStream(file7String));
file8 = new FilePlayer(minim.loadFileStream(file8String));
out = minim.getLineOut();
file1.patch(out);
file2.patch(out);
file3.patch(out);
file4.patch(out);
file5.patch(out);
file6.patch(out);
file7.patch(out);
file8.patch(out);
}
void draw() {
//Nothing here because the sketch wouldn't get past setup().
}
Working code:
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Minim minim = new Minim(this);
AudioOutput out;
FilePlayer file1;
FilePlayer file2;
FilePlayer file3;
FilePlayer file4;
FilePlayer file5;
FilePlayer file6;
FilePlayer file7;
FilePlayer file8;
String file1String = "file1.wav";
String file2String = "file2.wav";
String file3String = "file3.wav";
String file4String = "file4.wav";
String file5String = "file5.wav";
String file6String = "file6.wav";
String file7String = "file7.wav";
String file8String = "file8.wav";
void setup() {
size(400, 200);
out = minim.getLineOut();
}
int gameState = 0;
int currentTime = 0;
int previousTime = 0;
int interval = 10000;
void draw() {
background(255);
currentTime = millis();
if (currentTime - previousTime > interval) {
previousTime = currentTime;
gameState+=1;
if (gameState == 1) {
println("STATE 1");
file1 = new FilePlayer(minim.loadFileStream(file1String));
file1.patch(out);
file1.play();
} else if ( gameState == 2) {
println("STATE 2");
file2 = new FilePlayer(minim.loadFileStream(file2String));
file2.patch(out);
file1.pause();
file1.close();
file2.play();
} else if ( gameState == 3) {
println("STATE 3");
file3 = new FilePlayer(minim.loadFileStream(file3String));
file3.patch(out);
file2.pause();
file2.close();
file3.play();
} else if ( gameState == 4) {
println("STATE 4");
file4 = new FilePlayer(minim.loadFileStream(file4String));
file4.patch(out);
file3.pause();
file3.close();
file4.play();
} else if ( gameState ==5) {
println("STATE 5");
file5 = new FilePlayer(minim.loadFileStream(file5String));
file5.patch(out);
file4.pause();
file4.close();
file5.play();
} else if ( gameState == 6) {
println("STATE 6");
file6 = new FilePlayer(minim.loadFileStream(file6String));
file6.patch(out);
file5.pause();
file5.close();
file6.play();
} else if ( gameState ==7) {
println("STATE 7");
file7 = new FilePlayer(minim.loadFileStream(file7String));
file7.patch(out);
file6.pause();
file6.close();
file7.play();
} else if ( gameState == 8) {
println("STATE 8");
file8 = new FilePlayer(minim.loadFileStream(file8String));
file8.patch(out);
file7.pause();
file7.close();
file8.play();
} else if ( gameState >= 9) {
println("RESET STATE");
gameState = 0;
file8.pause();
file8.close();
}
}
}
Thanks for reading. I'm going to post this on the Minim Gitub as well.