简体   繁体   English

Audio Loop Processing.org(Java /最小声音库)

[英]Audio Loop Processing.org (Java/Minim sound Library)

I'm trying to create a long piece of audio which is a collection of recorded audio clips that are, in time, merged into one large collage of sounds. 我正在尝试创建一段较长的音频,该音频是记录的音频剪辑的集合,这些音频剪辑会及时合并为一个大的声音拼贴。 I am using the Minim sound library, but at the moment I'm having a hard time getting this to work. 我正在使用Minim声音库,但目前很难将其工作。 This works exactly the same as My programming skills are fairly basic but I was thinking this would be a very easy task! 这与我的编程技能是相当基本的,但我当时认为这将是一件非常容易的事! I :ant it to work just exactly as the 'RecordAndPlayback' in the examples folder of Minim but to have the events triggered by clicks done autonamously. 我:ant它的工作原理与Minim的example文件夹中的'RecordAndPlayback'完全相同,但是可以自动完成单击触发的事件。

Here's the code I have so far: 这是我到目前为止的代码:

import ddf.minim.*;
Minim minim;
AudioInput in;
AudioRecorder recorder;
AudioPlayer player;
Timer timer;
class Timer {

  int savedTime; // When Timer started
  int totalTime; // How long Timer should last

  Timer(int tempTotalTime) {
    totalTime = tempTotalTime;
  }

  // Starting the timer
   void start() {
    // When the timer starts it stores the current time in milliseconds.
    savedTime = millis();
  }

  // The function isFinished() returns true if 5,000 ms have passed.
  // The work of the timer is farmed out to this method.
  boolean isFinished() {
    // Check how much time has passed
    int passedTime = millis()- savedTime;
    if (passedTime > totalTime) {
      return true;
    } else {
      return false;
    }
  }
}
void setup()
{
  size(512, 200, P3D);
  textMode(SCREEN); 
  minim = new Minim(this);
  timer = new Timer(5000);
  timer.start();
  in = minim.getLineIn(Minim.STEREO, 2048);
  recorder = minim.createRecorder(in, "myrecording.wav", true);

  textFont(createFont("Arial", 12));
}
void draw()
{
  background(0);
  player =  minim.loadFile("myrecording.wav");
  player.play();
  player.loop();
//GUI
  stroke(255);
  for(int i = 0; i < in.left.size()-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);
  }
  //-- end of GUI
//recorder switching
  if (timer.isFinished())
  {
    text("not..", 5, 15);
    if(recorder.isRecording() == true){
     recorder.endRecord();
     recorder.save();
     timer.start();
    }
  }else
  {
    text("recording...", 5, 15);
    recorder.beginRecord();
   println(recorder.isRecording());
  }
  ///--- end recorder switching

}
void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  if ( player != null )
  {
    player.close();
  }
  minim.stop();

  super.stop();
}

Please any help would be much welcomed as this is driving me mad! 请任何帮助将非常欢迎,因为这使我发疯!

Thanks 谢谢

Daniel 丹尼尔

You seem to handle only one file and you're not loading what you're recording. 您似乎只处理一个文件,但没有加载正在录制的文件。

If you want to toggle recording on and off you could use a boolean to keep track of than, then save what was recorded. 如果要打开和关闭记录,可以使用布尔值来跟踪,然后保存记录的内容。

Here's a basic sketch using the modified sample: 这是使用修改后的示例的基本草图:

import ddf.minim.*;

Minim minim;
AudioInput in;
AudioRecorder recorder;
AudioPlayer player;

boolean isRecording;//are we recording ?

void setup(){
  size(512, 200);
  minim = new Minim(this);
  in = minim.getLineIn(Minim.STEREO, 512);
  recorder = minim.createRecorder(in, "rec.wav", true);
  textFont(createFont("Arial", 12));
}

void draw(){
  background(isRecording ? color(192,0,0) : color(0));
  stroke(255);
  for(int i = 0; i < in.left.size()-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' ){
    isRecording = !isRecording;//toggle recording
    if(isRecording){//if we need to record
      if(player != null &&  player.isPlaying()) player.pause();//stop any previously playing tracks, if any
      if(recorder.isRecording()) recorder.endRecord();//and the previous recording somehow wasn't ended, end it now
      recorder.beginRecord();//begin recording, according to docs: "If recording was previously halted, and save() was not called, samples will be appended to the end of the material recorded so far."
    }else recorder.endRecord();//stop recording
  }
  if ( key == 's' ){
    if(recorder != null) {
      recorder.save();
      println("Done saving ");
      player = minim.loadFile("rec.wav", 2048);
      player.play();
    }
  }
} 

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

If, for some reason you want to save each recording to a separate file, rather one, you could use some integers to hold the id of recording and the playing sample. 如果出于某种原因想要将每个录音保存到一个单独的文件中,而不是一个文件,则可以使用一些整数来保存录音ID和播放样本。

import ddf.minim.*;

Minim minim;
AudioInput in;
AudioRecorder recorder;
AudioPlayer player;

boolean isRecording;

int recID = -1;
int playID = 0;

void setup(){
  size(512, 200);
  minim = new Minim(this);
  in = minim.getLineIn(Minim.STEREO, 512);
  textFont(createFont("Arial", 12));
}

void draw(){
  background(0);
  stroke(255);
  for(int i = 0; i < in.left.size()-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 != null){
    if ( recorder.isRecording() ) text("Currently recording...", 5, 15);
    else                          text("Not recording.", 5, 15);
  }
  if(player != null && !isRecording){
    println("playing: " + playID); 
    if(player.position() >= player.length()){
      if(playID < recID){
        playID++;
        player = minim.loadFile("rec"+playID+".wav", 2048);
        player.play();
      }
    }
  }
}

void keyReleased(){
  if ( key == 'r' ){
    if(recorder != null && recorder.isRecording()) recorder.endRecord();
    else{
    recID++;
    recorder = minim.createRecorder(in, "rec"+recID+".wav", true);
    recorder.beginRecord();
    }
    isRecording = true;
  }
  if ( key == 's' ){
    if(recorder != null) {
      recorder.save();
      println("Done saving " + recID);
      player = minim.loadFile("rec"+recID+".wav", 2048);
      player.play();
      isRecording = false;
    }
  }
  if ( key == 'p' ){//play all
    playID = 0;
    player = minim.loadFile("rec"+playID+".wav", 2048);
    player.play();
  }
}

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

Goodluck ! 祝好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM