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

Author Topic: Trouble with feeding audio stream  (Read 6633 times)

0 Members and 1 Guest are viewing this topic.

farw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trouble with feeding audio stream
« on: July 10, 2009, 02:07:11 pm »
Hello,

I try to write a program that decodes (using mpg123) and plays mp3 data from a network stream. My function for receiving and decoding is the following.
Code: [Select]
bool player::OnGetData(sf::SoundStream::Chunk& ck)

{
    size_t got_now = 0;
    for (unsigned rcb = 0; rcb < in_size; ++rcb)
    {
        char* temp = in + rcb;
        *temp = data_stream->recieve_byte();
    }

    out = new unsigned char [out_size*2];
    int ret = mpg123_decode (mpg, (unsigned char* ) in, in_size,out, out_size*2, &got_now);

    if (ret == MPG123_NEED_MORE)
        throw std::runtime_error ("Decoding error");

    ck.Samples = reinterpret_cast<sf::Int16*> (out);
    ck.NbSamples = got_now;


    return true;
}


It plays some strange noises and the I get an segmentation fault with the following backtrace
Code: [Select]
#0 0x7ff8317fa11b memcpy() (/lib/libc.so.6:??)
#1 0x7ff82dab6c6e ??() (/usr/lib/libopenal.so.1:??)
#2 0x7ff82dab6e26 alBufferData() (/usr/lib/libopenal.so.1:??)
#3 0x7ff8322a0967 sf::SoundStream::FillAndPushBuffer() (/usr/lib/libsfml-audio.so.1.4:??)
#4 0x7ff8322a0b9d sf::SoundStream::Run() (/usr/lib/libsfml-audio.so.1.4:??)
#5 0x7ff8324b5f59 sf::Thread::ThreadFunc() (/usr/lib/libsfml-system.so.1.4:??)
#6 0x7ff83135d3ba start_thread() (/lib/libpthread.so.0:??)
#7 0x7ff83185bfcd clone() (/lib/libc.so.6:??)
#8 ( 0x0000000000000000 in ??() (??:??)


I think that I'm doing something wrong when feeding the audio stream. But since I haven't got any idea what it is, I am asking you for help. My buffer are all unsigned char* arrays and I guess that they are not too small but maybe too big.
If you need more code to help me just write it.

Thanks in advance

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble with feeding audio stream
« Reply #1 on: July 10, 2009, 02:26:47 pm »
The format required by SFML is 16-bits signed integers. If you have an array of unsigned char it's probably different.

And you're never deleting the memory allocated to out.
Laurent Gomila - SFML developer

farw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trouble with feeding audio stream
« Reply #2 on: July 10, 2009, 02:53:41 pm »
Quote from: "Laurent"
The format required by SFML is 16-bits signed integers. If you have an array of unsigned char it's probably different.


Well, mpg123 needs the unsigned char*s, as far as I know. Shouldn't my cast fix that problem?

Quote
And you're never deleting the memory allocated to out.

That's right, I am sorry.   :wink:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble with feeding audio stream
« Reply #3 on: July 10, 2009, 03:08:12 pm »
Quote
Well, mpg123 needs the unsigned char*s, as far as I know. Shouldn't my cast fix that problem?

No, you're just making the compiler happy but nothing is actually converted.

You should use the mpg123_format function with the MPG123_ENC_SIGNED_16 flag to setup mpg123 to output 16-bits signed integers.

Your SFML stream also needs to be Initialized with the same values that you pass to mpg123_format for channels count and samples rate.
Laurent Gomila - SFML developer

farw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trouble with feeding audio stream
« Reply #4 on: July 10, 2009, 09:02:10 pm »
Quote from: "Laurent"
You should use the mpg123_format function with the MPG123_ENC_SIGNED_16 flag to setup mpg123 to output 16-bits signed integers.

Your SFML stream also needs to be Initialized with the same values that you pass to mpg123_format for channels count and samples rate.

I do both inside the constructor. I'm sorry that I didn't post it earlier. Here is the complete code of the class as well as its header. Which type of data would you suggest to use for the buffer? Would an array of sf::Int16* do better?

Thanks for your help

Edit: I am using a 64Bit system, but that shouldn't make any difference, should it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble with feeding audio stream
« Reply #5 on: July 10, 2009, 10:58:09 pm »
Quote
Which type of data would you suggest to use for the buffer? Would an array of sf::Int16* do better?

Absolutely, as both SFML and mpg123 (with the MPG123_ENC_SIGNED_16 flag) will work with 16-bits signed integers.
Laurent Gomila - SFML developer

farw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trouble with feeding audio stream
« Reply #6 on: July 11, 2009, 03:12:33 pm »
Welll, I changed it to sf::Int16* but now I have to cast it in mpg123_decode like I do in line
Code: [Select]
mpg123_decode (mpg, (unsigned char* ) in, in_size, (unsigned char*) out, out_size*2, &got_now); since mpg123_decode doesn't accept any other type than unsigned char*.
Nevertheless, the SegFault is gone but it still sounds very crappy because it stutters.

Thanks for your help

Btw: Shouldn't I change the size of the output buffer when I switch the data type?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble with feeding audio stream
« Reply #7 on: July 11, 2009, 03:59:02 pm »
Quote
elll, I changed it to sf::Int16* but now I have to cast it in mpg123_decode

This one is ok (you can even use reinterpret_cast here ;)). The function can technically only take one type of argument but the true data type must be chosen according to the format flag and cast when passed to the function -- just like you do.

Quote
Nevertheless, the SegFault is gone but it still sounds very crappy because it stutters.

Now it probably has to do with the network part. Did you try to feed the stream from a file which is already in memory, to remove all the possible network issues?
Laurent Gomila - SFML developer

farw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Trouble with feeding audio stream
« Reply #8 on: July 11, 2009, 05:38:33 pm »
It works now. I had to divide the number of NbSamples by the size of sf::Int16. Now it looks like that
Code: [Select]
bool player::OnGetData(sf::SoundStream::Chunk& ck)
{
    for (unsigned got = 0; got < in_size; ++got)
    {
        char* temp = in + got; // inkrement the pointer
        *temp = data_stream->recieve_byte(); // get a byte of the audio stream
    }

    size_t decoded_bytes;
    mpg123_decode (mpg, (unsigned char* ) in, in_size, reinterpret_cast<unsigned char*> (out), out_size*4, &decoded_bytes);

    ck.Samples = out;
    ck.NbSamples = decoded_bytes/sizeof(sf::Int16);

    return true;
}


Thanks for your help!  :D

 

anything