Audio Wave file to HEX converter

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there,

Does anybody knows any freeware or any other simple way to convert an audio (.wav) file to HEX data ?

I' d like to convert a simple .wav file like c:\WINDOWS\Media\chimes.wav for example.

After this I can store the raw HEX data in a memory in order to playback them using an AVR and a simple analog circuit ?

Does anybody can help me ?

Best regards,

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Michael,

Maybe Bob's work could help you.

https://www.avrfreaks.net/index.p...

Cheers,

Ross

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you sure you want it as .hex directly? Most people would convert it to source or linkable object and add it as a globally accessible symbol to their project.

To convert to source I love xxd.exe that comes as part of the vim.exe editor package. You can get the Windows .zip file for Vim and simply take the xxd.exe from it. Go here:

http://www.vim.org/download.php#pc

Get vim73_46d32.zip (32bit DOS executable) for example. You then use:

c:\>xxd -i sound.wav sound.c

It then creates a source file like:

unsigned char sound_wav[] = {
  0x52, 0x49, 0x46, 0x46, 0xa0, 0x04, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,
  0x66, 0x6d, 0x74, 0x20, 0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00,
  0x22, 0x56, 0x00, 0x00, 0x27, 0x57, 0x00, 0x00, 0x00, 0x04, 0x04, 0x00,
  0x20, 0x00, 0xf4, 0x03, 0x07, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
  0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x40, 0x00, 0xf0, 0x00,
  0x00, 0x00, 0xcc, 0x01, 0x30, 0xff, 0x88, 0x01, 0x18, 0xff, 0x66, 0x61,
  0x63, 0x74, 0x04, 0x00, 0x00, 0x00, 0xa7, 0x02, 0x00, 0x00, 0x64, 0x61,
  0x74, 0x61, 0x00, 0x04, 0x00, 0x00, 0x02, 0x02, 0x28, 0x00, 0x37, 0x00,
  0x89, 0xff, 0x6f, 0x01, 0x3d, 0x01, 0x76, 0x00, 0x4e, 0x64, 0x05, 0xbf,
  0x1c, 0xc2, 0x0b, 0x70, 0x37, 0x80, 0x78, 0xe7, 0xfd, 0x50, 0xd4, 0xeb,
  0x2f, 0x22, 0x71, 0xf4, 0xed, 0xfd, 0x4e, 0x16, 0xf0, 0x0f, 0xd0, 0x4d,
  0x34, 0xc1, 0xfc, 0x20, 0x22, 0xf1, 0xcf, 0x0d, 0x20, 0x42, 0xe4, 0xed,
  0x2e, 0x02, 0x00, 0x21, 0x02, 0x2f, 0x92, 0x08, 0x00, 0xf0, 0x00, 0x00,

  0x34, 0x2e, 0x30, 0x00
};
unsigned int sound_wav_len = 1192;

You add this to your project (probably adding a PROGMEM to it or whatever you compiler uses to located data to flash) and now you can access sound_wav[] in the code and you even get the bonus of having sound_wav_len set to 1192 which you might use to know when all the bytes have been used.

The alternative to this (assuming avr-gcc being used) is to use avr-objcopy to convert binary to linkable object:

C:\>avr-objcopy -I binary -O elf32-avr sound.wav sound.o

C:\>avr-nm sound.o
000004a8 D _binary_sound_wav_end
000004a8 A _binary_sound_wav_size
00000000 D _binary_sound_wav_start

You can then link this with the other .o files and from code refer to _binary_sound_wav_start (and possibly use _binary_sound_waw_size to determine length).

EDIT: just noticed your signature. If you use IAR then the xxd.exe method will work for you (it'll work with ANY C compiler) but you'd need to investigate whether the IAR toolchain has a tool like avr-objcopy that can convert binary to linkable object.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much for your fast replies.

My best regards,

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One thing to bear in mind if you take the xxd route is that it's going to assume that sizeof(int) is 4 so the length added at the end of the file may need changing from "unsigned int" to "unsigned long" for use on an AVR if the WAV is more than 65536 bytes (of course that's going to take a pretty big AVR if it is!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW I just remembered I saw this in IAR the other day. I would guess it looks rather promising:

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ha ha ha ha haaaaaa nice shot !!!

God bless you.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//file pcm2c.c 

#include  
#include  
#include  
#include  

FILE * ifil; 
FILE * ofil; 
char ofilnam[20]; 
char ifilnam[20]; 

//----------------------- 
void main (int argc, char * argv[]){ 
unsigned char b,n; 

  printf("pcm2c  read 8 bit pcm, write to c   June 1 07 Bob G\n"); 
  if(argc != 3){ 
    printf("usage: pcm2c foo.pcm foo.c\n"); 
    exit(1); 
  } 
  strcpy(ifilnam,argv[1]); 
  ifil=fopen(ifilnam,"rb"); 

  strcpy(ofilnam,argv[2]); 
  ofil=fopen(ofilnam,"w"); 

  fprintf(ofil,"const char dat[]={\n"); 
  while(1){ 
    for(n=0; n<16; n++){ 
      fread(&b,1,sizeof(b),ifil); 
      fprintf(ofil,"0x%02x,",b); 
      if(feof(ifil)) break; 
    } 
    fprintf(ofil,"\n"); 
    if(feof(ifil)) break; 
  } 
  fprintf(ofil,"\n};\n"); 
  fclose(ifil); 
  fclose(ofil); 
} 
//------eof------------ 



Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you Bob.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't forget that a .wav also contains a header:
http://mathmatrix.narod.ru/Wavef...

So you might want to have the data only or process it to a certain format before converting to C.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The simplest is to use something like Audacity. Open the file, and then File -> Export -> Options -> RAW (headerless) Unsigned 8-bit PCM. That will give you the raw data, without a header, in unsigned 8 bit bytes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mbeels wrote:
The simplest is to use something like Audacity.
Audacity is a good choice if you would like a GUI and/or only need to do this for a few files. If you want a command-line utility (so you can write a script to process several files), use SoX, http://sox.sourceforge.net/ . It started on Linux but there is a Windows installer available.

Like Audacity, Sox will convert WAV (or MP3 or FLAC or just about anything) into whatever else you want, including a sequence of 8-bit samples, which is probably what you want for the AVR.

I hope this helps!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Clawson, Thank you for vim73_46d32.zip. But the xxx.exe can't install in win 7 64 bit version.. can you please help to convert wav file..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since when has a 64 bit Windows been unable to run 32 bit .exe's ??

 

Anyway there's a clue in the name "vim73_46d32.zip". I'm kind of guessing the site may also have "vim73_46d64.zip".

 

If all else fails install Linux as you get xxd natively there ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you. Vim 64-bit version is also available in the http://www.vim.org/download.php#pc

 

Win64

Native 64-bit binaries for MS-Windows can be found at http://code.google.com/p/vim-win3264/. The Win32 binaries should run too, but the 64 bit version has a few minor advantages (see the web page at the link).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I needed to convert some .jpg and .bmp files to an ascii dump format to include into some C source. A friend found this Unix command:

 

$ hexdump -v -e '16/1 "%4u,"' -e '"\n"' "whatever.jpg" > "whatever.c"

 

Nicely did the trick, you can fiddle with the formatting to fit your particular needs.

 

 

Tom Pappano
Tulsa, Oklahoma

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can u plz elaborate sir like if my wav file is on my pc desktop so what command should i type

anirudh

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Try reading this thread.