SDL Forum Index
SDL
Simple DirectMedia Layer Forums
Reply to topic
Basic sound dev help
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
A few questions:

The callback is called like this:
Code:
void my_audio_callback(void *userdata, Uint8 *stream, int len)


(1) I copy to stream, does SDL copy stream to audio device immediately? Or is it a pointer-based operation (a pointer is given to the sound card / device, which reads from that stream)? And if it's pointer-based, can I alter the len bytes in stream before the next callback, thereby altering the sound on-the-fly?

(2) Is there a way to get SDL to use a pointer I designate for sound input (such as pointing to my already existing data buffer), rather than copying data from wherever I have it into SDL's buffer?

(3) If I use a setup with "desired.channels=2", how do I populate both channels of sound? The callback doesn't seem to indicate channels.

(4) What if I want to specify that certain channels explicitly output to right / left (/other)?

Thank you in advance.

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
(3) If I use a setup with "desired.channels=2", how do I populate both channels of sound? The callback doesn't seem to indicate channels.

Generating my own sound files, and then using Audacity's raw import feature, I was able to figure out the answer to this one. It is apparently that every sample in the stream is actually a sample set, as in a group of channel data points for that sound sample, as in:

...[channel1][channel2][channelN]...[channel1][channel2][channelN]...

So for two-channel, 16-bit sound in SDL, this equates to a sound encoding schema of both 16-bit values per sample in bundles:

...[Sint16:1][Sint16:2]...[Sint16:1][Sint16:2]...

One down. Smile

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
Here's the test file I'm developing. If anybody has any pointers, I would appreciate hearing them. Thank you. Smile

Generates cosine wave tones for "Mary Had a Little Lamb":

http://libsf.org/sdl_mary.cpp

-----
Note: Written for Windows and Visual Studio 2008.
Note: It writes a file to c:\temp\song.raw that can be loaded into Audacity for examination. Use File--Import--Raw Data--16-bit signed, Little Endian, One Channel.

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
Linux version (compiles in G++):

http://libsf.org/sdl_mary_linux.cpp

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
My wife gave me one pointer. She said that my "rendition" of Mary Had a Little Lamb was missing a note where the word "Its" should go in "Its fleece was white as snow." I just used a pause and told her it was "artistic license." Smile Truth be told, I'm not sure I knew there was a played note there, just sung. LOL!

Any other pointers from anybody? I seem to have this code working properly in my project, a new virtual machine like Java or .NET (still under development).

Here's the current final C++ implementation I've coded (I allow up to four tones to be combined per single logical tone sound, along with sound inputs from any source. Search for "iisound_generateTones" to find the function body, around line 262 as of the time of this writing):

https://github.com/RickCHodgin/libsf/blob/master/vvm/core/plugins/sound/sound.cpp

Internally, I use 32-bit floating point computations for all sound mixing. This relates to values always in the range -1.0 to 1.0. These are then copied into SDL's stream based on the obtained format, which for now I have hard-coded as Sint16, but later will honor properly. Smile

-----
Does anybody have any advice on making non-sine-wave tones easily, something to produce a better sound? I have the idea of sawtooth waves, and square waves as easy alternatives, but I know there must be well-established algorithms for better synthesized sounds.

-----
Thank you SDL developers for creating this product!

One bit of advice I would offer for a documentation update: A deadlock condition occurs if the SDL_OpenAudio() function is called from DllMain()'s DLL_PROCESS_ATTACH operation. It took me a while to figure that one out. I had to move the initialization operation into its own function called later in the startup to bypass that deadlock. I didn't see that mentioned in the documentation. Maybe I missed it.

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
Basic sound dev help
KHMan
Guest

Reply with quote
On 6/2/2013 8:30 PM, RickCHodgin wrote:
Quote:
[snip]
Does anybody have any advice on making non-sine-wave tones easily,
something to produce a better sound? I have the idea of sawtooth
waves, and square waves as easy alternatives, but I know there
must be well-established algorithms for better synthesized sounds.

Csound has just about everything.

Or go Audiality for real time.

Or better the classic route -- just find one of those MOS6581
emulators... Smile

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Basic sound dev help
Ryan C. Gordon
Guest

Reply with quote
Quote:
(1) I copy to stream, does SDL copy stream to audio device immediately?

Depends on a lot of factors (platform, driver, etc), but you should
assume the pointer is invalid as soon as the callback returns, and
whatever you provided during the callback is on its way to the sound
hardware and can't be altered or stopped.

Quote:
(2) Is there a way to get SDL to use a pointer I designate for sound
input (such as pointing to my already existing data buffer), rather than
copying data from wherever I have it into SDL's buffer?

No. We considered a DMA-style interface at one point, but decided it
wasn't worth the complexity for a small optimization win.

Quote:
(3) If I use a setup with "desired.channels=2", how do I populate both
channels of sound? The callback doesn't seem to indicate channels.

Interleaved: left channel's sample, then right channel's sample, then
left again, etc.

Quote:
(4) What if I want to specify that certain channels explicitly output to
right / left (/other)?

Make sure you write them left-right-left-right-etc. We don't let you
arbitrarily map channels at this time.

I talked about stereo playback, but 5.1 output, etc, follows similar rules.

--ryan.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
Thanks, Ryan. All great information.

---------
BTW, it occurred to me. Your initials are RCG. Mine are RCH. So... RCH - 1 = RCG Smile The thoughts that just pop into my head sometimes! LOL. Smile

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
Re: Basic sound dev help
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
KHMan wrote:
Csound has just about everything.
Or go Audiality for real time.
Or better the classic route -- just find one of those MOS6581
emulators... Smile


KHMan,

I haven't had a chance to look at these yet. I have been (1) busy with work and (2) I still have some more framework code to lay down in my app before I get to the point of needing this ability.

I had hoped to get be on this part by now, but things are moving more slowly than I expected. Still, I wanted to let you know I appreciate your response. Thank you for your assistance. Smile

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
RickCHodgin


Joined: 31 May 2013
Posts: 8
Reply with quote
RickCHodgin wrote:
Generates cosine wave tones for "Mary Had a Little Lamb":
http://libsf.org/sdl_mary.cpp
http://libsf.org/sdl_mary_linux.cpp


These urls have been moved. The forum rules did not allow me to edit the prior message to reflect this change. Please forgive the new message.

Visual Studio:
http://libsf.org/software/misc/sdl_mary.cpp

GNU C++ Compiler:
http://libsf.org/software/misc/sdl_mary_linux.cpp

_________________
Best regards,
Rick C. Hodgin
-----
www.libsf.org
www.visual-freepro.org
View user's profileSend private messageVisit poster's website
Basic sound dev help
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic