Sound, And Ai


Khatoblepas

Still Fresh
Joined
Dec 28, 2005
Messages
94
Age
35
Location
England
Website
Visit site
I once again have a question on programming.

Before this project, I only used the PC Speaker to produce sound, with a timer to change the note when the last one finished. Sound is a strange and wonderful concept, and I just can't get my head around it.

How does it work on the GP2x, exactly? Do you have to, like I mentioned above, time everything and play individual tones out of the speaker? Or can you just run a function and it plays in the background automatically. ALso, on the subject of sound, I wish to implement a dynamic MOD system, which can mute and unmute channels with one function. This ensures I can change the mood of the song without loading another song in first. (It'll play a large part in the game's atmosphere, so this is important.)

Secondly, I need to find a way to implement a simple 'script' for NPC walking and action and junk. I've made a similar thing for the Monster AI in battles, with an ASM like syntax and 256 different commands. I just can't figure out how to make the NPCs move of their own accord along with the player character without resorting to convoluted and horribly complicated code.

I know you guys are geniuses at this kinda thing, so I hope you don't mind me asking. o.o
 
not a coder yet but i am up early. and i'm pretty sure you don't need to play "notes" on the speaker!

if you're using sdl then there's sdl_sound and sdl_mixer. i know that sdl_sound is pretty basic...i don't think it plays compressed audio or is able to play more than one sound at a time.

this thread might help: http://www.gp32x.de/board/index.php?showtopic=22759

at worst you may have to write some code that will load individual audio samples from a file in order to mix/play them. but you sure as hell don't need to deal with frequencies.

also some of the apps use timidity to play midi files (at least, i think it's midi...)

ok time for someone else to have a go.
 
Last edited by a moderator:
Khatoblepas posted on Feb 6 2006 at 12:43 PM said:
I once again have a question on programming.

Before this project, I only used the PC Speaker to produce sound, with a timer to change the note when the last one finished. Sound is a strange and wonderful concept, and I just can't get my head around it.

:blink:

Didn't you code that 15 years ago by accident? :)

Khatoblepas posted on Feb 6 2006 at 12:43 PM said:
How does it work on the GP2x, exactly? Do you have to, like I mentioned above, time everything and play individual tones out of the speaker? Or can you just run a function and it plays in the background automatically. ALso, on the subject of sound, I wish to implement a dynamic MOD system, which can mute and unmute channels with one function. This ensures I can change the mood of the song without loading another song in first. (It'll play a large part in the game's atmosphere, so this is important.)

Technically the gp2x has just a AC97 codec so basically a stereo pcm 16bit DAC.

Khatoblepas posted on Feb 6 2006 at 12:43 PM said:
Secondly, I need to find a way to implement a simple 'script' for NPC walking and action and junk. I've made a similar thing for the Monster AI in battles, with an ASM like syntax and 256 different commands. I just can't figure out how to make the NPCs move of their own accord along with the player character without resorting to convoluted and horribly complicated code.

First it's good to have a nice positional systems for characters. An 2D table for an example (if it'd be sufficient) and every value can be occupied say by one NPC. Then you can scan easily for neighbours and compute strength of things like the attack (if there is less enemies than friends), flee (if otherwise), sleep (if there no enemies and stamina level is low), etc. Then depending how many points every type of action had accumulated you can chose what is appropriate action for a character.
 
Last edited by a moderator:
On most systems, a common sound method is just to fill a sound buffer on request; ie: OS gives you a X-size buffer to fill, of (say) 16-bit samples. You can not fill it (muted), or you can change volume (multiply by some value to inc/decrease), or do what you wish. If you want to render MOD or MIDI then you need to write or port a MOD or MIDI renderer; MIDI tends to higher overhead, but is nicer of course.

As to moving things around .. As above.. usually you've got a linked list or table of some sort listing off all 'mobiles' in the world; then you just loop through it, making them do something (moving them by altering coordinates, etc.) You coudl do this on a timer, or on every frame, rhwtaver.. its up to your game design.

ie: First you need a game goal, then you worry about how to implement. You cannot come up with sensible implementation goals without a game goal :)

jeff
 
On Sound:

Well, I like the global sample data that MIDI has, but I prefer the way that MODs work, with their custom samples. So I thought about it for a while, and decided:

256 samples MAX in the library, but of that only a small amount can be loaded into memory (to allow for memory constraints), at 22Khz Mono (this will be compensated by the fact that each channel can be assigned to either left or right.). 0-15 will be reserved for synthasised waveforms (saves SD space, less data to process), and external OGG files can be parsed in as instrument 245-255 (for vocals - they would take up much more space and would NEED to be compressed with something other than RLE. Also ensures they can be Stereo, and high quality, too).

There are eight channels, with the eighth channel reserved for the vocal track.
I'm not quite sure how to store the pattern data, but RLE for all 7 other channels stored in blocks of 128, so that only that bit is pulled from the file at that time.... I'm not sure. Suggestions? Maybe if the pattern data didn't fill up the cache..

I want to avoid using Assembler as much as I can, but I think the MOD parser would have to be partly in ASM.

*Note: The reason I'm using Vocals along with the MODs is that voice-only OGG files would, in theory, compress better than the ogg file with all the other music in it too. Also, with the vocals playing seperately to the Dynamic Mod, I can change the instumental bits without using a totally new song file.

I don't know. That's kinda what I want to do.
 
Do ASM later; get it working, and then find the parts that need optimizing; never optimize in advance.

As to your description above.. its essentially a 'fat MOD'.

I doubt you need to RLE your playback table.. ie: How many notes are in a song? A few hundred for each channel? :)

Sound playback has to be _fast_ so it doesn't eat the whole CPU; so no compression, no fancy stuff; keep it simple, and waste as much memory as you can afford ;)

jeff
 
Alright! I don't have to do ASM off the cuff! ^^. (I can only convert, with a list of opcodes handy.. I can't write straight assembler.)

So, like the video, I can use a buffer to mix in all the elements and send it to the sound device?

How would I mix in the sounds in this case? Say I had:
Code:
NOTE  | 1 | 2 | 3 | 4 |
0        | C | C| D | D|
1        | -  | - | -  | D|
How would the second instruction be rendered? I would know how to play the sound, if it's simple, but how would I continue the sound or loop it? So confusing. I feel like I felt when I just started on my 386. ><
 
Back
Top