Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
bsondermann committed Oct 26, 2020
1 parent 15636be commit d7a2f3b
Show file tree
Hide file tree
Showing 5 changed files with 753 additions and 62 deletions.
8 changes: 4 additions & 4 deletions Blackbox2Sound.pde
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.io.InputStreamReader;
import java.util.LinkedList;
import javax.sound.sampled.*;
import java.io.*;
import java.nio.ByteBuffer;
SoundManager sound;
SDrop drop;
File[] logs;
Expand Down Expand Up @@ -49,7 +49,7 @@ void draw() {
stroke(0);

fill(100);
//rect(70,300,120,25);
rect(70,300,120,25);
rect(70,150,30,15);
//FILTER CHECK BOX
fill(255,0,0);
Expand Down Expand Up @@ -98,7 +98,7 @@ void draw() {
text("R | P | Y",width-85,35);
text(" Filtered | Unfiltered",70,168);
text(" Gyro | DTerm",70,268);
//text("Export Sound",70,297);
text("Export Sound",70,297);
fill(100);
rect(width/2,height-65,200,30);
fill(0);
Expand Down Expand Up @@ -148,7 +148,7 @@ void mousePressed(){
sound.setGyro(!sound.getGyro());
}
if(mouseX>10&&mouseX<130&&mouseY>288&&mouseY<312){
//sound.exportSound();
sound.exportSound(sketchPath()+"/export/");
}
if(mouseX>12&&mouseX<28&&mouseY>192&&mouseY<208){sound.setActive(!sound.getActive("ROLL"),"ROLL");}
if(mouseX>12+50&&mouseX<28+50&&mouseY>192&&mouseY<208){sound.setActive(!sound.getActive("PITCH"),"PITCH");}
Expand Down
122 changes: 99 additions & 23 deletions GyroSound.pde
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@ class GyroSound{
private float[] valFiltered,valUnfiltered,dtermval;
private String name;
private AudioSample hpfFiltered,hpfUnfiltered,dterm;
private HighPass hpFiltered, hpUnfiltered,hpfDterm;
private int sampleRate;
private int cutoff=150;
private boolean filterActive=false;
private boolean active=true;
private boolean gyroactive=true;
private int sampleRateExport=44100;
GyroSound(PApplet applet,float[] f,float[]u,float[]d, String name, int sampleRate){
this.applet = applet;
/*float maxf=0.0;
float maxu=0.0;
for(int i = 0; i< f.length;i++){
if(f[i]>maxf){maxf=f[i];}
if(u[i]>maxu){maxu=u[i];}
}*/
valFiltered=new float[f.length];
valUnfiltered=new float[u.length];
dtermval = new float[d.length];
Expand All @@ -29,16 +23,15 @@ class GyroSound{
}
this.name = name;
this.sampleRate = sampleRate;
valFiltered = hpf(valFiltered,10);
valUnfiltered = hpf(valUnfiltered,10);
dtermval = hpf(dtermval,10);

hpfFiltered = new AudioSample(this.applet,this.valFiltered,this.sampleRate);
hpfUnfiltered = new AudioSample(this.applet,this.valUnfiltered,this.sampleRate);
dterm = new AudioSample(this.applet,this.dtermval,this.sampleRate);
hpFiltered = new HighPass(this.applet);
hpFiltered.process(hpfFiltered,this.cutoff);
hpUnfiltered = new HighPass(this.applet);
hpUnfiltered.process(hpfUnfiltered,this.cutoff);
hpfDterm = new HighPass(this.applet);
hpfDterm.process(dterm,this.cutoff);
hpfFiltered = new AudioSample(this.applet,valFiltered,this.sampleRate);
hpfUnfiltered = new AudioSample(this.applet,valUnfiltered,this.sampleRate);
dterm = new AudioSample(this.applet,dtermval,this.sampleRate);
// new File(sketchPath()+"/export/").mkdir();
//saveAudio(sketchPath()+"/export/");

rmsu = new Amplitude(applet);
rmsu.input(hpfUnfiltered);
Expand All @@ -48,13 +41,7 @@ class GyroSound{
rmsd.input(dterm);
mute();
}
AudioSample[] getSamples(){
AudioSample[]ret = new AudioSample[3];
ret[0]=hpfFiltered;
ret[1]=hpfUnfiltered;
ret[2]=dterm;
return ret;
}

void jump(float t){

hpfFiltered.jump(t);
Expand Down Expand Up @@ -144,6 +131,95 @@ class GyroSound{
}
void setGyro(boolean gyromode){gyroactive = gyromode;mute();unmute();}
boolean getGyro(){return this.gyroactive;}
float[] lpf(float[]array,float factor){
factor = max(1,factor);
float val = array[0];
float[] ret = new float[array.length];
System.arraycopy(array,0,ret,0,array.length-1);
for(int i = 0; i<array.length;i++){
float currentValue = ret[i];
val +=((currentValue - val))/factor;
ret[i] = val;
}
return ret;
}
float[] hpf(float[]array,float factor){
factor = max(1,factor);
float val = array[0];
float[] ret = new float[array.length];
float[] lpf = lpf(array,factor);
System.arraycopy(array,0,ret,0,array.length-1);
for(int i = 0; i< array.length; i++){
ret[i]-=lpf[i];
}
return ret;
}
double[][] float2double(float[]in){
double[][]ret = new double[1][(int)(float(in.length)*(sampleRateExport/rate))];
float maxval=0;
for(int i = 0; i< in.length; i++){
maxval=max(abs(in[i]),maxval);
}
for(int i = 0; i< ret[0].length; i++){
ret[0][i] = map(in[(int)(float(i)*(rate/sampleRateExport))],-maxval,maxval,-1,1);
}
return ret;
}
void saveAudio(String path){
String fullpath=path+"/"+name;
new File(fullpath).mkdir();

try{
long numFrames = (long)(valUnfiltered.length*(sampleRateExport/rate));
WavFile wavFile = WavFile.newWavFile(new File(fullpath+"/filtered.wav"),1,numFrames,32,sampleRateExport);

long frameCounter = 0;

double[][] buffer = float2double(valFiltered);


// Write the buffer
wavFile.writeFrames(buffer, (int)numFrames);


// Close the wavFile
wavFile.close();
}catch(Exception e){e.printStackTrace();}

try{
long numFrames = (long)(valUnfiltered.length*(sampleRateExport/rate));
WavFile wavFile = WavFile.newWavFile(new File(fullpath+"/unfiltered.wav"),1,numFrames,32,sampleRateExport);

long frameCounter = 0;

double[][] buffer = float2double(valUnfiltered);


// Write the buffer
wavFile.writeFrames(buffer, (int)numFrames);


// Close the wavFile
wavFile.close();
}catch(Exception e){e.printStackTrace();}
if(!name.equals("yaw")){
try{
long numFrames = (long)(valUnfiltered.length*(sampleRateExport/rate));
WavFile wavFile = WavFile.newWavFile(new File(fullpath+"/dterm.wav"),1,numFrames,32,sampleRateExport);

long frameCounter = 0;

double[][] buffer = float2double(dtermval);


// Write the buffer
wavFile.writeFrames(buffer, (int)numFrames);


// Close the wavFile
wavFile.close();
}catch(Exception e){e.printStackTrace();}
}}
}


Expand Down
42 changes: 7 additions & 35 deletions SoundManager.pde
Original file line number Diff line number Diff line change
Expand Up @@ -205,39 +205,11 @@ class SoundManager {
boolean getGyro() {
return roll.getGyro();
}
/*void exportSound() {
float[] f = new float[roll.getSamples()[0].frames()];
roll.getSamples()[0].read(f);
exportFile(f);
}
void exportFile(float[]data) {
float max=0;
float min=0;
for (int i = 0; i< data.length; i++) {
if (data[i]>max) {
max=data[i];
}
if (data[i]<min) {
min=data[i];
}
}
byte[] export = new byte[data.length];
for (int i = 0; i<data.length; i++) {
export[i] = (byte)map(data[i], min, max, -127, 127);
}
AudioFormat frmt = new AudioFormat(int(rate)*1000, 8, 1, true, true);
AudioInputStream ais = new AudioInputStream(
new ByteArrayInputStream(export), frmt,
export.length / frmt.getFrameSize()
);
try {
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new
File(sketchPath() + "/test.wav")
);
}
catch(Exception e) {
e.printStackTrace();
}
}*/
void exportSound(String path) {
new File(path).mkdir();
roll.saveAudio(path);
pitch.saveAudio(path);
yaw.saveAudio(path);
}

}
Loading

0 comments on commit d7a2f3b

Please sign in to comment.