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