Link to home
Start Free TrialLog in
Avatar of dervisakyuz
dervisakyuz

asked on

reading writing wav files in java

hi expert;

Now i am going to read wav files and apply this files to my code  and then obtain  output by writing another wav file.how can i do it?

thanks.


Avatar of Mayank S
Mayank S
Flag of India image

You need to use the javax.sound API for reading wav files:

http://java.sun.com/j2se/1.3/docs/guide/sound/prog_guide/javasoundTOC.fm.html

>> apply this files to my code  and then obtain  output

What does your code do to the file?
Avatar of dervisakyuz
dervisakyuz

ASKER

my project is about echo cancellation.it takes as input  the echo wav file and normal speaking wav file and it will give me without echo wav file.
i think i cant tell the problem i want to read a wav file into an array and then use it for an input of function;

sth like this.

int main(int argc, char *argv[])
{
  STEREO inbuf[TAPS], outbuf[TAPS];

  fprintf(stderr, "usage: aec_test [ambient in dB] <in.raw >out.raw\n");

  AEC aec;

  if (argc >= 2) {
    aec.setambient(MAXPCM*dB2q(atof(argv[1])));    
  }

  int taps;
  while (taps = fread(inbuf, sizeof(STEREO), TAPS, stdin)) {
    int i;
    for (i = 0; i < taps; ++i) {
      int s0 = inbuf[i].l;      /* left channel microphone */
      int s1 = inbuf[i].r;      /* right channel speaker */

      /* and do NLMS */
      s0 = aec.doAEC(s0, s1);

      /* copy back */
      outbuf[i].l = 0;          /* left channel silence */
      outbuf[i].r = s0;         /* right channel echo cancelled mic */
    }

    fwrite(outbuf, sizeof(STEREO), taps, stdout);
  }

  float ambient = aec.getambient();
  float ambientdB = q2dB(ambient / 32767.0f);
  fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB);
  fflush(NULL);
  return 0;
}
C++? Do you have Java code?
no:((  but i want to.i just want to write it in java but not same as it. Something different for example it reads raw files i want to read wav files.10 hours left and i have noting yet.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you but it is very difficult.isnt there any code about reading wav file into array??
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, you can read any file into an array using FileInputStream but I don't know how you want to use it to modify the array later:

http://www.javaalmanac.com/egs/java.io/File2ByteArray.html 
hi mayankeagle

could you write  this code in java  ?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "aec.h"

#define TAPS        (80*8)

typedef signed short MONO;

typedef struct {
  signed short l;
  signed short r;
} STEREO;

float dB2q(float dB)
{
  /* Dezibel to Ratio */
  return powf(10.0f, dB / 20.0f);
}

float q2dB(float q)
{
  /* Ratio to Dezibel */
  return 20.0f * log10f(q);
}

/* Read a raw audio file (8KHz sample frequency, 16bit PCM, stereo)
 * from stdin, echo cancel it and write it to stdout
 */
int main(int argc, char *argv[])
{
  STEREO inbuf[TAPS], outbuf[TAPS];

  fprintf(stderr, "usage: aec_test [ambient in dB] <in.raw >out.raw\n");

  AEC aec;

  if (argc >= 2) {
    aec.setambient(MAXPCM*dB2q(atof(argv[1])));    
  }

  int taps;
  while (taps = fread(inbuf, sizeof(STEREO), TAPS, stdin)) {
    int i;
    for (i = 0; i < taps; ++i) {
      int s0 = inbuf[i].l;      /* left channel microphone */
      int s1 = inbuf[i].r;      /* right channel speaker */

      /* and do NLMS */
      s0 = aec.doAEC(s0, s1);

      /* copy back */
      outbuf[i].l = 0;          /* left channel silence */
      outbuf[i].r = s0;         /* right channel echo cancelled mic */
    }

    fwrite(outbuf, sizeof(STEREO), taps, stdout);
  }

  float ambient = aec.getambient();
  float ambientdB = q2dB(ambient / 32767.0f);
  fprintf(stderr, "Ambient = %2.0f dB\n", ambientdB);
  fflush(NULL);
  return 0;
}
????? is it possible?
you say continue from this topic.
But we  stop here.

how can i use inbuf in java???
Well, I don't know about reading wav files in C++ to convert that code in Java for you. We'll have to wait till somebody else posts here.
hi

nobody cant do it for me?????
Better also post a link to this question in the C++ topic-area.
(there might be some experts there who have also worked on Java and can convert it for you, at least they will understand the existing C++ code better).