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
|
#include <stdio.h>
#include <stdlib.h>
#include <plibsys.h>
#include <portaudio.h>
#include <mossrose.h>
#include "channel.h"
struct mossrose_global_t {
PaStream *stream;
struct channel_t *channels;
int n_channels;
} mossrose_global;
static int callback(
const void *input,
void *output,
unsigned long frame_count,
const PaStreamCallbackTimeInfo* time_info,
PaStreamCallbackFlags status_flags,
void *user_data)
{
float *out = output;
float *left, *right;
float l, r;
for (int i=0; i<frame_count; i++) {
left = out + 2*i;
right = left + 1;
*left = 0;
*right = 0;
for (int j=0; j<mossrose_global.n_channels; j++) {
struct channel_t *chan = mossrose_global.channels + j;
channel_get_next_sample(&l, &r, chan);
*left += l;
*right += r;
}
}
return 0;
}
int mossrose_init(double sample_rate, int n_channels, bool init_plibsys)
{
/* optionally initialze plibsys */
if (init_plibsys)
p_libsys_init();
/* initialize channels */
mossrose_global.n_channels = n_channels;
mossrose_global.channels = malloc(n_channels * sizeof(struct channel_t));
if (mossrose_global.channels == NULL) {
fprintf(stderr, "failed to allocate memory for %d channels\n", n_channels);
return 1;
}
for (int i=0; i<n_channels; i++)
channel_init(mossrose_global.channels + i);
PaError err;
/* initialize portaudio */
err = Pa_Initialize();
if (err != paNoError) {
fprintf(stderr, "failed to initialize portaudio\n");
return 2;
}
/* initialize 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 3;
}
/* start stream */
err = Pa_StartStream(mossrose_global.stream);
if (err != paNoError) {
fprintf(stderr, "failed to start audio stream\n");
return 4;
}
return 0;
};
int mossrose_play(struct mossrose_sound_t *sound, int channel)
{
if (channel >= 0) {
/* play on specified channel */
if (channel_sound_load(mossrose_global.channels + channel, sound, true) == 0)
return channel;
else
return -1;
}
else {
/* play on first available channel */
for (int i=0; i<mossrose_global.n_channels; i++) {
struct channel_t *chan = mossrose_global.channels + i;
if (channel_sound_load(chan, sound, false) == 0) return i;
}
return -1;
}
}
void mossrose_channel_set_volume(int channel, float volume) {
struct channel_t *chan = mossrose_global.channels + channel;
channel_set_volume(chan, volume);
}
void mossrose_channel_set_pan(int channel, float left, float right) {
struct channel_t *chan = mossrose_global.channels + channel;
channel_set_pan(chan, left, right);
}
void mossrose_channel_pause(int channel) {
struct channel_t *chan = mossrose_global.channels + channel;
channel_pause(chan);
}
void mossrose_channel_resume(int channel) {
struct channel_t *chan = mossrose_global.channels + channel;
channel_resume(chan);
}
int mossrose_terminate()
{
Pa_AbortStream(mossrose_global.stream);
Pa_CloseStream(mossrose_global.stream);
Pa_Terminate();
return 0;
}
|