I have been working on the piece of code below in Processing using the sound library Minim, and have been trying to stop the audio recorded into the program from chopping the sound, rendering it somewhat unaudible.
Setup():
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);
}
And draw():
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++;
}
}
}
}
Any advise on solving this problem would be a really big help.
And sorry if it have not been asking to right way, it is my first question, and the guide confused me a little.