Creating Callbacks Inside a Class for Use within the Class; Soundsipher
in
Contributed Library Questions
•
2 years ago
I'm having trouble creating a class out of a Soundsipher example code that uses callbacks. When I try to setup callbacks the usual way, myScore.addCallbackListener(this); it says I can't create a callback. I also have a question about assigning instruments using instrument names. This question relates to the Soundsipher library, but, may be generally relevant to creating and using classes in general.
I've commented out the lines that would turn the script into a class so that the following script executes. The string list is not being used; that's my second question.
It's line 62 that causes the error, and I suspect lines 77, 81, and 91 would too if it executed that far as these lines also add a callbacklistener.
I've done a little exploring and found there's things like 'ActionListeners' and something about needing to create callback classes.
Soundsipher requires callbacks in order to use the score mode. (Lines 160-177.) The callbacklistenerclass I created when I had things uncommented to run as a class, lines 218 to 238, wasn't available from inside the MusicMaker class.
http://soundcipher.org/doc/constant-values.html
These don't look like integers. Is there a trick to using instrument names instead of instrument numbers that are stored in a list of instruments to be used?
I've commented out the lines that would turn the script into a class so that the following script executes. The string list is not being used; that's my second question.
It's line 62 that causes the error, and I suspect lines 77, 81, and 91 would too if it executed that far as these lines also add a callbacklistener.
I've done a little exploring and found there's things like 'ActionListeners' and something about needing to create callback classes.
Soundsipher requires callbacks in order to use the score mode. (Lines 160-177.) The callbacklistenerclass I created when I had things uncommented to run as a class, lines 218 to 238, wasn't available from inside the MusicMaker class.
- /**
- * UNTLS (BSD 2011)
- Music Class for Java Applets and Applications
- * Based on GeneratingBeats and Unlimited Art SoundCipher examples
- * by Andrew R. Brown.
- * A drum pattern generator that creates a 4 beat pattern,
- * plays it then generates another 4 beat pattern, and so on.
- * SoundCipher's callback facility is used to provide the loop
- * regeneration notification at the end of the pattern.
- * Simple drawing in time with the music is triggered by callbacks.
- */
- import arb.soundcipher.*;
- //MusicMaker myMusicMaker;
- //
- //void setup(){
- // size(200,200);
- //
- // myMusicMaker = new MusicMaker();
- //
- //}
- //
- //void draw(){
- //
- //}
- String[] SubInstruments = {"BIRD","BOTTLE_BLOW","CELESTA","CRYSTAL","ELECTRIC_GRAND","FRETS",
- "JAZZ_GUITAR","KALIMBA","KOTO","MARIMBA","MUSIC_BOX","ORCHESTRA_HIT",
- "PICKED_BASS","SEA","SHAMISEN","STAR_THEME","STEEL_GUITAR","STEELDRUMS",
- "TAIKO","TOMS","VIBRAPHONE","WOODBLOCKS","THUMB_PIANO"};
- //Double[] SubInstruments = {BIRD,BOTTLE_BLOW,CELESTA,CRYSTAL,ELECTRIC_GRAND,FRETS,
- // JAZZ_GUITAR,KALIMBA,KOTO,MARIMBA,MUSIC_BOX,ORCHESTRA_HIT,
- // PICKED_BASS,SEA,SHAMISEN,STAR_THEME,STEEL_GUITAR,STEELDRUMS,
- // TAIKO,TOMS,VIBRAPHONE,WOODBLOCKS};
- //public class MusicMaker {
- //SCScore score = new SCScore();
- SCScore myScore = new SCScore();
- SCScore Melody = new SCScore();
- SCScore Melody2 = new SCScore();
- float[] r = new float[4];
- //public int uTempo;
- int Instrument;
- int LastInstrument;
- int uTempo;
- int NotePitch;
- int SwapTime;
- int lastSwap;
- float[] pitchSet = {57, 60, 60, 60, 62, 64, 67, 67, 69, 72, 72, 72, 74, 76, 79};
- //float[] pitchSet = {57, 57, 57, 60, 62, 64, 67, 67, 69, 62, 72, 64, 74, 76, 79};
- //float[] pitchSet = {24, 24, 35, 32, 32, 32, 34, 31, 31, 30, 26, 26, 28, 28, 29};
- float setSize = pitchSet.length;
- float keyRoot = 0;
- float density = 0.8;
- void setup(){
- // public MusicMaker() {
- //noLoop();
- uTempo = 112;
- //score.tempo(uTempo);
- myScore.tempo(uTempo);
- //score.addCallbackListener(this);
- myScore.addCallbackListener(this);
- makeMusic();
- SwapTime = 32 * 1000;
- lastSwap = millis();
- Instrument = int(random(130));
- LastInstrument = int(random(130));
- }
- synchronized void makeMusic() {
- myScore.empty();
- for (float i=0; i<16; i++) {
- if (i%8 == 0 || i == 14) {
- myScore.addNote(i/4, 9, 0, 36, 100, 0.25, 0.8, 64); // original instrument 36
- myScore.addCallback(i/4, 1);
- } else if (random(10) < 1) myScore.addNote(i/4, 9, 0, 36, 70, 0.25, 0.8, 64);
- if (i%8 == 4) {
- myScore.addNote(i/4, 9, 0, 38, 100, 0.25, 0.8, 64);
- myScore.addCallback(i/4, 2);
- } else if (random(10) < 2) myScore.addNote(i/4, 9, 0, 38, 60, 0.25, 0.8, 64);
- if(random(10) < 8) {
- myScore.addNote(i/4, 9, 0, 42, random(40) + 70, 0.25, 0.8, 64);
- myScore.addNote(i/8, 9, 0, LastInstrument, pitchSet[(int)random(setSize)]+keyRoot, 0.25, 0.8, 64);
- } else {
- myScore.addNote(i/4, 9, 0, Instrument, random(40) + 70, 0.25, 0.8, 64); //original instrument = 38
- myScore.addNote(i/4, 9, 0, Instrument, 80, 0.25, 0.8, 64); //original instrument = 38
- }
- }
- myScore.addCallback(4, 0);
- myScore.play();
- Melody.empty();
- Melody.instrument(Melody.VIBRAPHONE);
- int Itemp = int(random(126));
- for (float i =0;i<32;i++) {
- NotePitch = int(random(setSize));
- if (random(1) < density) {
- //sc.playNote(pitchSet[(int)random(setSize)]+keyRoot, random(90)+30, random(20)/10 + 0.2);
- Melody.addNote(i/4,pitchSet[NotePitch]+keyRoot, random(90)+30, random(20)/10 + 0.2);
- fill(color(random(256), random (256), random(256)));
- rect(random(100), random(100), random(40), random(40));
- }
- if (i2 == 0) {
- keyRoot = (random(4)-2)*2;
- density = random(7) / 10 + 0.3;
- Melody.addNote(i/2,46+keyRoot, random(40) + 70, 0.8);
- }
- if (i == 0) {
- //Melody.instrument(Instrument);
- //float[] pitches = {pitchSet[(int)random(setSize)]+keyRoot-12, pitchSet[(int)random(setSize)]+keyRoot-12};
- //Melody.addNote(i/4,pitchSet[(int)random(setSize)]+keyRoot-12, random(50)+30, 4.0);
- //Melody.addNote(i/4, 9, Instrument, pitchSet[(int)random(setSize)]+keyRoot-12, 90,2, .75, 64);
- Melody.addNote(i/2,9,76,pitchSet[(int)random(setSize)]+keyRoot-12,120,1,2.8,65);
- //Melody.instrument(Melody.SLAP_BASS );
- rect(random(100), random(100), random(40), random(40));
- }
- }
- //Melody.instrument(49);
- //Melody.instrument(Instrument);
- Melody.play();
- Melody2.empty();
- Melody2.instrument(Melody2.STEELDRUMS );
- for (float i =0;i<32;i++) {
- if (random(1) < density) {
- //sc.playNote(pitchSet[(int)random(setSize)]+keyRoot, random(90)+30, random(20)/10 + 0.2);
- Melody2.addNote(i/4,pitchSet[NotePitch]+keyRoot, random(90)+10, random(20)/10 + 0.2);
- fill(color(random(256), random (256), random(256)));
- rect(random(100), random(100), random(40), random(40));
- }
- if (i2 == 0) {
- keyRoot = (random(4)-2)*2;
- density = random(7) / 10 + 0.3;
- Melody2.addNote(i/2,46+keyRoot, random(40) + 20, 0.8);
- }
- if (i == 0) {
- //Melody.instrument(Instrument);
- //float[] pitches = {pitchSet[(int)random(setSize)]+keyRoot-12, pitchSet[(int)random(setSize)]+keyRoot-12};
- //Melody.addNote(i/4,pitchSet[(int)random(setSize)]+keyRoot-12, random(50)+30, 4.0);
- //Melody.addNote(i/4, 9, Instrument, pitchSet[(int)random(setSize)]+keyRoot-12, 90,2, .75, 64);
- Melody2.addNote(i/2,9,76,pitchSet[NotePitch]+keyRoot-12,35,1,2.8,65);
- //Melody.instrument(Melody.SLAP_BASS );
- rect(random(100), random(100), random(40), random(40));
- }
- }
- //Melody.instrument(49);
- //Melody.instrument(Instrument);
- Melody2.play();
- }
- public void handleCallbacks(int callbackID) {
- switch (callbackID) {
- case 0:
- //score.stop();
- myScore.stop();
- makeMusic();
- break;
- case 1:
- float w = random(20);
- r = new float[] {50-w, 50-w, w*2, w*2};
- redraw();
- break;
- case 2:
- r = new float[] {20, 20, 60, 60};
- redraw();
- break;
- }
- }
- void draw() {
- //background(120);
- rect(r[0], r[1], r[2], r[3]);
- if (millis()-lastSwap >= SwapTime) {
- ChangeUp();
- lastSwap = millis();
- }
- }
- void stop() {
- //score.stop();
- myScore.stop();
- Melody.stop();
- Melody2.stop();
- }
- public void ChangeUp(){
- //score.stop(); //?
- myScore.stop();
- //Melody.stop();
- LastInstrument = Instrument;
- Instrument = int(random(126));
- println("Instrument: " + str(Instrument)); // good instruments: 65, 35,29,86,30,47,55, 69,58,54,83,92,81,62,75,123
- //score.tempo(uTempo);
- myScore.tempo(uTempo);
- Melody.tempo(uTempo);
- makeMusic();
- if (random(10) >= 5) {float[] pitchSet = {57, 57, 57, 60, 62, 64, 67, 67, 69, 62, 72, 64, 74, 76, 79};} else
- {float[] pitchSet = {57, 60, 60, 60, 62, 64, 67, 67, 69, 72, 72, 72, 74, 76, 79};}
- }
- void mousePressed(){
- ChangeUp();
- uTempo = mouseX*2;
- }
- //void mousePressed(){
- // //MusicMaker.ChangeUp();
- // uTempo = mouseX*2;
- //}
- //public class handleCallbacks implements ActionListener {
- //
- // handleCallbacks (int callbackID) {
- // switch (callbackID) {
- // case 0:
- // //score.stop();
- // myScore.stop();
- // makeMusic();
- // break;
- // case 1:
- // float w = random(20);
- // r = new float[] {50-w, 50-w, w*2, w*2};
- // redraw();
- // break;
- // case 2:
- // r = new float[] {20, 20, 60, 60};
- // redraw();
- // break;
- // }
- // }
- //}
http://soundcipher.org/doc/constant-values.html
These don't look like integers. Is there a trick to using instrument names instead of instrument numbers that are stored in a list of instruments to be used?
public static final float |
ACOUSTIC_BASS |
32.0f |
1