Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Struggling with recorder.getBuffer() - help appreciated  (Read 3708 times)

0 Members and 1 Guest are viewing this topic.

binaryduke

  • Newbie
  • *
  • Posts: 5
    • View Profile
Struggling with recorder.getBuffer() - help appreciated
« 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;
                }
        }
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Struggling with recorder.getBuffer() - help appreciated
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binaryduke

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Struggling with recorder.getBuffer() - help appreciated
« Reply #2 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 :(

binaryduke

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Struggling with recorder.getBuffer() - help appreciated
« Reply #3 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.