How to whrite Channel into wav file

Started by dima72,

dima72

Hello,
I've created Channel with BASS_StreamCreateURL and FChannel is valid variable.
Now I want to write the Channel into wav file.
var
  Encoder: HENCODE;
  buf: array [0..20000] of BYTE;
begin
  Encoder := BASS_Encode_Start(FChannel, 'output.wav', BASS_ENCODE_PCM, Nil, Nil);
  while (BASS_ChannelIsActive(FChannel) > 0) and (BASS_Encode_IsActive(Encoder) > 0) do
  begin
  BASS_ChannelGetData(FStream, @buf, sizeof(buf));
  end;
but no output.wav is ever created.
Please suggest, how to do it right.

Chris

The encode Stuff is not needed, you can do it directly in the DownloadProc inside the Bass_streamCreateUrl Call.
https://www.un4seen.com/doc/#bass/DOWNLOADPROC.html

dima72

this really answers the question.
writing the buffer into a file directly, in CALLBACK method, works for me and data is in wav format.

Thank you, Chris!



Ian @ un4seen

Just to be sure, is your URL a WAV file? Your DOWNLOADPROC function will receive the downloaded data in its original format, so it wouldn't actually be writing a WAV file unless the download is a WAV file. If it's a different format then using BASS_Encode_Start with BASS_ENCODE_PCM will give you a WAV file, but note you usually also need to include the BASS_UNICODE flag when using Delphi, like this:

  Encoder := BASS_Encode_Start(FChannel, 'output.wav', BASS_ENCODE_PCM {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF}, Nil, Nil);

dima72

Hi Ian,
Encoder := BASS_Encode_Start(FChannel, 'output.wav', BASS_ENCODE_PCM {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF}, Nil, Nil);your comment is really valuable!

So now when I created a channel from URL, having channel handle, it is writing right into a file.
URL is:
'http://stream-dc1.radioparadise.com/rp_192m.ogg', 'http://www.radioparadise.com/m3u/mp3-32.m3u'

I can re-play the wav file and it sounds good. Header of the wav file looks like valid.
 
I have noticed one issue.
If I open this file in WavePad Sound Editor - it plays but the wave chart is not displayed.
However, if I write this stream using DOWNLOADPROC method, without wav header - WavePad Sound Editor shows wave chart.
Any ideas why it happens?

Thanks in advance.


dima72

QuoteIf I open this file in WavePad Sound Editor - it plays but the wave chart is not displayed.

please ignore this comment.
I've eventually discovered 'Zoom track bar' slider in the right bottom corner of the program, which scales chart to visible waves.
It appears that if wav header is present, the program just changing some lay out.

Ian @ un4seen

If the software is showing a wrong length then it may be that the file's WAV header wasn't finalized with the correct length. BASS_Encode_Stop handles that, so check for that being called in your code. Alternatively, you can add the BASS_ENCODE_AUTOFREE flag to the BASS_Encode_Start call, to have it done automatically when the source channel is freed.

dima72

Hi Ian

QuoteBASS_ENCODE_AUTOFREE flag to the BASS_Encode_Start call, to have it done automatically when the source channel is freed.

thank you for this neat suggestion.
This really keeps wav file header according to the standard and chart (in Wave Pad) now is displayed right.