Question about sound Fx length and volume

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Question about sound Fx length and volume

Post by Newbie »

Hi everybody,

I want to use a large sound effect as music background on a screen i am coding.

I want to use this sound as a background music in fact because i have no more CPU time to decompress a steam neither MP3 nor ADX.

The screen could not be speed up because the code make a lot of interesting and complex things.

So i came to sfxmgr in Kos !

First Dan potter says :

This file contains declarations for doing simple sound effects. This code is
only usable for simple WAV files containing either 16-bit samples (stereo or
mono) or Yamaha ADPCM (4-bits, stereo or mono). Also, all sounds played in
this manner must be at most 65534 samples in length, as this does not handle
buffer chaining or anything else complex. For more interesting stuff, you
should probably look at the sound stream stuff instead.
But reading the source code i found :

Code: Select all


int snd_sfx_play_chn(int chn, sfxhnd_t idx, int vol, int pan) {
    int size;
    snd_effect_t * t = (snd_effect_t *)idx;
    AICA_CMDSTR_CHANNEL(tmp, cmd, chan);

    size = t->len;

   if(size >= 65535) size = 65534;

So first question is :
-------------------
Is a sound FX must be 65535 bytes maximum length or 65535 samples length (it's quite different) ?

Second question :
-------------------
Is it possible to modify this length constraint value by modify KOS (even just to create my own KOS version) ?

Third question :
-----------------
Is it possible to modify the volume of a FX when is playing by modify KOS (even just to create my own KOS version) ?

Thanks for your help and merry Christmas !
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Question about sound Fx length and volume

Post by BlueCrab »

The maximum is in samples. If you look up at the function that loads the sound (snd_sfx_load), you'll find that t->len is set by the size in bytes multiplied or divided by various values depending on the format.

The constraint is not changeable without making the snd_sfx stuff a lot more complicated than it is, as the constraint there is in what you can set a channel on the sound chip to do in one shot. At that point, you'd basically be making it into the snd_stream stuff, so it'd be better just to use that in the first place (snd_sfx is for one-shot things, whereas snd_stream is meant for things that loop or are longer than the one-shot limit of samples).

Since snd_sfx is meant for one-shots, no you can't change the volume while it's playing as things are set up. However, snd_stream does afford you that functionality. :wink:

So, after all is said and done, you're really better off using the snd_stream functionality for what you're looking to do. It was designed for doing background music and the like, and it supports longer length music and volume adjustment and the like, just as you seem to want. :)
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: Question about sound Fx length and volume

Post by Newbie »

OK, i understand.

Let's say i want very basic stream playing with no decoding routine at all.

I have something like a big bytes array full of the data of a WAV sample file into main memory.

So i want to stream those data to the sound card to play the sound.

I imagine all the functions i must use to do that are in the stream.h file :

Code: Select all


void snd_stream_set_callback(snd_stream_hnd_t hnd, snd_stream_callback_t cb);

void snd_stream_filter_add(snd_stream_hnd_t hnd, snd_stream_filter_t filtfunc,
                           void *obj)
						   
void snd_stream_filter_remove(snd_stream_hnd_t hnd,
                              snd_stream_filter_t filtfunc, void *obj);

void snd_stream_prefill(snd_stream_hnd_t hnd);

int snd_stream_init();

void snd_stream_shutdown();

snd_stream_hnd_t snd_stream_alloc(snd_stream_callback_t cb, int bufsize);

int snd_stream_reinit(snd_stream_hnd_t hnd, snd_stream_callback_t cb);

void snd_stream_destroy(snd_stream_hnd_t hnd);

void snd_stream_queue_enable(snd_stream_hnd_t hnd);

void snd_stream_queue_disable(snd_stream_hnd_t hnd);

void snd_stream_queue_go(snd_stream_hnd_t hnd);

void snd_stream_start(snd_stream_hnd_t hnd, uint32 freq, int st);

void snd_stream_stop(snd_stream_hnd_t hnd);

int snd_stream_poll(snd_stream_hnd_t hnd);

void snd_stream_volume(snd_stream_hnd_t hnd, int vol);

I could understand easily that i must initialize the system before doing anything by using

Code: Select all


int snd_stream_init();

I certainly must kill th stream i made and shut down the system before exiting at the end of my code

Code: Select all


void snd_stream_destroy(snd_stream_hnd_t hnd);

void snd_stream_shutdown();

But i am stuck in the process of giving data to be played ....

Could you explain me how it works please ?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Question about sound Fx length and volume

Post by BlueCrab »

The best example (and the only one I'd have prepared at the moment) is to look at the existing snd_stream_* using libraries -- liboggvorbisplay, libtremor, libmp3, etc. They all show the basic way to use the functionality. You'd simply be replacing the decoding process with a much simpler "decoder" that potentially does almost nothing. That's all there would be to it.

The basic gist of it is that you initialize the whole stream system, set up your stream handle, then periodically call snd_stream_poll(), which will call the callback you set up when you initialize the stream handle if more data is needed. Your callback would be really simple -- all it would have to do is to return a pointer to the next sample in memory and update the *size_out value to say how many samples are available, pretty much.
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: Question about sound Fx length and volume

Post by Newbie »

Ok, i manage to make it with your tips :)
Work great like a charm.

Now a last question about stream :
when i submit mono / stereo WAV data to the stream system, is it played mono /stereo ?
Is it played always stereo ?
Is there something to switch to mono / stereo playback ?

Thanks.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5652
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: Question about sound Fx length and volume

Post by BlueCrab »

The way the code is, it is always played in stereo. If you submit mono audio, it will be played on both channels.
User avatar
Newbie
Insane DCEmu
Insane DCEmu
Posts: 171
Joined: Sat Jul 27, 2013 1:16 pm
Has thanked: 0
Been thanked: 0

Re: Question about sound Fx length and volume

Post by Newbie »

Ok, thanks for your help.
Post Reply