/cosmix

Primary LanguageCGNU General Public License v3.0GPL-3.0

cosMix - A sound mixing engine for the Sony Playstation2

cosMix is the result of my attempt to create a sound engine easy to use under ps2sdk as alternative to PS2 SDLmixer while adding different features. Currently it's only partially compatible.

Future features :

Ogg/MP3/MOD streaming support while mixing regular samples.

Usage and API Reference

The principle of operation is simple : The mixer expects the samples being available from RAM, so it's up to you to put them into sint16 buffers before calling any of the play functions. The mixer accepts mono and stereo samples as long they're aligned in memory just like the WAV format excluding any header info (see 'Stereo sample format reference' at end of file). The sample rate is fixed at 48Khz since it uses SjPCM but other sample rates should be available in the future.

The mixer lives on its own thread, so it's a matter of calling and forget, as long as you take take of thread switching (see example 'simpleTest_with_thread_switching').

So, you must call the init functions (see 'Section Init' bellow) to set up the mixer thread and use any of the play functions to actually fire the sound (see 'Section A'). Each sample is played at a selected channel, indexed from 0 to _MIXER_MAXCHANNELS or automatically selected.

See also the examples section.

Section Init

SjPCM related (you must call these first):

SjPCM_Init(i)	// 1 = sync mode, 0 async mode

SjPCM_Setvol(x);	// 0x3fff is full vol

any other SjPCM function you may want

cosMix init:

void Mixer_Init()				// Init mixer channels, autodetect PAL/NTSC and sets buffer
                             // size accordingly, creates and starts mixer thread and
										// associated VBlank handler

void Mixer_Terminate()		// Release resources used, optional (not proper tested)

Section A

void PlaySample(sint16 * sampleAddress, int sampleLenght, int vol, int stereo)
// Instruct mixer to play stereo or mono 16-Bit Stereo PCM (PC Windows WAV format).
// sampleAddress must point to sample previously loaded in memory, sampleLenght is the
// lenght in bytes, vol ranges from 0 to 128. Stereo is used if stereo = 1, mono if = 0.
// Channel selection is automatic.

int  PlaySampleAtChannel(int selected_channel, sint16 * sampleAddress, int sampleLenght, int vol, int stereo);
// Same as above but you can specify a particular channel to use. 'selected_channel' should take a value
// from 0 to _MIXER_MAXCHANNELS.

int  StopSampleAtChannel(int selected_channel);
// Stop sample at channel 'selected_channel'.

int  IsPlayingAtChannel(int chan);
// Returs 1 if a sample is playing at channel 'chan' and 0 otherwise.

int  IsPlaying();
// Returns the number of channels currently playing (0 or more).

WAV sample format reference

Stereo :

from: http://replaygain.hydrogenaudio.org/wav_format.txt

Sample 1
						   LEFT                    RIGHT
                         16 bits                  16 bits
                   ----SIGNED INT----       ----SIGNED INT----
/                    \   /
LEFT LOW     LEFT HIGH   RIGHT LOW    RIGHT HIGH

Channel 0    Channel 0   Channel 1    Channel 1
                  (left)       (left)      (right)      (right)
                 low-order   high-order   low-order   high-order
                   byte         byte         byte        byte

Data Packing for 16-Bit Stereo PCM

Mono :

Expect the LOW HIGH order.

To be done list

  • Mixer_Init() should have some arguments
  • implement sample loop

Thanks for the following people, not in any particular order :

  • Lukasz for starting his PS2Doom port with broken sound which compelled me to write a sound engine
  • adresd for his PS2 ModPlayer v2.0 that shread some valuable light on how to make and use threads with ps2sdk
  • Lazy Bastard for his PS2Compiles pack where I found PS2 ModPlayer v2.0
  • EEUG for the valueble tips on thread switching
  • jbit for the talk and tips for using adpcm instead (who knows in the future?)
  • All the other guys who made the ps2sdk