aboutsummaryrefslogtreecommitdiff
path: root/mixer.h
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-10-01 00:06:50 +0000
committerrodri <rgl@antares-labs.eu>2023-10-01 00:06:50 +0000
commitb221c56a6bb90de4631f4b2eb999db8dad05a006 (patch)
tree9ed5baabbf12fc4e6b97bd4777a4d5348e5536f7 /mixer.h
parent9c377e4f25344a908f18f5154ac9143ffb9577e6 (diff)
downloadbattleship-b221c56a6bb90de4631f4b2eb999db8dad05a006.tar.gz
battleship-b221c56a6bb90de4631f4b2eb999db8dad05a006.tar.bz2
battleship-b221c56a6bb90de4631f4b2eb999db8dad05a006.zip
add a mixer and some background sfx.
also postponed the connection establishment until the subsystem initialization is complete. there's no point in taking a seat you're going to leave milliseconds after a local resource failure.
Diffstat (limited to 'mixer.h')
-rw-r--r--mixer.h123
1 files changed, 123 insertions, 0 deletions
diff --git a/mixer.h b/mixer.h
new file mode 100644
index 0000000..81e22ee
--- /dev/null
+++ b/mixer.h
@@ -0,0 +1,123 @@
+/*
+ * standalone sound mixer based on rxi's cmixer.
+ */
+enum {
+ FIXED_BITS = 12,
+ FIXED_UNIT = 1<<FIXED_BITS,
+ FIXED_MASK = FIXED_UNIT-1,
+
+ MIXBUFSIZE = 512,
+ MIXBUFMASK = MIXBUFSIZE-1,
+
+ AUDIO_STATE_STOPPED = 0,
+ AUDIO_STATE_PLAYING,
+ AUDIO_STATE_PAUSED,
+
+ AUDIO_EVENT_DESTROY = 0,
+ AUDIO_EVENT_SAMPLES,
+ AUDIO_EVENT_REWIND
+};
+
+typedef struct Wav Wav;
+typedef struct WavStream WavStream;
+typedef struct Pcm Pcm;
+typedef struct AudioEvent AudioEvent;
+typedef struct AudioSourceInfo AudioSourceInfo;
+typedef struct AudioSource AudioSource;
+typedef struct Mixer Mixer;
+
+struct Wav
+{
+ void *data;
+ int bitdepth;
+ int samplerate;
+ int channels;
+ int length;
+};
+
+struct WavStream
+{
+ Wav wav;
+ void *data;
+ int idx;
+};
+
+struct Pcm
+{
+ void *data;
+ int len;
+ int off;
+ int depth;
+ int chans;
+ int rate;
+};
+
+struct AudioEvent
+{
+ int type;
+ void *udata;
+ char *msg;
+ s16int *buffer;
+ int length;
+};
+
+struct AudioSourceInfo
+{
+ void *udata;
+ int samplerate;
+ int length;
+
+ void (*handler)(AudioEvent *e); /* Event handler */
+};
+
+struct AudioSource
+{
+ AudioSource *next; /* Next source in list */
+ s16int buffer[MIXBUFSIZE]; /* Internal buffer with raw stereo PCM */
+ void *udata; /* Stream's udata (from AudioSourceInfo) */
+ int samplerate; /* Stream's native samplerate */
+ int length; /* Stream's length in samples */
+ int end; /* End index for the current play-through */
+ int state; /* Current state (playing|paused|stopped) */
+ s64int position; /* Current playhead position (fixed point) */
+ int lgain, rgain; /* Left and right gain (fixed point) */
+ int rate; /* Playback rate (fixed point) */
+ int nextfill; /* Next frame idx where the buffer needs to be filled */
+ int loop; /* Whether the source will loop when `end` is reached */
+ int rewind; /* Whether the source will rewind before playing */
+ int active; /* Whether the source is part of `sources` list */
+ double gain; /* Gain set by `audio_set_gain()` */
+ double pan; /* Pan set by `audio_set_pan()` */
+
+ void (*handler)(AudioEvent *e); /* Event handler */
+};
+
+struct Mixer
+{
+ char *lasterror; /* Last error message */
+ AudioSource *sources; /* Linked list of active (playing) sources */
+ s32int buffer[MIXBUFSIZE]; /* Internal master buffer */
+ int samplerate; /* Master samplerate */
+ int gain; /* Master gain (fixed point) */
+};
+
+
+void audio_init(int);
+void audio_set_master_gain(double);
+void audio_process(s16int*, int);
+
+AudioSource *audio_new_source(AudioSourceInfo*);
+AudioSource *audio_new_source_from_file(char*);
+AudioSource *audio_new_source_from_mp3file(char*);
+AudioSource *audio_new_source_from_mem(void*, int);
+void audio_destroy_source(AudioSource*);
+double audio_get_length(AudioSource*);
+double audio_get_position(AudioSource*);
+int audio_get_state(AudioSource*);
+void audio_set_gain(AudioSource*, double);
+void audio_set_pan(AudioSource*, double);
+void audio_set_pitch(AudioSource*, double);
+void audio_set_loop(AudioSource*, int);
+void audio_play(AudioSource*);
+void audio_pause(AudioSource*);
+void audio_stop(AudioSource*);