Author Topic: How to whrite Channel into wav file  (Read 308 times)

dima72

  • Posts: 5
How to whrite Channel into wav file
« on: 15 Jul '24 - 23:49 »
Hello,
I've created Channel with BASS_StreamCreateURL and FChannel is valid variable.
Now I want to write the Channel into wav file.
Code: [Select]
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

  • Posts: 2217
Re: How to whrite Channel into wav file
« Reply #1 on: 16 Jul '24 - 01:19 »
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

  • Posts: 5
Re: How to whrite Channel into wav file
« Reply #2 on: 16 Jul '24 - 02:00 »
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

  • Administrator
  • Posts: 26177
Re: How to whrite Channel into wav file
« Reply #3 on: 16 Jul '24 - 17:15 »
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:

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

dima72

  • Posts: 5
Re: How to whrite Channel into wav file
« Reply #4 on: 18 Jul '24 - 04:40 »
Hi Ian,
Code: [Select]
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

  • Posts: 5
Re: How to whrite Channel into wav file
« Reply #5 on: 18 Jul '24 - 16:30 »
Quote
If 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

  • Administrator
  • Posts: 26177
Re: How to whrite Channel into wav file
« Reply #6 on: 19 Jul '24 - 12:11 »
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

  • Posts: 5
Re: How to whrite Channel into wav file
« Reply #7 on: 22 Jul '24 - 18:40 »
Hi Ian

Quote
BASS_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.