使用Minim时如何在Processing中停止声音斩波?

时间:2015-05-20 12:21:10

标签: audio processing javasound minim

我一直在处理使用声音库Minim处理下面的代码,并且一直试图阻止录制到程序中的音频斩断声音,使其听起来有点不可听。

设置():

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim sound;
AudioOutput speak;
AudioInput mic;
Sampler samp;
MultiChannelBuffer bigBuf;
AudioPlayer loadAudio;

slider frequency, amplitude;
button mute, record, play, stop;
trigger freqBut, ampBut, load;

float[] mainLeft = new float[1024];
float[] mainRight = new float[1024];
float amp = 1;
boolean muted = false, recording = false, playInit = false;

int sampleCount = 0;
int tempCount = 0;
int tempFreq = 0;

boolean tempUnder = false;
int tempTimeNew = 0;
int tempTimeOld = 0;

void setup() {
  size(512, 300);

  // UI
  frequency = new slider(10, 60, 300, 20, 0, 30, true, color(120), color(180), color(255), color(80, 0, 0));
  amplitude = new slider(10, 90, 300, 20, 0, 20, false, color(120), color(180), color(255), color(0, 0, 80));

  mute = new button(10, 10, 40, 40, color(120), color(180), color(255));
  record = new button(60, 10, 40, 40, color(120), color(180), color(255));
  play = new button(110, 10, 40, 40, color(120), color(180), color(255));
  stop = new button(160, 10, 40, 40, color(120), color(180), color(255));

  load = new trigger(210, 10, 40, 40, color(120), color(210), color(180), color(255));
  freqBut = new trigger(320, 60, 20, 20, color(40, 0, 0), color(220, 0, 0), color(180, 0, 0), color(255));
  ampBut = new trigger(320, 90, 20, 20, color(0, 0, 40), color(0, 0, 220), color(0, 0, 180), color(255));

  // Minim
  sound = new Minim(this);
  mic = sound.getLineIn();
  speak = sound.getLineOut(sound.STEREO, mic.bufferSize(), mic.sampleRate());

  loadAudio = sound.loadFile("sound.mp3");
  loadAudio.loop();
  loadAudio.mute();

  bigBuf = new MultiChannelBuffer(mic.bufferSize(), 2);
  samp = new Sampler(bigBuf, mic.sampleRate(), 2);

  samp.patch(speak);
}

并绘制():

void draw() {
  float[] micLeft;
  float[] micRight;
  micLeft = mic.left.toArray();
  micRight = mic.right.toArray();

  if (record.state == true) {
    recording = !recording;
  }

  if (recording == true) {

    int temp = mainLeft.length - 1;
    mainLeft = expand(mainLeft, temp + micLeft.length);
    mainRight = expand(mainRight, temp + micRight.length);
    sampleCount++;

    for (int i = 0; i < micLeft.length - 1; i++) {
      mainLeft[i + temp] = micLeft[i];
      mainRight[i + temp] = micRight[i];
    }
  }

  // Play
  if (play.state == true) {
    playInit = true;
  }

  if (playInit == true) {
    println("playing");

    if (tempTimeOld > tempTimeNew) {
      tempUnder = true;
    }

    tempTimeOld = tempTimeNew;

    tempTimeNew = millis() % micLeft.length;

    println(millis() % (micLeft.length));

    if (tempUnder == true) {
      if (tempCount == sampleCount) {
        playInit = false;
        tempCount = 0;
      }
      else {
        // Amplitude
        if (ampBut.state == true) {
          amp = map(amplitude.value, amplitude.minVal, amplitude.maxVal, 0.05, 20);
        }
        else {
          amp = 1;
        }

        if (freqBut.state == true) {
          float newFreq = map(frequency.value, frequency.minVal, frequency.maxVal, 140, 940);
          tempFreq = 0;

          for (int i = 0; i < micLeft.length - 2; i++) {
            if (micLeft[i] < micLeft[i + 1]) {
              tempFreq ++;
            }
          }

          for (int i = 0; i < micLeft.length - 1; i++) {

            bigBuf.setSample(0, i, mainLeft[int(i * newFreq / tempFreq) + (tempCount * micLeft.length)] * amp);
            bigBuf.setSample(1, i, mainRight[int(i * newFreq / tempFreq) + (tempCount * micRight.length)] * amp);
          }
        }
        else {
          for (int i = 0; i < micLeft.length - 1; i++) {

            bigBuf.setSample(0, i, mainLeft[i + (tempCount * micLeft.length)] * amp);
            bigBuf.setSample(1, i, mainRight[i + (tempCount * micRight.length)] * amp);
          }
        }
        samp.trigger();
        tempCount++;
      }
    }
  }
}

任何有关解决这个问题的建议都会有很大的帮助。

对不起,如果没有按照正确的方式提出要求,这是我的第一个问题,指南让我感到困惑。

0 个答案:

没有答案