SFML community forums

Help => Audio => Topic started by: airsniper on August 07, 2012, 12:12:23 pm

Title: Recording stereo sound
Post by: airsniper on August 07, 2012, 12:12:23 pm
I have a problem with the recording of stereo sound. 
 
It is  code:

#include <SFML/Audio.hpp>
#include <iomanip>
#include <iostream>
int main( ){    
    if (sf::SoundRecorder::CanCapture() == false)
    {
        std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
        return EXIT_SUCCESS;
    }  
    unsigned int SampleRate=48000;
    std::cout << "Press enter to start recording audio";
    std::cin.ignore(10000, '\n');
    sf::SoundBufferRecorder Recorder;    
    Recorder.Start(SampleRate);
    std::cout << "Recording... press enter to stop";
    std::cin.ignore(10000, '\n');
    Recorder.Stop();

    // Get the buffer containing the captured data
    const sf::SoundBuffer& Buffer = Recorder.GetBuffer();

   const short  int*hbuf;
   hbuf=Buffer.GetSamples () ;
 
   //Display 10 samples of captured sound
 
   int i;
   for (i=0; i<10;i++) std::cout << *(hbuf+i)<<"\n";

    // Display captured sound informations
    std::cout << "Sound information :" << std::endl;
    std::cout << " " << Buffer.GetDuration()      << " seconds"           << std::endl;
    std::cout << " " << Buffer.GetSampleRate()    << " samples / seconds" << std::endl;
    std::cout << " " << Buffer.GetChannelsCount() << " channels"          << std::endl;

    return EXIT_SUCCESS;
}

It works for:
CaptureDevice = alcCaptureOpenDevice (NULL, SampleRate, AL_FORMAT_MONO16, SampleRate)
 in SoundBufferRecorder

I have modified:
CaptureDevice = alcCaptureOpenDevice (NULL, SampleRate, AL_FORMAT_STEREO16, SampleRate)  in SoundBufferRecorder  and compiled SFML-audio,    but it does not work.
I think, I should do something else for stereo recording. I would be grateful for your help.
Title: Re: Recording stereo sound
Post by: Laurent on August 07, 2012, 12:26:04 pm
What does "doesn't work" mean?
Title: Re: Recording stereo sound
Post by: airsniper on August 07, 2012, 01:00:29 pm
Process returned -1073741819 (0xC00000005)    execution time : 2.578 
 
for example.
Title: Re: Recording stereo sound
Post by: Laurent on August 07, 2012, 01:22:20 pm
Then use the debugger and get more details about the crash, like the call stack for example.
Title: Re: Recording stereo sound
Post by: airsniper on August 07, 2012, 02:48:36 pm
More details:

Building to ensure sources are up-to-date
Build succeeded
Selecting target:
Debug
Adding source dir: C:\Documents and Settings\p79180\Pulpit\SMFL\Code\
Adding source dir: C:\Documents and Settings\p79180\Pulpit\SMFL\Code\
Adding file: bin\Debug\Code.exe
Starting debugger:
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb 6.8
Child process PID: 500
Program received signal SIGSEGV, Segmentation fault.
In ntdll!RtlStartRXact () (C:\WINDOWS\system32\ntdll.dll)

In window Call Stack:

#0 7C91839E     ntdll!RtlStartRXact() (C:\WINDOWS\system32\ntdll.dll:??)
#1 00000000     0x00000040 in ??() (??:??)
#2 7C96FB98     ntdll!RtlpNtMakeTemporaryKey() (C:\WINDOWS\system32\ntdll.dll:??)
#3 7C94B244     ntdll!LdrFindEntryForAddress() (C:\WINDOWS\system32\ntdll.dll:??)
#4 00000000     0x00ab0000 in ??() (??:??)
#5 00000000     0x50000161 in ??() (??:??)
#6 00000000     0x00000040 in ??() (??:??)
#7 7C919C0C     ntdll!RtlpUnWaitCriticalSection() (C:\WINDOWS\system32\ntdll.dll:??)
#8 77C1C3C9     msvcrt!free() (C:\WINDOWS\system32\msvcrt.dll:??)
#9 00000000     0x00ab0000 in ??() (??:??)
#10 00000000    0x40000060 in ??() (??:??)
#11 00000000    0x00000040 in ??() (??:??)
#12 77C1C3E7    msvcrt!free() (C:\WINDOWS\system32\msvcrt.dll:??)
#13 00000000    0x00000040 in ??() (??:??)
#14 77C1C42E    msvcrt!malloc() (C:\WINDOWS\system32\msvcrt.dll:??)
#15 70B59649    operator new() (C:\Documents and Settings\p79180\Pulpit\SMFL\Code\sfml-audio.dll:??)
#16 70BB80FC    std::vector<short, std::allocator<short> >::_M_insert_aux() (C:\Documents and Settings\p79180\Pulpit\SMFL\Code\sfml-audio.dll:??)
#17 70B45BE4    sf::SoundBufferRecorder::OnProcessSamples() (C:\Documents and Settings\p79180\Pulpit\SMFL\Code\sfml-audio.dll:??)
#18 70B47FBB    sf::SoundRecorder::ProcessCapturedSamples() (C:\Documents and Settings\p79180\Pulpit\SMFL\Code\sfml-audio.dll:??)
#19 70B48044    sf::SoundRecorder::Run() (C:\Documents and Settings\p79180\Pulpit\SMFL\Code\sfml-audio.dll:??)
#20 663034B1    sf::Thread::ThreadFunc() (C:\Documents and Settings\p79180\Pulpit\SMFL\Code\sfml-system.dll:??)
#21 77C2A3B0    msvcrt!_endthreadex() (C:\WINDOWS\system32\msvcrt.dll:??)
#22 7C80B729    KERNEL32!GetModuleFileNameA() (C:\WINDOWS\system32\kernel32.dll:??)
#23 00000000    0x00000000 in ??() (??:??)

But i don't know what must  I to do
Title: Re: Recording stereo sound
Post by: Laurent on August 07, 2012, 02:58:51 pm
Thanks :)

Is it the only thing that fails? Have you tried other (unchanged) classes/functions of SFML, to make sure that you recompiled it correctly?
Title: Re: Recording stereo sound
Post by: binary1248 on August 07, 2012, 03:32:40 pm
With the callstack it was easy to find the problem. When you change the format to record 2 channels (stereo) you mustn't forget to resize SFML's buffer accordingly. 2 channels need double the space of 1 channel and also need a bit more code to retrieve the data of each channel individually. For more info look here (http://opensource.creative.com/pipermail/openal/2011-March/012357.html).
Title: Re: Recording stereo sound
Post by: Laurent on August 07, 2012, 04:00:17 pm
Quote
When you change the format to record 2 channels (stereo) you mustn't forget to resize SFML's buffer accordingly.
In fact it could be corrected now, even if SFML only supports mono. That would leave less potential errors for people who modify SFML, and for me the day I implement this feature.

The line to update (https://github.com/SFML/SFML/blob/sfml1/src/SFML/Audio/SoundRecorder.cpp#L198) is:
mySamples.resize(SamplesAvailable);
(SFML 1.6)

Corrected version :
mySamples.resize(SamplesAvailable * ChannelCount);
(ChannelCount has to be defined accordingly -- 1 for mono, 2 for stereo)

The second argument of OnProcessSamples may have to be modified too, sorry I don't have the time to have a closer look at it now.
Title: Re: Recording stereo sound
Post by: airsniper on August 08, 2012, 09:06:22 am
Thanks for the help. All working. :D