Re: [LAD] mixing while using libao and libsndfile

2016-05-17 Thread Andrea Del Signore
On Tue, 17 May 2016 08:26:17 +, David Griffith wrote:

> On Tue, 17 May 2016, Andrea Del Signore wrote:
> 
>> On Tue, May 17, 2016 at 12:25 AM, David Griffith
>>  wrote:
>> On Mon, 16 May 2016, Andrea Del Signore wrote:
>> 
>> > I'm not simply trying to mix two files.  My main project is a game
>> > engine in which two sounds are allowed at any one time. For instance,
>> > there can be constant background music punctuated by sound effects. 
>> > I can't get these to mix correctly.
>> 
>> Hi,
>> 
>> in that case you can just skip the right number of frames before
>> starting playing sounds.
>> 
>> I modified my code to take the start time for each file and schedule
>> the play time with frame accuracy.
>> 
>> http://pastebin.com/0PMyfPvK
>> 
>> If you want your timing to be sample accurate the algorithm is a bit
>> more complex.
> 
> That won't work.  Your code schedules things ahead of time before
> anything else happens.  I need to be able to fire off sound effects the
> instant the player does something to cause them.  I can't know in
> advance.

Hi,

another code iteraction: http://pastebin.com/v8gTWBtJ

This is a basic implementation, there is a lot of things left todo (RT 
safeness as Paul already noted, code cleanup, etc...)

HTH,
Andrea

P.S.
I don't code in C very often, may be this is my first time in 6 or 7 
years. For me this code snippet is just an excuse to do some exercise :)


___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-17 Thread Paul Davis
Your design is way too simple and fundamentally wrong.

If you want low latency you need to use a pull model (aka callback model)
for audio i/o to the device. Let the device tell you when it wants audio
data, and deliver it,on time, without blocking (which means no on-demand
file i/o in the same thread as the device audio i/o). See
http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing

On Tue, May 17, 2016 at 4:26 AM, David Griffith  wrote:

> On Tue, 17 May 2016, Andrea Del Signore wrote:
>
> On Tue, May 17, 2016 at 12:25 AM, David Griffith  wrote:
>> On Mon, 16 May 2016, Andrea Del Signore wrote:
>>
>> > I'm not simply trying to mix two files.  My main project is a
>> > game engine in which two sounds are allowed at any one time.
>> > For instance, there can be constant background music punctuated
>> > by sound effects.  I can't get these to mix correctly.
>>
>> Hi,
>>
>> in that case you can just skip the right number of frames before starting
>> playing sounds.
>>
>> I modified my code to take the start time for each file and schedule the
>> play time with frame accuracy.
>>
>> http://pastebin.com/0PMyfPvK
>>
>> If you want your timing to be sample accurate the algorithm is a bit more
>> complex.
>>
>
> That won't work.  Your code schedules things ahead of time before anything
> else happens.  I need to be able to fire off sound effects the instant the
> player does something to cause them.  I can't know in advance.
>
> --
> David Griffith
> d...@661.org
> ___
> Linux-audio-dev mailing list
> Linux-audio-dev@lists.linuxaudio.org
> http://lists.linuxaudio.org/listinfo/linux-audio-dev
>
>
___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-17 Thread Andrea Del Signore
On Tue, May 17, 2016 at 10:26 AM, David Griffith  wrote:

> On Tue, 17 May 2016, Andrea Del Signore wrote:
>
> On Tue, May 17, 2016 at 12:25 AM, David Griffith  wrote:
>> On Mon, 16 May 2016, Andrea Del Signore wrote:
>>
>> > I'm not simply trying to mix two files.  My main project is a
>> > game engine in which two sounds are allowed at any one time.
>> > For instance, there can be constant background music punctuated
>> > by sound effects.  I can't get these to mix correctly.
>>
>> Hi,
>>
>> in that case you can just skip the right number of frames before starting
>> playing sounds.
>>
>> I modified my code to take the start time for each file and schedule the
>> play time with frame accuracy.
>>
>> http://pastebin.com/0PMyfPvK
>>
>> If you want your timing to be sample accurate the algorithm is a bit more
>> complex.
>>
>
> That won't work.  Your code schedules things ahead of time before anything
> else happens.  I need to be able to fire off sound effects the instant the
> player does something to cause them.  I can't know in advance.
>
> --
> David Griffith
> d...@661.org



Yes my code for now implements only your initial request (play 2 sounds
spaced by few seconds), and it shows how to schedule N sound files of
different length at different times.

I'll try to write a simple mixer for my next learning exercise :)

Have a good day,
Andrea
___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-17 Thread David Griffith

On Tue, 17 May 2016, Andrea Del Signore wrote:


On Tue, May 17, 2016 at 12:25 AM, David Griffith  wrote:
On Mon, 16 May 2016, Andrea Del Signore wrote:

> I'm not simply trying to mix two files.  My main project is a
> game engine in which two sounds are allowed at any one time. 
> For instance, there can be constant background music punctuated
> by sound effects.  I can't get these to mix correctly.

Hi,

in that case you can just skip the right number of frames before starting
playing sounds.

I modified my code to take the start time for each file and schedule the
play time with frame accuracy.

http://pastebin.com/0PMyfPvK

If you want your timing to be sample accurate the algorithm is a bit more
complex.


That won't work.  Your code schedules things ahead of time before anything 
else happens.  I need to be able to fire off sound effects the instant the 
player does something to cause them.  I can't know in advance.


--
David Griffith
d...@661.org___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-16 Thread Andrea Del Signore
On Mon, 16 May 2016 17:50:24 -0700, David Griffith wrote:

> On May 16, 2016 3:25:48 PM PDT, David Griffith
>  wrote:
> 
>>  Earlier you set up filebuffer like this:
>>
>>buflen = BUFFSIZE * sf_info[0].channels;
>>filebuffer = malloc(buflen * sizeof(float));
>>
>>The size of filebuffer is BUFFSIZE float-sized frames.  Therefore when
>>you specify BUFFSIZE as the number of floats to read, they all fit in
>>filebuffer.
> 
> Sorry.  I meant BUFFSIZE as the number of /frames/.

Yes, but if I understand correctly when using sf_read_* functions, 
sndfile expect the number of items as the third parameters and NOT the 
number of frames.

As per FAQ a frame is 1 item per channel, so I expect BUFFSIZE * channels 
to be the correct value to pass, but it doesn't work!?

Ciao,
Andrea

___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-16 Thread Andrea Del Signore
On Tue, May 17, 2016 at 12:25 AM, David Griffith  wrote:

> On Mon, 16 May 2016, Andrea Del Signore wrote:
>
> > I've been knocking my head against a wall for more than a year trying to
>> > figure out how to correctly mix two streams of audio while using
>> > libsndfile for input and libao for output.  My main requirement is that
>> > I cannot assume anything about the output drivers -- that is, I cannot
>> > depend on the output driver (ALSA, OSS, Sun, etc) being able to do the
>> > mixing for me.  Many of my target platforms lack any sort of mixing
>> > services.  I need to do this myself.  I tried starting a mixer/player
>> > thread that would work in a producer/consumer relationship with one or
>> > two audio file decoder threads.  I can play one sound at a time just
>> > fine. When I try to do both, I get distortion followed by a segfault.
>>
>> Hi,
>>
>> not sure if I understood correctly: do you just want to mix N files?
>>
>> Like you I'm learning libsndfile and libao so this is my attempt to mix
>> some audio files:
>>
>> http://pastebin.com/dm7z8b3Z
>>
>> HTH,
>> Andrea
>>
>> P.S.
>> Can someone explain line 88 (I already read the sndfile FAQ)?
>>
>
> I'm not simply trying to mix two files.  My main project is a game engine
> in which two sounds are allowed at any one time.  For instance, there can
> be constant background music punctuated by sound effects.  I can't get
> these to mix correctly.
>
>
Hi,

in that case you can just skip the right number of frames before starting
playing sounds.

I modified my code to take the start time for each file and schedule the
play time with frame accuracy.

http://pastebin.com/0PMyfPvK

If you want your timing to be sample accurate the algorithm is a bit more
complex.

Ciao,
Andrea
___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-16 Thread David Griffith
On May 16, 2016 3:25:48 PM PDT, David Griffith  wrote:

>  Earlier you set up filebuffer like this:
>
>buflen = BUFFSIZE * sf_info[0].channels;
>filebuffer = malloc(buflen * sizeof(float));
>
>The size of filebuffer is BUFFSIZE float-sized frames.  Therefore when
>you 
>specify BUFFSIZE as the number of floats to read, they all fit in 
>filebuffer.

Sorry.  I meant BUFFSIZE as the number of /frames/.
-- 
David Griffith
d...@661.org
___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-16 Thread David Griffith

On Mon, 16 May 2016, Andrea Del Signore wrote:


> I've been knocking my head against a wall for more than a year trying to
> figure out how to correctly mix two streams of audio while using
> libsndfile for input and libao for output.  My main requirement is that
> I cannot assume anything about the output drivers -- that is, I cannot
> depend on the output driver (ALSA, OSS, Sun, etc) being able to do the
> mixing for me.  Many of my target platforms lack any sort of mixing
> services.  I need to do this myself.  I tried starting a mixer/player
> thread that would work in a producer/consumer relationship with one or
> two audio file decoder threads.  I can play one sound at a time just
> fine. When I try to do both, I get distortion followed by a segfault.

Hi,

not sure if I understood correctly: do you just want to mix N files?

Like you I'm learning libsndfile and libao so this is my attempt to mix some 
audio files:

http://pastebin.com/dm7z8b3Z

HTH,
Andrea

P.S.
Can someone explain line 88 (I already read the sndfile FAQ)?


I'm not simply trying to mix two files.  My main project is a game engine 
in which two sounds are allowed at any one time.  For instance, there can 
be constant background music punctuated by sound effects.  I can't get 
these to mix correctly.


Regarding your line 88, I had trouble with this too:

sf_count_t item_read = sf_read_float (sndfile[i], filebuffer, BUFFSIZE); 
// WHY BUFFSIZE? Shouldn't be BUFFSIZE * channels?


The third parameter is for the number of items or frames.  A frame is made 
up of one sample per channel.  Earlier you set up filebuffer like this:


buflen = BUFFSIZE * sf_info[0].channels;
filebuffer = malloc(buflen * sizeof(float));

The size of filebuffer is BUFFSIZE float-sized frames.  Therefore when you 
specify BUFFSIZE as the number of floats to read, they all fit in 
filebuffer.



--
David Griffith
d...@661.org___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


Re: [LAD] mixing while using libao and libsndfile

2016-05-16 Thread Andrea Del Signore
> I've been knocking my head against a wall for more than a year trying to
> figure out how to correctly mix two streams of audio while using
> libsndfile for input and libao for output.  My main requirement is that
> I cannot assume anything about the output drivers -- that is, I cannot
> depend on the output driver (ALSA, OSS, Sun, etc) being able to do the
> mixing for me.  Many of my target platforms lack any sort of mixing
> services.  I need to do this myself.  I tried starting a mixer/player
> thread that would work in a producer/consumer relationship with one or
> two audio file decoder threads.  I can play one sound at a time just
> fine. When I try to do both, I get distortion followed by a segfault.

Hi,

not sure if I understood correctly: do you just want to mix N files?

Like you I'm learning libsndfile and libao so this is my attempt to mix
some audio files:

http://pastebin.com/dm7z8b3Z

HTH,
Andrea

P.S.
Can someone explain line 88 (I already read the sndfile FAQ)?

On Mon, May 16, 2016 at 11:30 PM, <
linux-audio-dev-ow...@lists.linuxaudio.org> wrote:

> You are not allowed to post to this mailing list, and your message has
> been automatically rejected.  If you think that your messages are
> being rejected in error, contact the mailing list owner at
> linux-audio-dev-ow...@lists.linuxaudio.org.
>
>
>
> -- Forwarded message --
> From: Andrea Del Signore <seje...@gmail.com>
> To: linux-audio-dev@lists.linuxaudio.org
> Cc:
> Date: Mon, 16 May 2016 21:26:40 + (UTC)
> Subject: Re: [LAD] mixing while using libao and libsndfile
> On Sun, 15 May 2016 16:34:34 +, David Griffith wrote:
>
> > I've been knocking my head against a wall for more than a year trying to
> > figure out how to correctly mix two streams of audio while using
> > libsndfile for input and libao for output.  My main requirement is that
> > I cannot assume anything about the output drivers -- that is, I cannot
> > depend on the output driver (ALSA, OSS, Sun, etc) being able to do the
> > mixing for me.  Many of my target platforms lack any sort of mixing
> > services.  I need to do this myself.  I tried starting a mixer/player
> > thread that would work in a producer/consumer relationship with one or
> > two audio file decoder threads.  I can play one sound at a time just
> > fine. When I try to do both, I get distortion followed by a segfault.
> >
> > So, I'm back to a demo program.  What must I do to this program to cause
> > it to start playing one audio file, then play another N seconds later?
> >
> > David Griffith d...@661.org
> >
>
> Hi,
>
> not sure if I understood correctly: do you just want to mix N files?
>
> I'm a noob with both libsndfile and libao :)
>
> Here my code: http://pastebin.com/dm7z8b3Z
>
> HTH,
> Andrea
>
> P.S.
> Can someone explain line 88 (I already read the sndfile FAQ)?
>
>
>
___
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev