aboutsummaryrefslogtreecommitdiff
path: root/mixer.h
blob: 81e22eef14327a861f649629e7662615e91fccad (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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*);