Determine the duration of the input sound signal

edited November 2015 in Library Questions

I want to be able to be able to translate morse code as an input via a sound file, or a microphone into the appropriate dash or dot based on its duration using p5. Can someone point me in the right direction where to start to program this.

Thanks

Answers

  • Here is a rough example based on the AudioInput-example.
    There might be better solutions, but maybe it's a good starting point.

    import processing.sound.*;
    
    AudioIn input;
    Amplitude rms;
    
    // tresholf for valid input-signals
    float treshold =0.1;  
    // minimum duration of long signals in ms
    int durationLong = 500;
    
    float value;
    boolean soundIsOn = false;
    int startTime;
    
    void setup() {
      size(640, 360);
      background(255);
    
      //Create an Audio input and grab the 1st channel
      input = new AudioIn(this, 0);
    
      // start the Audio Input
      input.start();
    
      // create a new Amplitude analyzer
      rms = new Amplitude(this);
    
      // Patch the input to an volume analyzer
      rms.input(input);
    }      
    
    
    void draw() {
      // get current amplitude from input
      value =rms.analyze();
    
      // when amplitude is above threshold
      if (value>treshold) {
        soundOn();
      } 
    
      // when amplitude is below threshold
      else {
        soundOff();
      }
    }
    
    void soundOn(){
      // when signal just started
      if(!soundIsOn){
        // remember time
        startTime = millis();
      }
      // set sound to 'on'
      soundIsOn = true;
    }
    
    void soundOff(){
      // when sound just ended
      if(soundIsOn){
        // check if signal duration was long or short
        int time = millis()-startTime;
        if(time>=durationLong){
          println("long signal");
        }else{
          println("short signal");    
        }
      }
      // set sound to 'off'
      soundIsOn = false;
    }
    
Sign In or Register to comment.