SFML community forums

Help => Audio => Topic started by: binaryduke on September 08, 2018, 03:29:22 pm

Title: Struggling with recorder.getBuffer() - help appreciated
Post by: binaryduke on September 08, 2018, 03:29:22 pm
I am creating a simple modeless dialog box that deals with the recording and playback of audio and has UI controls for record and playback. In the dialog box's procedure I am trying to manage the SFML audio functions but when I try to access the buffer contents after recording, they appear to be empty. The saved .wav file is consistently only 44kb.

Here's the basics of the audio handling within the procedure. I'd appreciate a steer as to what I'm missing regarding accessing the buffer's contents.

INT_PTR CALLBACK VJWindowProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
        //declare these outside the switch statement handling button messages so they're available to the buttons
        sf::Sound sound;
        sf::SoundBufferRecorder recorder;

        switch (Message)
        {
        case WM_COMMAND:
                switch (LOWORD(wParam))
                {
                case IDC_STOP:  //this is the dialog box's End Recording button
                        recorder.stop();
                        //Now the capture has ended we can get the buffer. Test saving the file and also playing it.

                        const sf::SoundBuffer& buffer = recorder.getBuffer();
                        buffer.saveToFile("TESTRECORDING.wav");
                        sound.setBuffer(buffer);
                        sound.play();
                        break;

                case IDC_RECORD: //this is the dialog box's Start Recording button
                        recorder.start();
                        break;
                }
        }
}
 
Title: Re: Struggling with recorder.getBuffer() - help appreciated
Post by: eXpl0it3r on September 08, 2018, 03:44:22 pm
Your sound recorder and sound instances are local to the call back. As soon as you leave the call back function (which is right after you triggered IDC_RECORD) the instance will stop existing and thus nothing is really recorded.
Title: Re: Struggling with recorder.getBuffer() - help appreciated
Post by: binaryduke on September 08, 2018, 03:52:12 pm
Thanks for the quick response. I thought that may be the case.

When I've tested with creating the sound recorder and sound instances as globals so that they're accessible to the call back, the application hangs at startup for which I can't find any explanation :(
Title: Re: Struggling with recorder.getBuffer() - help appreciated
Post by: binaryduke on September 08, 2018, 04:07:44 pm
I see from this post:
https://github.com/SFML/SFML/issues/970

that globals are to be avoided so will attempt something with pointers to the Sound and SoundBufferRecorder.

I can't see any other way to make this work with a modeless dialog box that holds the audio controls but would appreciate any ideas other than the pointer route.