Page 1 of 1

IAudioHandler - How to save to a playable file

PostPosted: Fri Nov 20, 2020 6:17 pm
by Thawing
In CefSharp there is the AudioHandler, which implements IAudioHandler. I have looked at the example code(https://github.com/cefsharp/CefSharp/blob/76e5a8d5c0541dfe5235c6d43d2b41187ce6ef0f/CefSharp.Example/Handlers/AudioHandler.cs#L23).

How do I take the raw PCM data from OnAudioStreamPacket and save it to a playable file?

Re: IAudioHandler - How to save to a playable file

PostPosted: Mon Nov 23, 2020 3:02 am
by ndesktop
I think you need OnAudioStreamPacket, which seems missing from the AudioHandler.cs (either OnAudioStreamPacket was added after, or the C# implementation ignored it).

Code: Select all
///
  // Called on the audio stream thread when a PCM packet is received for the
  // stream. |data| is an array representing the raw PCM data as a floating
  // point type, i.e. 4-byte value(s). |frames| is the number of frames in the
  // PCM packet. |pts| is the presentation timestamp (in milliseconds since the
  // Unix Epoch) and represents the time at which the decompressed packet should
  // be presented to the user. Based on |frames| and the |channel_layout| value
  // passed to OnAudioStreamStarted you can calculate the size of the |data|
  // array in bytes.
  ///
  /*--cef()--*/
  virtual void OnAudioStreamPacket(CefRefPtr<CefBrowser> browser,
                                   const float** data,
                                   int frames,
                                   int64 pts) = 0;

Re: IAudioHandler - How to save to a playable file

PostPosted: Mon Nov 23, 2020 4:05 am
by amaitland
ndesktop wrote:I think you need OnAudioStreamPacket, which seems missing from the AudioHandler.cs (either OnAudioStreamPacket was added after, or the C# implementation ignored it).


The example linked is from an older branch. See https://github.com/cefsharp/CefSharp/blob/cefsharp/85/CefSharp.Example/Handlers/AudioHandler.cs for the link relevant to 85 release which is the current stable release.

Re: IAudioHandler - How to save to a playable file

PostPosted: Mon Nov 23, 2020 5:53 pm
by Thawing
I am using OnAudioStreamPacket.

So, how do I calculate the array size in bytes? Or actually in float/single?

The comments say:

Code: Select all
  // Based on |frames| and the |channel_layout| value
  // passed to OnAudioStreamStarted you can calculate the size of the |data|
  // array in bytes.


My current values are as follows:

Code: Select all
   // ChannelLayout = LayoutStereo
   // FramesPerBuffer = 1024
   // SampleRate = 44100
   // channels = 2


I am trying to use NAudio to read the samples from the data array into a WAV file.

Code: Select all
        public void OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts)
        {
            // ChannelLayout = LayoutStereo
            // FramesPerBuffer = 1024
            // SampleRate = 44100
            // channels = 2

            var length = noOfFrames / 4;  // <== I am pretty sure I am calculating the length incorrectly.
            var array = new float[length];

            Marshal.Copy(data, array, 0, length);

            this.waveFileWriter.WriteSamples(array, 0, length);
        }


However, the file that is create is too small (10 second recording is only a couple of seconds long) and has no sound.

Re: IAudioHandler - How to save to a playable file

PostPosted: Tue Nov 24, 2020 2:18 am
by ndesktop
This might help.

Re: IAudioHandler - How to save to a playable file

PostPosted: Tue Dec 15, 2020 8:55 am
by Thawing
I have been struggling with this for over a month. Last night, I decided to use "unsafe" instead of using a marshal copy. Instead of the "exception of type 'system.executionengineexception'" error message I have been receiving I got "Attempted to read or write protected memory. This is often an indication that other memory is corrupt". After some research, I found this is a x64 versus x86 issue. I am using Visual Studio (which is still 32 bit). I had the project set to 64 bit.

Set my project to be x86, and I was able to access the memory pointers data. The WAV file I was able to create was the correct length. The audio was the original test audio, but muffled. I still have more work to get the audio sounding perfectly, however I am getting closer.

If you are using Visual Studio to debug and you want to retrieve audio, make sure to use x86.