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.


dervisakyuzAsked:
Who is Participating?
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, perhaps one thing to do is call your existing C++ code from Java using JNI:

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html 
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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?
0
 
dervisakyuzAuthor Commented:
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dervisakyuzAuthor Commented:
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;
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
C++? Do you have Java code?
0
 
dervisakyuzAuthor Commented:
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.
0
 
dervisakyuzAuthor Commented:
thank you but it is very difficult.isnt there any code about reading wav file into array??
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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 
0
 
dervisakyuzAuthor Commented:
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;
}
0
 
dervisakyuzAuthor Commented:
????? is it possible?
0
 
dervisakyuzAuthor Commented:
you say continue from this topic.
But we  stop here.

0
 
dervisakyuzAuthor Commented:
how can i use inbuf in java???
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
dervisakyuzAuthor Commented:
hi

nobody cant do it for me?????
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Better also post a link to this question in the C++ topic-area.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
(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).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.