Enregistrer et filtrer un audio sous java
Résolu
Greg_Greg
Messages postés
8
Statut
Membre
-
KX Messages postés 19031 Statut Modérateur -
KX Messages postés 19031 Statut Modérateur -
Bonjour,
J'ai un programme java qui fait de l'enregistrement audio, mais quand je l'exécute je trouve pas le fichier audio enregistré :( !!
package com.javasrc.audio;
import javax.sound.sampled.*;
import java.io.File;
/**
* Sample audio recorder
*/
public class Recorder extends Thread
{
double i1, i2, o1, o2; /* temporary variables */
double a0, a1, a2; /* coefficients */
double b0, b1, b2; /* coefficients */
double f; /* last Frequency used */
double q; /* last Q used */
double g; /* last Gain used */
long t; /* last Type used */
static int maxfilterunits=10;
double FilterCell(long Unit, double Input, double Frequency, double Q, double Gain, int Type)
{
/* --------------------------------------------------------------- */
double Output=0,S,omega,A,sn,cs,alpha,beta,temp1,temp2,temp3,temp4;
/* -- check if frequency, Q, gain or type has changed.. and, if so, update coefficients */
if ( ( Frequency != f ) || ( Q != q ) || ( Gain != g ) || ( Type != t ) ) {
f = Frequency; q = Q; g = Gain; t = Type; /* remember last frequency, q, gain and type */
switch (Type) {
case 0: /* no filtering */
b0 = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
break;
case 1: /* lowpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = ( 1.0 - cs ) * a0 * Gain;
b0 = b1 * 0.5;
break;
case 2: /* highpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = -( 1.0 + cs ) * a0 * Gain;
b0 = -b1 * 0.5;
break;
case 3: /* bandpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn =Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = alpha * a0 * Gain;
break;
case 4: /* notch */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = a0 * Gain;
b1 = a1 * Gain;
break;
case 5: /* lowshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0 , ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 + temp4 + beta );
a1 = ( -2.0 * ( temp2 + temp3 ) ) * a0;
a2 = ( temp1 + temp4 - beta ) * a0;
b0 = ( A * ( temp1 - temp4 + beta ) ) * a0;
b1 = ( 2.0 * A * ( temp2 - temp3 ) ) * a0;
b2 = ( A * ( temp1 - temp4 - beta ) ) * a0;
break;
case 6: /* highshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 - temp4 + beta );
a1 = ( 2.0 * ( temp2 - temp3 ) ) * a0;
a2 = ( temp1 - temp4 - beta ) * a0;
b0 = ( A * ( temp1 + temp4 + beta ) ) * a0;
b1 = ( -2.0 * A * ( temp2 + temp3 ) ) * a0;
b2 = ( A * ( temp1 + temp4 - beta ) ) * a0;
break;
case 7: /* peaking */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
temp1 = alpha * A;
temp2 = alpha / A;
a0 = 1.0 / ( 1.0 + temp2 );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - temp2 ) * a0;
b0 = ( 1.0 + temp1 ) * a0;
b2 = ( 1.0 - temp1 ) * a0;
break;
}
}
/* -- filter loop: if you don't change the parameters of the filter dynamically, ~only this code will be executed. */
switch (Type) {
case 0: /* no filtering */
Output = b0*Input;
Output = Input;
break;
case 1: /* lowpass */
case 2: /* highpass */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 3: /* bandpass */
Output = b0*Input - b0*i2 - a1*o1 - a2*o2;
break;
case 4: /* notch */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 5: /* low shelving */
case 6: /* high shelving */
Output = b0*Input + b1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
case 7: /* peaking */
Output = b0*Input + a1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
}
o2=o1; o1=Output; i2=i1; i1=Input; /* update variables for recursion */
return(Output);
}
/**
* The TargetDataLine that we'll use to read data from
*/
private TargetDataLine line;
/**
* The audio format type that we'll encode the audio data with
*/
private AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
/**
* The AudioInputStream that we'll read the audio data from
*/
private AudioInputStream inputStream;
/**
* The file that we're going to write data out to
*/
File file=new File("Test.wav");
static final String fsPath = "D:/test/";
// private File file;
/**
* Creates a new Audio Recorder
*/
public Recorder( String outputFilename )
{
try
{
// Create an AudioFormat that specifies how the recording will be performed
// In this example we'll 44.1Khz, 16-bit, stereo
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding technique
44100.0F, // Sample Rate
16, // Number of bits in each channel
2, // Number of channels (2=stereo)
4, // Number of bytes in each frame
44100.0F, // Number of frames per second
false ); // Big-endian (true) or little-
// endian (false)
// Create our TargetDataLine that will be used to read audio data by first
// creating a DataLine instance for our audio format type
DataLine.Info info = new DataLine.Info( TargetDataLine.class, audioFormat );
// Next we ask the AudioSystem to retrieve a line that matches the
// DataLine Info
this.line = ( TargetDataLine )AudioSystem.getLine( info );
// Open the TargetDataLine with the specified format
this.line.open( audioFormat );
// Create an AudioInputStream that we can use to read from the line
this.inputStream = new AudioInputStream( this.line );
// Create the output file
this.file = new File( outputFilename );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void startRecording()
{
// Start the TargetDataLine
this.line.start();
// Start our thread
start();
}
public void stopRecording()
{
// Stop and close the TargetDataLine
this.line.stop();
this.line.close();
}
public void run()
{
try
{
// Ask the AudioSystem class to write audio data from the audio input stream
// to our file in the specified data type (PCM 44.1Khz, 16-bit, stereo)
AudioSystem.write( this.inputStream, this.targetType, this.file );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public static void main( String[] args )
{
if( args.length == 0 )
{
System.out.println( "Usage: Recorder <filename>" );
System.exit( 0 );
}
try
{
// Create a recorder that writes WAVE data to the specified filename
Recorder r = new Recorder( args[ 0 ] );
System.out.println( "Press ENTER to start recording" );
System.in.read();
// Start the recorder
r.startRecording();
System.out.println( "Press ENTER to stop recording" );
System.in.read();
// Stop the recorder
r.stopRecording();
System.out.println( "Recording complete" );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
et j'ai un un bout de code qui fait le filtrage d'un fichier audio mais je sais pas comment lui faire passer le fichier enregistré (dans le premier programme) pour le filtrer.
static int maxfilterunits=10; /* set it to the max number of filter units you may use (10 units = use units 0..9) */
double FilterCell(long Unit, double Input, double Frequency, double Q, double Gain, int Type)
{
/* --------------------------------------------------------------- */
double Output=0,S,omega,A,sn,cs,alpha,beta,temp1,temp2,temp3,temp4;
/* -- check if frequency, Q, gain or type has changed.. and, if so, update coefficients */
if ( ( Frequency != f ) || ( Q != q ) || ( Gain != g ) || ( Type != t ) ) {
f = Frequency; q = Q; g = Gain; t = Type; /* remember last frequency, q, gain and type */
switch (Type) {
case 0: /* no filtering */
b0 = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
break;
case 1: /* lowpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = ( 1.0 - cs ) * a0 * Gain;
b0 = b1 * 0.5;
break;
case 2: /* highpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = -( 1.0 + cs ) * a0 * Gain;
b0 = -b1 * 0.5;
break;
case 3: /* bandpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn =Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = alpha * a0 * Gain;
break;
case 4: /* notch */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = a0 * Gain;
b1 = a1 * Gain;
break;
case 5: /* lowshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0 , ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 + temp4 + beta );
a1 = ( -2.0 * ( temp2 + temp3 ) ) * a0;
a2 = ( temp1 + temp4 - beta ) * a0;
b0 = ( A * ( temp1 - temp4 + beta ) ) * a0;
b1 = ( 2.0 * A * ( temp2 - temp3 ) ) * a0;
b2 = ( A * ( temp1 - temp4 - beta ) ) * a0;
break;
case 6: /* highshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 - temp4 + beta );
a1 = ( 2.0 * ( temp2 - temp3 ) ) * a0;
a2 = ( temp1 - temp4 - beta ) * a0;
b0 = ( A * ( temp1 + temp4 + beta ) ) * a0;
b1 = ( -2.0 * A * ( temp2 + temp3 ) ) * a0;
b2 = ( A * ( temp1 + temp4 - beta ) ) * a0;
break;
case 7: /* peaking */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
temp1 = alpha * A;
temp2 = alpha / A;
a0 = 1.0 / ( 1.0 + temp2 );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - temp2 ) * a0;
b0 = ( 1.0 + temp1 ) * a0;
b2 = ( 1.0 - temp1 ) * a0;
break;
}
}
/* -- filter loop: if you don't change the parameters of the filter dynamically, ~only this code will be executed. */
switch (Type) {
case 0: /* no filtering */
Output = b0*Input;
Output = Input;
break;
case 1: /* lowpass */
case 2: /* highpass */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 3: /* bandpass */
Output = b0*Input - b0*i2 - a1*o1 - a2*o2;
break;
case 4: /* notch */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 5: /* low shelving */
case 6: /* high shelving */
Output = b0*Input + b1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
case 7: /* peaking */
Output = b0*Input + a1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
}
o2=o1; o1=Output; i2=i1; i1=Input; /* update variables for recursion */
return(Output);
}
Quelqu'un peut m'aider SVP!
J'ai un programme java qui fait de l'enregistrement audio, mais quand je l'exécute je trouve pas le fichier audio enregistré :( !!
package com.javasrc.audio;
import javax.sound.sampled.*;
import java.io.File;
/**
* Sample audio recorder
*/
public class Recorder extends Thread
{
double i1, i2, o1, o2; /* temporary variables */
double a0, a1, a2; /* coefficients */
double b0, b1, b2; /* coefficients */
double f; /* last Frequency used */
double q; /* last Q used */
double g; /* last Gain used */
long t; /* last Type used */
static int maxfilterunits=10;
double FilterCell(long Unit, double Input, double Frequency, double Q, double Gain, int Type)
{
/* --------------------------------------------------------------- */
double Output=0,S,omega,A,sn,cs,alpha,beta,temp1,temp2,temp3,temp4;
/* -- check if frequency, Q, gain or type has changed.. and, if so, update coefficients */
if ( ( Frequency != f ) || ( Q != q ) || ( Gain != g ) || ( Type != t ) ) {
f = Frequency; q = Q; g = Gain; t = Type; /* remember last frequency, q, gain and type */
switch (Type) {
case 0: /* no filtering */
b0 = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
break;
case 1: /* lowpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = ( 1.0 - cs ) * a0 * Gain;
b0 = b1 * 0.5;
break;
case 2: /* highpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = -( 1.0 + cs ) * a0 * Gain;
b0 = -b1 * 0.5;
break;
case 3: /* bandpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn =Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = alpha * a0 * Gain;
break;
case 4: /* notch */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = a0 * Gain;
b1 = a1 * Gain;
break;
case 5: /* lowshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0 , ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 + temp4 + beta );
a1 = ( -2.0 * ( temp2 + temp3 ) ) * a0;
a2 = ( temp1 + temp4 - beta ) * a0;
b0 = ( A * ( temp1 - temp4 + beta ) ) * a0;
b1 = ( 2.0 * A * ( temp2 - temp3 ) ) * a0;
b2 = ( A * ( temp1 - temp4 - beta ) ) * a0;
break;
case 6: /* highshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 - temp4 + beta );
a1 = ( 2.0 * ( temp2 - temp3 ) ) * a0;
a2 = ( temp1 - temp4 - beta ) * a0;
b0 = ( A * ( temp1 + temp4 + beta ) ) * a0;
b1 = ( -2.0 * A * ( temp2 + temp3 ) ) * a0;
b2 = ( A * ( temp1 + temp4 - beta ) ) * a0;
break;
case 7: /* peaking */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
temp1 = alpha * A;
temp2 = alpha / A;
a0 = 1.0 / ( 1.0 + temp2 );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - temp2 ) * a0;
b0 = ( 1.0 + temp1 ) * a0;
b2 = ( 1.0 - temp1 ) * a0;
break;
}
}
/* -- filter loop: if you don't change the parameters of the filter dynamically, ~only this code will be executed. */
switch (Type) {
case 0: /* no filtering */
Output = b0*Input;
Output = Input;
break;
case 1: /* lowpass */
case 2: /* highpass */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 3: /* bandpass */
Output = b0*Input - b0*i2 - a1*o1 - a2*o2;
break;
case 4: /* notch */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 5: /* low shelving */
case 6: /* high shelving */
Output = b0*Input + b1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
case 7: /* peaking */
Output = b0*Input + a1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
}
o2=o1; o1=Output; i2=i1; i1=Input; /* update variables for recursion */
return(Output);
}
/**
* The TargetDataLine that we'll use to read data from
*/
private TargetDataLine line;
/**
* The audio format type that we'll encode the audio data with
*/
private AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
/**
* The AudioInputStream that we'll read the audio data from
*/
private AudioInputStream inputStream;
/**
* The file that we're going to write data out to
*/
File file=new File("Test.wav");
static final String fsPath = "D:/test/";
// private File file;
/**
* Creates a new Audio Recorder
*/
public Recorder( String outputFilename )
{
try
{
// Create an AudioFormat that specifies how the recording will be performed
// In this example we'll 44.1Khz, 16-bit, stereo
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding technique
44100.0F, // Sample Rate
16, // Number of bits in each channel
2, // Number of channels (2=stereo)
4, // Number of bytes in each frame
44100.0F, // Number of frames per second
false ); // Big-endian (true) or little-
// endian (false)
// Create our TargetDataLine that will be used to read audio data by first
// creating a DataLine instance for our audio format type
DataLine.Info info = new DataLine.Info( TargetDataLine.class, audioFormat );
// Next we ask the AudioSystem to retrieve a line that matches the
// DataLine Info
this.line = ( TargetDataLine )AudioSystem.getLine( info );
// Open the TargetDataLine with the specified format
this.line.open( audioFormat );
// Create an AudioInputStream that we can use to read from the line
this.inputStream = new AudioInputStream( this.line );
// Create the output file
this.file = new File( outputFilename );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void startRecording()
{
// Start the TargetDataLine
this.line.start();
// Start our thread
start();
}
public void stopRecording()
{
// Stop and close the TargetDataLine
this.line.stop();
this.line.close();
}
public void run()
{
try
{
// Ask the AudioSystem class to write audio data from the audio input stream
// to our file in the specified data type (PCM 44.1Khz, 16-bit, stereo)
AudioSystem.write( this.inputStream, this.targetType, this.file );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public static void main( String[] args )
{
if( args.length == 0 )
{
System.out.println( "Usage: Recorder <filename>" );
System.exit( 0 );
}
try
{
// Create a recorder that writes WAVE data to the specified filename
Recorder r = new Recorder( args[ 0 ] );
System.out.println( "Press ENTER to start recording" );
System.in.read();
// Start the recorder
r.startRecording();
System.out.println( "Press ENTER to stop recording" );
System.in.read();
// Stop the recorder
r.stopRecording();
System.out.println( "Recording complete" );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
et j'ai un un bout de code qui fait le filtrage d'un fichier audio mais je sais pas comment lui faire passer le fichier enregistré (dans le premier programme) pour le filtrer.
static int maxfilterunits=10; /* set it to the max number of filter units you may use (10 units = use units 0..9) */
double FilterCell(long Unit, double Input, double Frequency, double Q, double Gain, int Type)
{
/* --------------------------------------------------------------- */
double Output=0,S,omega,A,sn,cs,alpha,beta,temp1,temp2,temp3,temp4;
/* -- check if frequency, Q, gain or type has changed.. and, if so, update coefficients */
if ( ( Frequency != f ) || ( Q != q ) || ( Gain != g ) || ( Type != t ) ) {
f = Frequency; q = Q; g = Gain; t = Type; /* remember last frequency, q, gain and type */
switch (Type) {
case 0: /* no filtering */
b0 = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
break;
case 1: /* lowpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = ( 1.0 - cs ) * a0 * Gain;
b0 = b1 * 0.5;
break;
case 2: /* highpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = -( 1.0 + cs ) * a0 * Gain;
b0 = -b1 * 0.5;
break;
case 3: /* bandpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn =Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = alpha * a0 * Gain;
break;
case 4: /* notch */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = a0 * Gain;
b1 = a1 * Gain;
break;
case 5: /* lowshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0 , ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 + temp4 + beta );
a1 = ( -2.0 * ( temp2 + temp3 ) ) * a0;
a2 = ( temp1 + temp4 - beta ) * a0;
b0 = ( A * ( temp1 - temp4 + beta ) ) * a0;
b1 = ( 2.0 * A * ( temp2 - temp3 ) ) * a0;
b2 = ( A * ( temp1 - temp4 - beta ) ) * a0;
break;
case 6: /* highshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 - temp4 + beta );
a1 = ( 2.0 * ( temp2 - temp3 ) ) * a0;
a2 = ( temp1 - temp4 - beta ) * a0;
b0 = ( A * ( temp1 + temp4 + beta ) ) * a0;
b1 = ( -2.0 * A * ( temp2 + temp3 ) ) * a0;
b2 = ( A * ( temp1 + temp4 - beta ) ) * a0;
break;
case 7: /* peaking */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
temp1 = alpha * A;
temp2 = alpha / A;
a0 = 1.0 / ( 1.0 + temp2 );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - temp2 ) * a0;
b0 = ( 1.0 + temp1 ) * a0;
b2 = ( 1.0 - temp1 ) * a0;
break;
}
}
/* -- filter loop: if you don't change the parameters of the filter dynamically, ~only this code will be executed. */
switch (Type) {
case 0: /* no filtering */
Output = b0*Input;
Output = Input;
break;
case 1: /* lowpass */
case 2: /* highpass */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 3: /* bandpass */
Output = b0*Input - b0*i2 - a1*o1 - a2*o2;
break;
case 4: /* notch */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 5: /* low shelving */
case 6: /* high shelving */
Output = b0*Input + b1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
case 7: /* peaking */
Output = b0*Input + a1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
}
o2=o1; o1=Output; i2=i1; i1=Input; /* update variables for recursion */
return(Output);
}
Quelqu'un peut m'aider SVP!
A voir également:
- Java audio recorder
- Jeux java itel - Télécharger - Jeux vidéo
- Waptrick java football - Télécharger - Jeux vidéo
- Musique audio - Télécharger - Lecture & Playlists
- Realtek audio driver - Télécharger - Pilotes & Matériel
- Streaming audio recorder gratuit - Télécharger - Téléchargement & Transfert
10 réponses
Je le trouve toujours pas, le chemin est affiché dans la console mais le fichier audio n'existe pas dans le dossier spécifié ! :(
Voici le code original, avec ta ligne de code System.out.println(file.getAbsolutePath)
package com.javasrc.audio;
import javax.sound.sampled.*;
import java.io.File;
/**
* Sample audio recorder
*/
public class Recorder extends Thread
{
/**
* The TargetDataLine that we'll use to read data from
*/
private TargetDataLine line;
/**
* The audio format type that we'll encode the audio data with
*/
private AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
/**
* The AudioInputStream that we'll read the audio data from
*/
private AudioInputStream inputStream;
/**
* The file that we're going to write data out to
*/
//static File file=new File("Test.wav");
private static File file;
/**
* Creates a new Audio Recorder
*/
public Recorder( String outputFilename )
{
try
{
// Create an AudioFormat that specifies how the recording will be performed
// In this example we'll 44.1Khz, 16-bit, stereo
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding technique
44100.0F, // Sample Rate
16, // Number of bits in each channel
2, // Number of channels (2=stereo)
4, // Number of bytes in each frame
44100.0F, // Number of frames per second
false ); // Big-endian (true) or little-
// endian (false)
// Create our TargetDataLine that will be used to read audio data by first
// creating a DataLine instance for our audio format type
DataLine.Info info = new DataLine.Info( TargetDataLine.class, audioFormat );
// Next we ask the AudioSystem to retrieve a line that matches the
// DataLine Info
this.line = ( TargetDataLine )AudioSystem.getLine( info );
// Open the TargetDataLine with the specified format
this.line.open( audioFormat );
// Create an AudioInputStream that we can use to read from the line
this.inputStream = new AudioInputStream( this.line );
// Create the output file
this.file = new File( outputFilename );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void startRecording()
{
// Start the TargetDataLine
this.line.start();
// Start our thread
start();
}
public void stopRecording()
{
// Stop and close the TargetDataLine
this.line.stop();
this.line.close();
}
public void run()
{
try
{
// Ask the AudioSystem class to write audio data from the audio input stream
// to our file in the specified data type (PCM 44.1Khz, 16-bit, stereo)
AudioSystem.write( this.inputStream, this.targetType, this.file );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public static void main( String[] args )
{
if( args.length == 0 )
{
System.out.println( "Usage: Recorder <filename>" );
System.out.println(file.getAbsoluteFile());
System.exit( 0 );
}
try
{
// Create a recorder that writes WAVE data to the specified filename
Recorder r = new Recorder( args[ 0 ] );
System.out.println( "Press ENTER to start recording" );
System.in.read();
// Start the recorder
r.startRecording();
System.out.println( "Press ENTER to stop recording" );
System.in.read();
// Stop the recorder
r.stopRecording();
System.out.println( "Recording complete" );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
Et voila ce que j'ai comme erreur :
Usage: Recorder <filename>
Exception in thread "main" java.lang.NullPointerException
at com.javasrc.audio.Recorder.main(Recorder.java:115)
Merci a tous
package com.javasrc.audio;
import javax.sound.sampled.*;
import java.io.File;
/**
* Sample audio recorder
*/
public class Recorder extends Thread
{
/**
* The TargetDataLine that we'll use to read data from
*/
private TargetDataLine line;
/**
* The audio format type that we'll encode the audio data with
*/
private AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
/**
* The AudioInputStream that we'll read the audio data from
*/
private AudioInputStream inputStream;
/**
* The file that we're going to write data out to
*/
//static File file=new File("Test.wav");
private static File file;
/**
* Creates a new Audio Recorder
*/
public Recorder( String outputFilename )
{
try
{
// Create an AudioFormat that specifies how the recording will be performed
// In this example we'll 44.1Khz, 16-bit, stereo
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding technique
44100.0F, // Sample Rate
16, // Number of bits in each channel
2, // Number of channels (2=stereo)
4, // Number of bytes in each frame
44100.0F, // Number of frames per second
false ); // Big-endian (true) or little-
// endian (false)
// Create our TargetDataLine that will be used to read audio data by first
// creating a DataLine instance for our audio format type
DataLine.Info info = new DataLine.Info( TargetDataLine.class, audioFormat );
// Next we ask the AudioSystem to retrieve a line that matches the
// DataLine Info
this.line = ( TargetDataLine )AudioSystem.getLine( info );
// Open the TargetDataLine with the specified format
this.line.open( audioFormat );
// Create an AudioInputStream that we can use to read from the line
this.inputStream = new AudioInputStream( this.line );
// Create the output file
this.file = new File( outputFilename );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void startRecording()
{
// Start the TargetDataLine
this.line.start();
// Start our thread
start();
}
public void stopRecording()
{
// Stop and close the TargetDataLine
this.line.stop();
this.line.close();
}
public void run()
{
try
{
// Ask the AudioSystem class to write audio data from the audio input stream
// to our file in the specified data type (PCM 44.1Khz, 16-bit, stereo)
AudioSystem.write( this.inputStream, this.targetType, this.file );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public static void main( String[] args )
{
if( args.length == 0 )
{
System.out.println( "Usage: Recorder <filename>" );
System.out.println(file.getAbsoluteFile());
System.exit( 0 );
}
try
{
// Create a recorder that writes WAVE data to the specified filename
Recorder r = new Recorder( args[ 0 ] );
System.out.println( "Press ENTER to start recording" );
System.in.read();
// Start the recorder
r.startRecording();
System.out.println( "Press ENTER to stop recording" );
System.in.read();
// Stop the recorder
r.stopRecording();
System.out.println( "Recording complete" );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
Et voila ce que j'ai comme erreur :
Usage: Recorder <filename>
Exception in thread "main" java.lang.NullPointerException
at com.javasrc.audio.Recorder.main(Recorder.java:115)
Merci a tous
Dans le premier code tu avais File file = new File("Test.wav"); et c'est le chemin absolu de ce fichier que je te demandais d'afficher (l est décrit par un chemin relatif et ce que tu veux c'est le chemin absolu)
Or dans ce deuxième code tu fais File file; et tu ne l'initialises que bien plus tard dans le code, au moment du new Recorder(args[0]). Donc si ça plante c'est parce qu'au moment où tu affiches son chemin, file vaut null ! Ce qu'il te faut afficher c'est finalement new File(args[0]).getAbsolutPath(); ou alors mais ça revient normalement au même, faire l'affichage après le new Recorder(args[0])
Or dans ce deuxième code tu fais File file; et tu ne l'initialises que bien plus tard dans le code, au moment du new Recorder(args[0]). Donc si ça plante c'est parce qu'au moment où tu affiches son chemin, file vaut null ! Ce qu'il te faut afficher c'est finalement new File(args[0]).getAbsolutPath(); ou alors mais ça revient normalement au même, faire l'affichage après le new Recorder(args[0])
Merci pour tes explication, mais tu a raison ça revient au même.
J'ai essayé tout et j'ai toujours cette erreur au moment de l'exécution de mon programme, et le fichier son ne pourra pas être enregistré, et le problème c'est que je vois pas ou est l'erreur dans mon code.
quand j'ajoute System.out.println(file.getAbsolutPath());ou new File(args[0]).getAbsolutPath(); dans mon main c'est une erreur d'exception qu'il me renvoi et quand je les retire le programme exécute et le fichier son n'est nul part :('
Je vais m'y mettre a nouveau
@ +
J'ai essayé tout et j'ai toujours cette erreur au moment de l'exécution de mon programme, et le fichier son ne pourra pas être enregistré, et le problème c'est que je vois pas ou est l'erreur dans mon code.
quand j'ajoute System.out.println(file.getAbsolutPath());ou new File(args[0]).getAbsolutPath(); dans mon main c'est une erreur d'exception qu'il me renvoi et quand je les retire le programme exécute et le fichier son n'est nul part :('
Je vais m'y mettre a nouveau
@ +
Donne directement à args[0] un chemin absolu (D:\\Test.wav par exemple) chez moi le fichier Test.wav est alors bien enregistré dans D:\
Mais chez moi ce code fonctionne pour savoir où est le chemin des anciens fichiers enregistré :
Mais chez moi ce code fonctionne pour savoir où est le chemin des anciens fichiers enregistré :
System.out.println( "Recording complete" ); System.out.println(file.getAbsolutePath());
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
STP u peux me donner la ligne du code ou t'a modifié le args[0] à D:\\Test.wav (ce que tu a modifié toi )
ça fait 3 jours que je travailles sur ce code :) et je voudrais bien voir mon résultat :)
Merci
ça fait 3 jours que je travailles sur ce code :) et je voudrais bien voir mon résultat :)
Merci
Quand je lance mon programme voila ce qui m'affiche dans la console :
Usage: Recorder <filename>
C:\Users\workspace\Reco_Vocale\Test.wav
Mais quand j'y vais a ce dossier je trouve rien.
tu peux me dire il t'affiche quoi le programme quand tu le lance chez toi, et comment tu arrive a lui donner en premier argument un chemin absolu!?
Merci beaucoup
Usage: Recorder <filename>
C:\Users\workspace\Reco_Vocale\Test.wav
Mais quand j'y vais a ce dossier je trouve rien.
tu peux me dire il t'affiche quoi le programme quand tu le lance chez toi, et comment tu arrive a lui donner en premier argument un chemin absolu!?
Merci beaucoup
Voici ton code, légèrement remanié, de sorte que tu ne devrais plus avoir de problème :
import javax.sound.sampled.*; import java.io.File; import java.io.IOException; import java.util.Scanner; /** Sample audio recorder */ public class Recorder extends Thread { /** The TargetDataLine that we'll use to read data from */ private final TargetDataLine line; /** The audio format type that we'll encode the audio data with */ private final AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE; /** The AudioInputStream that we'll read the audio data from */ private final AudioInputStream inputStream; /** The file that we're going to write data out to */ private File file; /** * Creates a new Audio Recorder * @param outputFilename * @throws LineUnavailableException */ public Recorder(String outputFilename) throws LineUnavailableException { // Create an AudioFormat that specifies how the recording will be performed // In this example we'll 44.1Khz, 16-bit, stereo AudioFormat audioFormat = new AudioFormat ( AudioFormat.Encoding.PCM_SIGNED, // Encoding technique 44100.0F, // Sample Rate 16, // Number of bits in each channel 2, // Number of channels (2=stereo) 4, // Number of bytes in each frame 44100.0F, // Number of frames per second false // Big-endian (true) or little-endian (false) ); // Create our TargetDataLine that will be used to read audio data by first // creating a DataLine instance for our audio format type DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); // Next we ask the AudioSystem to retrieve a line that matches the // DataLine Info line = (TargetDataLine) AudioSystem.getLine(info); // Open the TargetDataLine with the specified format line.open(audioFormat); // Create an AudioInputStream that we can use to read from the line inputStream = new AudioInputStream(line); // Create the output file file = new File(outputFilename); } public void startRecording() { // Start the TargetDataLine line.start(); // Start our thread start(); } public void stopRecording() { // Stop and close the TargetDataLine line.stop(); line.close(); } public void run() { // Ask the AudioSystem class to write audio data from the audio input stream // to our file in the specified data type (PCM 44.1Khz, 16-bit, stereo) try { AudioSystem.write(inputStream,targetType,file); } catch (IOException e) { e.printStackTrace(); } } public String getPath() { return file.getAbsolutePath(); } public static void pressEnter(String text) { System.out.println(text); new Scanner(System.in).nextLine(); } public static void main(String...args) { // Setting fileName String fileName = "default.wav"; // default; if(args.length == 0) System.out.println("Usage: java Recorder <filename>\n" + "Default file will be used\n"); else fileName = args[0]; try { // Create a recorder that writes WAVE data to the specified filename Recorder r = new Recorder(fileName); // Start the recorder pressEnter("Press ENTER to start recording"); r.startRecording(); // Stop the recorder pressEnter("Press ENTER to stop recording"); r.stopRecording(); System.out.println("Recording complete : "+r.getPath()); } catch (LineUnavailableException e) { e.printStackTrace(); } } }
Enfin pas tout à fait fini !!! il reste a faire passer ce fichier son via filtre passe bande que j'ai déjà posté au début ! tout sera posté à la fin de ce programme.
je le fait pour apprendre et les autres puissent en profiter!
@plus
je le fait pour apprendre et les autres puissent en profiter!
@plus
pour filtrer le son passé via un micro j'ai fait une petite recherche et je suis tombé sur cet article http://membres.multimania.fr/amycoders/tutorials/maverick/sfftm.html qui propose un filtre en java
double i1, i2, o1, o2; /* temporary variables */
double a0, a1, a2; /* coefficients */
double b0, b1, b2; /* coefficients */
double f; /* last Frequency used */
double q; /* last Q used */
double g; /* last Gain used */
long t; /* last Type used */
static int maxfilterunits=10;
double FilterCell(long Unit, double Input, double Frequency, double Q, double Gain, int Type)
{
/* --------------------------------------------------------------- */
double Output=0,S,omega,A,sn,cs,alpha,beta,temp1,temp2,temp3,temp4;
/* -- check if frequency, Q, gain or type has changed.. and, if so, update coefficients */
if ( ( Frequency != f ) || ( Q != q ) || ( Gain != g ) || ( Type != t ) ) {
f = Frequency; q = Q; g = Gain; t = Type; /* remember last frequency, q, gain and type */
switch (Type) {
case 0: /* no filtering */
b0 = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
break;
case 1: /* lowpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = ( 1.0 - cs ) * a0 * Gain;
b0 = b1 * 0.5;
break;
case 2: /* highpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = -( 1.0 + cs ) * a0 * Gain;
b0 = -b1 * 0.5;
break;
case 3: /* bandpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn =Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = alpha * a0 * Gain;
break;
case 4: /* notch */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = a0 * Gain;
b1 = a1 * Gain;
break;
case 5: /* lowshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0 , ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 + temp4 + beta );
a1 = ( -2.0 * ( temp2 + temp3 ) ) * a0;
a2 = ( temp1 + temp4 - beta ) * a0;
b0 = ( A * ( temp1 - temp4 + beta ) ) * a0;
b1 = ( 2.0 * A * ( temp2 - temp3 ) ) * a0;
b2 = ( A * ( temp1 - temp4 - beta ) ) * a0;
break;
case 6: /* highshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 - temp4 + beta );
a1 = ( 2.0 * ( temp2 - temp3 ) ) * a0;
a2 = ( temp1 - temp4 - beta ) * a0;
b0 = ( A * ( temp1 + temp4 + beta ) ) * a0;
b1 = ( -2.0 * A * ( temp2 + temp3 ) ) * a0;
b2 = ( A * ( temp1 + temp4 - beta ) ) * a0;
break;
case 7: /* peaking */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
temp1 = alpha * A;
temp2 = alpha / A;
a0 = 1.0 / ( 1.0 + temp2 );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - temp2 ) * a0;
b0 = ( 1.0 + temp1 ) * a0;
b2 = ( 1.0 - temp1 ) * a0;
break;
}
}
/* -- filter loop: if you don't change the parameters of the filter dynamically, ~only this code will be executed. */
switch (Type) {
case 0: /* no filtering */
Output = b0*Input;
Output = Input;
break;
case 1: /* lowpass */
case 2: /* highpass */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 3: /* bandpass */
Output = b0*Input - b0*i2 - a1*o1 - a2*o2;
break;
case 4: /* notch */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 5: /* low shelving */
case 6: /* high shelving */
Output = b0*Input + b1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
case 7: /* peaking */
Output = b0*Input + a1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
}
o2=o1; o1=Output; i2=i1; i1=Input; /* update variables for recursion */
return(Output);
}
}
mais je sais pas trop comment l'utiliser et l'anglais n'est pas trop mon truc !
Si on finit le boulot ?! :) :) :) !
Je sais pas trop si c'est faisable déjà :( !
Merci
double i1, i2, o1, o2; /* temporary variables */
double a0, a1, a2; /* coefficients */
double b0, b1, b2; /* coefficients */
double f; /* last Frequency used */
double q; /* last Q used */
double g; /* last Gain used */
long t; /* last Type used */
static int maxfilterunits=10;
double FilterCell(long Unit, double Input, double Frequency, double Q, double Gain, int Type)
{
/* --------------------------------------------------------------- */
double Output=0,S,omega,A,sn,cs,alpha,beta,temp1,temp2,temp3,temp4;
/* -- check if frequency, Q, gain or type has changed.. and, if so, update coefficients */
if ( ( Frequency != f ) || ( Q != q ) || ( Gain != g ) || ( Type != t ) ) {
f = Frequency; q = Q; g = Gain; t = Type; /* remember last frequency, q, gain and type */
switch (Type) {
case 0: /* no filtering */
b0 = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
break;
case 1: /* lowpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = ( 1.0 - cs ) * a0 * Gain;
b0 = b1 * 0.5;
break;
case 2: /* highpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b1 = -( 1.0 + cs ) * a0 * Gain;
b0 = -b1 * 0.5;
break;
case 3: /* bandpass */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn =Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = alpha * a0 * Gain;
break;
case 4: /* notch */
Gain = Math.pow( 10.0, Gain / 20.0 ); /* convert from dB to linear */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
a0 = 1.0 / ( 1.0 + alpha );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - alpha ) * a0;
b0 = a0 * Gain;
b1 = a1 * Gain;
break;
case 5: /* lowshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0 , ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 + temp4 + beta );
a1 = ( -2.0 * ( temp2 + temp3 ) ) * a0;
a2 = ( temp1 + temp4 - beta ) * a0;
b0 = ( A * ( temp1 - temp4 + beta ) ) * a0;
b1 = ( 2.0 * A * ( temp2 - temp3 ) ) * a0;
b2 = ( A * ( temp1 - temp4 - beta ) ) * a0;
break;
case 6: /* highshelf */
/* "shelf slope" 1.0 = max slope, because neither Q nor bandwidth is used in */
/* those filters (note: true only for lowshelf and highshelf, not peaking). */
S = 1.0; /* used only by lowshelf and highshelf */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
temp1 = A + 1.0; temp2 = A - 1.0; temp3 = temp1 * cs; temp4 = temp2 * cs;
beta = sn * Math.sqrt( ( A * A + 1.0 ) / S - temp2 * temp2 );
a0 = 1.0 / ( temp1 - temp4 + beta );
a1 = ( 2.0 * ( temp2 - temp3 ) ) * a0;
a2 = ( temp1 - temp4 - beta ) * a0;
b0 = ( A * ( temp1 + temp4 + beta ) ) * a0;
b1 = ( -2.0 * A * ( temp2 + temp3 ) ) * a0;
b2 = ( A * ( temp1 + temp4 - beta ) ) * a0;
break;
case 7: /* peaking */
A = Math.pow( 10.0, ( Gain / 40.0 ) ); /* Gain is expressed in dB */
omega = ( Math.PI*2 * Frequency ) / 44100;
sn = Math.sin( omega ); cs = Math.cos( omega );
alpha = sn / ( 2.0 * Q );
temp1 = alpha * A;
temp2 = alpha / A;
a0 = 1.0 / ( 1.0 + temp2 );
a1 = ( -2.0 * cs ) * a0;
a2 = ( 1.0 - temp2 ) * a0;
b0 = ( 1.0 + temp1 ) * a0;
b2 = ( 1.0 - temp1 ) * a0;
break;
}
}
/* -- filter loop: if you don't change the parameters of the filter dynamically, ~only this code will be executed. */
switch (Type) {
case 0: /* no filtering */
Output = b0*Input;
Output = Input;
break;
case 1: /* lowpass */
case 2: /* highpass */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 3: /* bandpass */
Output = b0*Input - b0*i2 - a1*o1 - a2*o2;
break;
case 4: /* notch */
Output = b0*Input + b1*i1 + b0*i2 - a1*o1 - a2*o2;
break;
case 5: /* low shelving */
case 6: /* high shelving */
Output = b0*Input + b1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
case 7: /* peaking */
Output = b0*Input + a1*i1 + b2*i2 - a1*o1 - a2*o2;
break;
}
o2=o1; o1=Output; i2=i1; i1=Input; /* update variables for recursion */
return(Output);
}
}
mais je sais pas trop comment l'utiliser et l'anglais n'est pas trop mon truc !
Si on finit le boulot ?! :) :) :) !
Je sais pas trop si c'est faisable déjà :( !
Merci
Un peu d'explication !!! dans le genre "un signal audio est composé de sons à différentes fréquences, je veux éliminer tous les sons qui ont des fréquences inférieures à 5kHz ou supérieures à 15kHz" (par exemple)
je veux filtrer mon son pour avoir une bonne qualité d'enregistrement ! et si y a des bibliothèques java qui font ça ça sera mieux que de reprendre ce code !
pour ceux qui s'intéressent au traitement de signal et au filtrage sonore je vous conseilles de voir ce lien ( en anglais bien évidemment) http://membres.multimania.fr/amycoders/tutorials/maverick/sfftm.html
je veux filtrer mon son pour avoir une bonne qualité d'enregistrement ! et si y a des bibliothèques java qui font ça ça sera mieux que de reprendre ce code !
pour ceux qui s'intéressent au traitement de signal et au filtrage sonore je vous conseilles de voir ce lien ( en anglais bien évidemment) http://membres.multimania.fr/amycoders/tutorials/maverick/sfftm.html
Moi perso je n'arrive pas à tester, mais comme j'ai pas de micro ceci explique surement cela ^^
Pour avoir des exemples de code, le mieux c'est regarder sur le site officiel Java Sound Demo et de potasser le code source des parties qui t'intéressent...
Pour avoir des exemples de code, le mieux c'est regarder sur le site officiel Java Sound Demo et de potasser le code source des parties qui t'intéressent...