Record audio from Arduino with Minim.

edited December 2016 in Arduino

Hello. I am new to Processing and somewhat new to programming.

I want to use Processing to record sound from and Arduino with an Adafruit Electret Microphone Amplifier and have problem with the code. I haven't been able to find any tutorial so I looked in to the example Serial > Simple read and Minim > Basic > RecordAudioInput and tried to put them together.

What I am trying to do is to read the value from the serial and use it as the value for sound (was that clear?). Here is the code

I am using Processing 3.2.3 and latest Minim lib.

Am I on the right track? Thanks in advance. Daniel

import ddf.minim.*;
import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

Minim minim;
AudioInput in;  // <---------This is the current input from the computer I need it from Serial (probably 'val')
AudioRecorder recorder;

void setup()
{
  size(512, 200, P3D);

  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);

  minim = new Minim(this);

  in = minim.getLineIn();
  // create a recorder that will record from the input to the filename specified
  // the file will be located in the sketch's root folder.
  recorder = minim.createRecorder(in, "myrecording.wav");

  textFont(createFont("Arial", 12));
}

void draw()
{
  background(0); 
  stroke(255);
    if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  for(int i = 0; i < in.bufferSize() - 1; i++)
  {
    line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
    line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
  }

  if ( recorder.isRecording() )
  {
    text("Currently recording...", 5, 15);
  }
  else
  {
    text("Not recording.", 5, 15);
  }
}

void keyReleased()
{
  if ( key == 'r' ) 
  {
    // to indicate that you want to start or stop capturing audio data, you must call
    // beginRecord() and endRecord() on the AudioRecorder object. You can start and stop
    // as many times as you like, the audio data will be appended to the end of the buffer 
    // (in the case of buffered recording) or to the end of the file (in the case of streamed recording). 
    if ( recorder.isRecording() ) 
    {
      recorder.endRecord();
    }
    else 
    {
      recorder.beginRecord();
    }
  }
  if ( key == 's' )
  {
    // we've filled the file out buffer, 
    // now write it to the file we specified in createRecorder
    // in the case of buffered recording, if the buffer is large, 
    // this will appear to freeze the sketch for sometime
    // in the case of streamed recording, 
    // it will not freeze as the data is already in the file and all that is being done
    // is closing the file.
    // the method returns the recorded audio as an AudioRecording, 
    // see the example  AudioRecorder >> RecordAndPlayback for more about that
    recorder.save();
    println("Done saving.");
  }
}

Answers

  • edited December 2016

    The question is rather specific, so we'll need more information.
    Give us your Arduino code.
    Also note that a much higher baud rate (set on line 16 in your Processing code, and also somewhere in your Arduino code) will be required to sample sound properly.

    As to the question, you may need to write your own subclass of AudioInput overriding some key method in AudioInput.

    EDIT: You just need to write a class (or make a new type) that implements the Recordable interface.

    UPDATE: The job is easier said than done.

Sign In or Register to comment.