Multiple audio samples with minim
in
Core Library Questions
•
5 months ago
Hello all!
I'm currently writing a program that's basically a grid sequencer, and I had an audio question. Currently, my program can detect "hits" by displaying fill colors, but I'd like a variable audio sample (one of 16) to play depending on the position of the tile. I have my program split up into three different classes: Timer, Column, and Tile. It works by passing timing and placement information into an array of the Columns, which is made up of an array of the Tiles. I based my audio integration off of the following doorbell tutorial:
http://www.learningprocessing.com/examples/chapter-20/example-20-3/
I was able to get it so a single sound played any time a tile was clicked, but whenever I tried to load a second sound I got an out of memory error. I should have plenty of memory for two data sounds, so I'm guessing I made a mistake somewhere along the line, and I'd appreciate someone taking a look at it. Thanks!
- //*********** Visical Revision 10
- Timer timer;
- import ddf.minim.*;
- Column [] columns;
- Minim minim;
- int interval = 100;
- int measureH = 16;
- int measureL = 60;
- int timerSpeed = interval*measureL;
- int dL = measureL*25;
- float mxMain, myMain;
- void setup() {
- size(dL, 400);
- minim = new Minim(this);
- timer = new Timer(timerSpeed);
- timer.start();
- columns = new Column[measureL];
- for (int i=0; i<columns.length; i++) {
- columns[i] = new Column(i, measureH, interval, "dingdong.wav", "BD.wav");
- }
- }
- void draw() {
- int m = timer.passedTime;
- if (timer.isFinished()) {
- timer.start();
- }
- //println("timer.passedTime " +timer.passedTime);
- mxMain = mouseX;
- myMain = mouseY;
- for (int i=0; i<columns.length; i++) {
- columns[i].display(mxMain, myMain, m);
- }
- if(mousePressed){
- ellipseMode(CENTER);
- ellipse(mouseX, mouseY, 10, 10);
- }
- ////////////////////////////****************Drawing
- }
- //////////////////// Column Class
- class Column {// class start
- Tile [] tiles;
- int spacerX = 25;
- int spacerY = 25;
- int val, high, play, interval;
- float count;
- Column(int tempVal, int tempHigh, int tempInt, String filename, String filename2) { //Start constructor>>>>>>>>>
- val = tempVal;
- high = tempHigh;
- interval = tempInt;
- play = 0;
- tiles = new Tile[high];
- for (int i=0; i < tiles.length; i++) {
- tiles[i] = new Tile(spacerX*val, spacerY*i, spacerY, filename, filename2);
- }
- } // End constructor>>>>>>>>>>>>>>>>>>>
- void display(float dmx, float dmy, float cT) {/////////////Display
- count = cT;
- if (count >= val*interval && count < (val+1)*interval) {
- play = 1;
- }
- else {
- play = 0;
- }
- for (int i=0; i<tiles.length; i++) {
- tiles[i].display(dmx, dmy, play);
- }
- }////////////////End Display
- }// class end
- ///////////// Tile Class
- class Tile {//Class Start
- int x, y, spacer;
- boolean pressed, play;
- int w, h;
- color c = color(170, 170, 170);
- AudioSnippet dingdong1;
- AudioSnippet dingdong2;
- Tile(int tempX, int tempY, int tempSpace, String filename, String filename2) {
- w = tempSpace;
- h = tempSpace;
- y = tempY;
- x = tempX;
- dingdong1 = minim.loadSnippet(filename);
- dingdong2 = minim.loadSnippet(filename2);
- } // End constructor
- void ring1() {
- if (!dingdong1.isPlaying()) {
- // The ring() function plays the sound, as long as it is not already playing.
- // rewind() ensures the sound starts from the beginning.
- dingdong1.rewind();
- dingdong1.play();
- }
- }
- void ring2() {
- if (!dingdong2.isPlaying()) {
- // The ring() function plays the sound, as long as it is not already playing.
- // rewind() ensures the sound starts from the beginning.
- dingdong2.rewind();
- dingdong2.play();
- }
- }
- void display(float mx, float my, float p) {
- fill(c);
- rect(x, y, w, h);
- if (p >= 1) {
- play = true;
- }
- else {
- play = false;
- }
- if (mx >= x && mx <= x+w &&
- my >= y && my <= y+h &&
- mousePressed) {
- pressed=true;
- }
- if (pressed &&
- play) {
- c = color(170, 170, 0);
- if(x > 200){
- ring1();
- } else if (x < 200){
- ring2();
- }
- }
- else if (play) {
- c = color(0, 0, 170);
- }
- else if (pressed) {
- c = color(0, 170, 170);
- }
- else {
- c = color(170, 170, 170);
- }
- } //End Display
- void close() {
- // The doorbell has a close() function to close the AudioPlayer object.
- dingdong1.close();
- dingdong2.close();
- }
- } // Class End
- ///////////////////// Timer Class
- class Timer {
- int savedTime; // When Timer started
- int totalTime; // How long Timer should last
- int passedTime;
- Timer(int tempTotalTime) {
- totalTime = tempTotalTime;
- }
- // Starting the timer
- void start() {
- savedTime = millis();
- //println("timer.savedTime "+ savedTime);
- }
- boolean isFinished() {
- passedTime = millis()- savedTime;
- if (passedTime > totalTime) {
- return true;
- } else {
- return false;
- }
- }
- }
1