Issue Saving Multiple Files

edited October 2015 in Library Questions

I am working on a program that uses minim and Opencv. I am having issues with Minim when I try saving multiple audio files. Although it's currently creating multiple files, they are all empty and I can't figure out why. The function of the program is to know when someone is talking, record, and then save the audio file when the volume drops (they stop talking). Below is the code, if you can figure out what the issue is, or have a suggestion please let me know.

import gab.opencv.*;
import ddf.minim.*;
import processing.video.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.text.DateFormat.*;



Capture video;
OpenCV opencv;
Minim minim;
AudioInput in;
AudioRecorder recorder;
AudioPlayer player;
int countname; //change the name
int name = 000000; //set the number in key's' function
boolean analyzing = false; 
boolean recording = false;
boolean beginning = false;
float threshold = 5f;
Timer timer2;
Timer timer3;
ArrayList<Float> volumes;
float vol = 0;

void setup() 
{
  size(640, 480, OPENGL); //set window size
  video = new Capture(this, 640/2, 480/2); //video input
  video.start(); //start taking in video 
  minim = new Minim(this);

  // get a stereo line-in: sample buffer length of 2048
  // default sample rate is 44100, default bit depth is 16
  in = minim.getLineIn(Minim.STEREO, 2048);

 newFile();//go to change file name
}

void draw() 
{
  scale(2);
  image(video, 0, 0 );

// determines the resting volume of the space so that the program can start recording when someone talks close to the mic 
if (recording == false) //if someone is not currently talking into the mic
{
  if (!analyzing) //and we have not already started analysing
  {
    timer2 = new Timer(5000); //create a 5 second timer
    timer2.start();
    analyzing = true;
    volumes = new ArrayList<Float>(); 
  }
  if (timer2 != null) //timer starts
  {
    if (!timer2.isFinished()) //during the 2 seconds
    {
      float volume = in.mix.level() * 1000; //record volume
      volumes.add(volume); //inside the array
    } 
    else //after 2 seconds
    {
        float avg = 0.0f;
        float max = 0.0f;
        for (int i = 0; i < volumes.size(); i++)  //find the average
        {
            avg += volumes.get(i);
            if (volumes.get(i) > max) max = volumes.get(i);
        }
        avg /= volumes.size();
        threshold = (float) Math.ceil(max);
        println(getTime() + " Volume threshold automatically set to " + threshold);
        analyzing = false;
        beginning = true; //make sure it sets an initial threshold before the recording part starts
    } 
  }
}

vol = in.mix.level() * 1000; //calculate the current volume

if (beginning == true) //if it's found the initial threshold
{
  if (vol > threshold) //if volume is above threshold
  {
        newFile();  //make a new file
        recorder.beginRecord(); //put shit in the file
        recording = true; //say you've recorded someting
  }
  if (vol < threshold) //if volume falls below threshold (person stops talking)
  {
    if(recording == true) //and we've already started recording
    {
        name++; //change the file name, everytime +1
        recorder.endRecord(); //then end the recording and save
        recorder.save();
        println("Done saving.");
        println(name);//check the name
        recording = false;
    }
   }
  } 
}

// change the file name
void newFile()
{      
 countname =( name + 1);
 recorder = minim.createRecorder(in, "file/Voice" + countname + ".wav", false);
}

void captureEvent(Capture c) 
{
  c.read();
}


void stop()
{
 in.close();
 minim.stop();
 super.stop();
} 

private String getTime() 
{
  DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  Date date = new Date();
  return dateFormat.format(date);
}

public class Timer 
{
    int savedTime; // When Timer started
    int totalTime; // How long Timer should last
    public Timer(int tempTotalTime) 
    {
        totalTime = tempTotalTime;
    }
    // Starting the timer
    public void start() {
        // When the timer starts it stores the current time in milliseconds.
        savedTime = (int) System.currentTimeMillis();
    }
    // The function isFinished() returns true if 5,000 ms have passed.
    // The work of the timer is farmed out to this method.
    public boolean isFinished() 
    {
        // Check how much time has passed
        int passedTime = (int) (System.currentTimeMillis() - savedTime);
        if (passedTime > totalTime) 
        {
        return true;
        }
        else {
          return false;
        }
    }
}
Tagged:
Sign In or Register to comment.