aboutsummaryrefslogtreecommitdiff
path: root/mixer.h
blob: 6b7f3bdf1dfca05924ee1c47e1bf8e0e5a9c062b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * 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 Pcm Pcm;
typedef struct AudioEvent AudioEvent;
typedef struct AudioSourceInfo AudioSourceInfo;
typedef struct AudioSource AudioSource;
typedef struct Mixer Mixer;

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
{
	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 initaudio(int);
void audio_set_master_gain(double);
void processaudio(s16int*, int);

AudioSource *newaudiosource(AudioSourceInfo*);
AudioSource *loadaudiosource(char*);
void delaudiosource(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 playaudio(AudioSource*);
void pauseaudio(AudioSource*);
void stopaudio(AudioSource*);