18 May '13 - 10:20 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: How to convert MP3 file to WAV with new sample rate?  (Read 1459 times)
PJMonty
Posts: 2


« on: 24 Jan '11 - 20:48 »
Reply with quoteQuote

I have searched the forum but haven't yet what I'm looking for, so Im posting this message.  I'm trying to take an MP3 file and convert it to a WAV at a specific sample rate, regardless of the original sample rate.  I found code to convert from MP3 to WAV, and it works fine.  However, when I tried to add in a mixer to do sample rate conversion, I end up with a 1K file that is basically empty.

Here's my code.  I'm writing in Delphi, but if you know the solution in C,C++, or VB, that's fine.  I can translate it without any problems.  I've stripped out the error handling to make it cleaner to read:

procedure TMainFRM.ConvertMp3ToWav(ThisMp3, ToThisWav : string);
var
  Decoder, Mixer : HSTREAM;
  Buf : array[0..20000] of byte;

begin

// Create a decoding channel from the source file
Decoder := BASS_StreamCreateFile(FALSE, pchar(ThisMp3), 0, 0, BASS_STREAM_DECODE);

// Create a mixer to change the sample rate to 32 kHz
Mixer := BASS_Mixer_StreamCreate(32000, 1, BASS_STREAM_DECODE);

// Add the decoder to the mixer  as a channel
BASS_Mixer_StreamAddChannel(Mixer, Decoder, BASS_MIXER_DOWNMIX);

// Set a WAV writer on it
BASS_Encode_Start(Mixer, pchar(ToThisWav), BASS_ENCODE_PCM or BASS_ENCODE_AUTOFREE, NIL, NIL);
while (BASS_ACTIVE_PLAYING = BASS_ChannelIsActive(decoder)) do
  begin
  BASS_ChannelGetData(decoder, @Buf, sizeof(buf)); // Process some data (decode and write)
  end;

// Free the decoder (and encoder due to AUTOFREE)
BASS_StreamFree(decoder);

// Free the mixer
if Mixer <> 0 then
  BASS_StreamFree(Mixer);

end;

I would appreciate any help in getting this to work.

Thanks,
PeterM
« Last Edit: 24 Jan '11 - 20:55 by PJMonty » Logged
Wishmaster
Posts: 124


« Reply #1 on: 24 Jan '11 - 22:37 »
Reply with quoteQuote

BASS_ChannelGetData(Mixer , @Buf, sizeof(buf));
Logged
PJMonty
Posts: 2


« Reply #2 on: 25 Jan '11 - 00:53 »
Reply with quoteQuote

Aaaaarrgh!

Classic.  Many thanks for pointing out the (now) obvious mistake.  It works perfectly.

Thanks,
PeterM
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines