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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include <stdio.h>
#include <stdlib.h>
#include <portaudio.h>
#include <mossrose.h>
#include "mutex.h"
#include "channel.h"
/* ~~~~~~~~~~~~~~~~ type definitions ~~~~~~~~~~~~~~~~ */
/* audio output */
struct audio_output_t {
float l;
float r;
};
/* ~~~~~~~~~~~~~~~~ globals ~~~~~~~~~~~~~~~~ */
struct mossrose_globals_t {
PaStream *stream;
struct mossrose_channel_t *channels;
int n_channels;
} mossrose_global;
struct audio_output_t build_sample()
{
struct audio_output_t out;
out.l = 0; out.r = 0;
/* loop variables */
struct mossrose_channel_t *chan;
float chan_l, chan_r;
for (int i=0; i<mossrose_global.n_channels; i++) {
chan = mossrose_global.channels + i;
if (mossrose_mutex_trylock(&(chan->mutex)) != 0) {
/* can't lock the mutex, this channel is being modified */
printf("can't lock channel %d\n", i);
continue;
}
if (chan->n_samples == 0) {
/* channel is not currently in use, skip */
}
else {
if (mossrose_channel_advance(&chan_l, &chan_r, chan) != 0)
/* channel is done playing, reset */
mossrose_channel_reset(chan);
else {
out.l += chan_l;
out.r += chan_r;
}
}
mossrose_mutex_unlock(&(chan->mutex));
}
return out;
}
static int callback(
const void *input,
void *output,
unsigned long frame_count,
const PaStreamCallbackTimeInfo *time_info,
void *userdata)
{
struct audio_output_t *out = output;
struct audio_output_t sample;
for (int i=0; i<frame_count; i++) {
sample = build_sample();
out[i].l = sample.l;
out[i].r = sample.r;
}
return 0;
}
int mossrose_init(double sample_rate, int n_channels)
{
/* initialize channels */
mossrose_global.n_channels = n_channels;
mossrose_global.channels = malloc(n_channels * sizeof(struct mossrose_channel_t));
if (mossrose_global.channels == NULL) {
fprintf(stderr, "failed to allocate memory for %d channels", n_channels);
return 1;
}
for (int i=0; i<n_channels; i++) {
mossrose_channel_init(mossrose_global.channels + i);
}
PaError err;
/* initialize portaudio */
err = Pa_Initialize();
if (err != paNoError) {
fprintf(stderr, "failed to initialize PortAudio!\n");
return 1;
}
/* open stream */
err = Pa_OpenDefaultStream(&(mossrose_global.stream), 0, 2, paFloat32, sample_rate, 0, callback, NULL);
if (err != paNoError) {
fprintf(stderr, "failed to open audio stream!\n");
return 1;
}
/* start stream */
err = Pa_StartStream(mossrose_global.stream);
if (err != paNoError) {
fprintf(stderr, "failed to start audio stream!\n");
return 1;
}
return 0;
};
int mossrose_play(float *left, float *right, size_t len, int channel)
{
if (channel > 0) {
if (mossrose_channel_set(mossrose_global.channels+channel, left, right, len, 1) == 0)
return channel;
else
return -1;
}
else {
struct mossrose_channel_t *chan;
for (int i=0; i<mossrose_global.n_channels; i++) {
chan = mossrose_global.channels + i;
if (mossrose_channel_set(chan, left, right, len, 0) == 0) return i;
}
return -1;
}
}
int mossrose_terminate(double sample_rate, int n_channels)
{
Pa_AbortStream(mossrose_global.stream);
Pa_CloseStream(mossrose_global.stream);
Pa_Terminate();
return 0;
}
|