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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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.
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;
}
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Mayank SAssociate Director - Product EngineeringCommented:
C++? Do you have Java code?
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.
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 

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

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

dervisakyuzAuthor Commented:
how can i use inbuf in java???
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.
dervisakyuzAuthor Commented:
hi

nobody cant do it for me?????
Mayank SAssociate Director - Product EngineeringCommented:
Better also post a link to this question in the C++ topic-area.
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).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.