A For Loop Question: How to play, record, save in sequence

edited February 2018 in Questions about Code

Can anyone help -I am new to processing and I am trying to create an experiment protocol which includes a part in the draw function that when called will loop through a sequence of play-> record ->save etc. until a set number of recorded files are saved.

This is part of a longer code so I have simplified it here to just the minim play/record issue, but it is why I am trying to keep the draw function clear.

Current Problems: 1. I'm at a complete loss how to control the play->record->save loop for a set number of times before returning to the Draw function. At the moment it moves through twice and then stops?

I hope someone can help -I've been trying for days now :/

<

pre lang="javascript">

import ddf.minim.*; import ddf.minim.AudioPlayer;

Minim minim; Minim record; AudioInput In;

static final int SONG = 5; final AudioPlayer[] audio = new AudioPlayer [SONG];

static final int RECORDED = 5; final AudioRecorder[] recorder = new AudioRecorder [RECORDED];

boolean pressSave; PFont f;

int audioidx = 0; int audiocount = 0; int recorderidx = 0; int recordercount = 0;

int saved; static final int SAVED = 5;

void setup() { size (512, 200, P3D); f= createFont ("Georgia", 16);

minim = new Minim(this); audio [0] = minim.loadFile ("Audio_01.mp3"); audio [1] = minim.loadFile ("Audio_02.mp3"); audio [2] = minim.loadFile ("Audio_03.mp3"); audio [3] = minim.loadFile ("Audio_04.mp3"); audio [4] = minim.loadFile ("Audio_05.mp3"); printArray (audio);

record = new Minim(this); In = record.getLineIn(); recorder [0] = record.createRecorder(In, "myrecording1.wav"); recorder [1] = record.createRecorder(In, "myrecording2.wav"); recorder [2] = record.createRecorder(In, "myrecording3.wav"); recorder [3] = record.createRecorder(In, "myrecording4.wav"); recorder [4] = record.createRecorder(In, "myrecording5.wav"); printArray (recorder); }

void keyPressed(){ if ( keyCode == TAB ){ recorder[recorderidx].save(); recordercount = recordercount+1; recorderidx =recorderidx+1; audiocount = audiocount+1; audioidx = audioidx+1; //recorderidx = recorderidx+1; pressSave = true; println ("recorder count", recordercount,"recorderidx", recorderidx); println ("audio count", audiocount,"audioidx", audioidx); } }

void draw() { background (255); textFont (f, 16); fill (0); playrecord(); }

void playrecord(){

for (int i=0; i<audio.length;i++) { for (int r=0; r<recorder.length;r++) {

  if (audioidx==i && !audio[audioidx].isPlaying()){
  audio[audioidx].play();
  audiocount = audiocount+1;
  println ("variable i", i);

  }     
  else if (audiocount==i+1){
  recorder[recorderidx].beginRecord();
  println ("variable r", r);
  //println ("recording", recorderidx);
    if(pressSave){
    println("Done saving.");
    pressSave = true;
     } else { 
     text ( "Press TAB to save", 10, 100);
     pressSave = false;
     }
     if (!pressSave){
     return;
     }           
     }
  else if (audiocount == i+1 && recordercount == r+1){
  audio[audioidx = (audioidx + 1) % SONG].play();
  recorderidx = recorderidx+1 ;
  println ("variable r", r);
  }
  else{
    return;
}
}

} }

Answers

  • edited February 2018

    I thought I would share a progress report, in case anyone is trying to solve a similar issue. I have set up a nested for loop for the player and recorder. this seems to work except for a few bugs.

    1. I get an error message when I try to save the second audio recording -and the KeyPress window does not refresh.
  • <

    pre lang="javascript">

    import ddf.minim.*;
    import ddf.minim.AudioPlayer;
    
    Minim minim;
    Minim record;
    AudioInput In;
    
    static final int SONG = 2;
    final AudioPlayer[] audio = new AudioPlayer [SONG];
    
    static final int RECORDED = 2;
    final AudioRecorder[] recorder = new AudioRecorder [RECORDED];
    
    boolean pressSave =false;
    PFont f;
    
    int audioidx = 0;
    int audiocount = 0;
    int recorderidx = 0;
    int recordercount = 0;
    int saved;
    static final int SAVED = 2;
    
    
    void setup() {
      size (512, 200, P3D);
      f= createFont ("Georgia", 16);
    
      minim = new Minim(this);
      audio [0] = minim.loadFile ("Audio_01.mp3");
      audio [1] = minim.loadFile ("Audio_02.mp3");
      printArray (audio);
    
      record = new Minim(this);
      In = record.getLineIn();
      recorder [0] = record.createRecorder(In, "myrecording0.wav");
      recorder [1] = record.createRecorder(In, "myrecording1.wav");
      printArray (recorder);
    }
    
    void keyPressed(){
       if ( keyCode == TAB ){
            recorder[recorderidx].save();
            recordercount = recordercount+1;
            audioidx = audioidx+1;
            recorderidx = recorderidx+1;
            pressSave = true;
       }
    }
    
    void draw() {
      background (255);
      textFont (f, 16);
      fill (0);
      playrecord();
    }
    
    void playrecord(){
      for (int i=0; i<audio.length; i++) { 
        for (int r=0; r<recorder.length; r++) {
    
          if (audioidx==i && !audio[audioidx].isPlaying()){
          audio[audioidx].play();
          audiocount = i+1;
          println ("audio count",audiocount,"variable i", i, "audio idx", audioidx);
          }     
          else if (audiocount==i+1){
          recorder[recorderidx].beginRecord();
          println ("recording", recorderidx);
            if(pressSave){
            println("Done saving.");
             } else { 
             text ( "Press TAB to save", 10, 100);
             }
             if (!pressSave){
             return;
             }
            println ("recorder count", recordercount,"variable r", r, "recorderidx", recorderidx);      
             }
          else if (audiocount == i+1 && recordercount == r+1){
          audio[audioidx = (audioidx + 1) % SONG].play();
          recorderidx = recorderidx+1 ;
          }
          else{
            return;
        }
        }
      }
    }
    

  • the code you posted is much better formatted now! Congrats!

    puuh

    if I were you I wouldn't choose a nested for loop to tackle this

    especially because processing updates the screen only at the end of draw() and not troughout

    I am not sure what you are getting at / trying to do

    but if I were you I'd use a state system and in draw() a

    if(state==0) {

    ...

    } else if(state==1){

    ....

    } else if(state==2){

    ....

    }

    now, when play a sound you are in state 0; when it is finished, set state to 1. here you can display a message with text. set state to 2.

    then in state 2 record something

    thus you don't need for loop and have precise separation of situations

Sign In or Register to comment.