0

I don't understand how I can play sound with OpenAL library. I write program for VoIP. I'm getting every 10ms sound buffer.

size_t len = socket.read_some(boost::asio::buffer(buf),error);

buf is

boost::array <char, 441> buf;

And that I need to do with this buffer? I was reading examples, but didn't understand :(. Please help me. P.S. I use C++ on Visual Studio 2010. After editing

    alGenSources(1, &alSource);
    alGenBuffers(1, &alSampleSet);
    alBufferData(alSampleSet, AL_FORMAT_MONO16, buf.data(), sizeof(buf.data()), 44100);
    alSourcei(alSource, AL_BUFFER, alSampleSet);
    //
    alSourcei(alSource, AL_LOOPING, alSampleSet);
    alSourcePlay(alSource);
Alexander Mashin
  • 693
  • 3
  • 14
  • 34

2 Answers2

0

buf.data() returns a pointer, its size is probably either 32 or 64 bits.

The size of the array is buf.size() instead; and the size requested by alBufferData is the number of bytes in the buffer, that is the number of elements in the array buf.size() multiplied by the size of each element sizeof(buf.front()) or sizeof(char).
[Note sizeof(buf) may probably work, at least with current Boost implementation, but I do not think that is guaranteed]

You are specifying an AL_FORMAT_MONO16 channel format, but you are using an odd-sized 8 bytes buffer. So that's probably an error in either the declaration of the array or the specification of the format. Based on the rest of your question, I'd assume you meant AL_FORMAT_MONO8 there instead.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • ok. I use buf.size(). But i don't have sound. At first I thought that I get wrong buffer, but if i write buffer to file, in this I have sound. – Alexander Mashin Jan 31 '13 at 17:31
  • @EXTRAM: Have you been able to play **any** sound before? I.e., by trying a sample? The error(s) could be anywhere in your code – K-ballo Jan 31 '13 at 17:33
0

you have to keep the sound alive, try out this function.

void keepItPlay(unsigned int * source)
{
    ALint state;
    alGetSourcei(source, AL_SOURCE_STATE, &state);
    if(state == AL_STOPPED) CleanUp(source);
}
greenfox
  • 208
  • 1
  • 9