-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.c
157 lines (135 loc) · 4.49 KB
/
sound.c
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
150
151
152
153
154
155
156
157
#include <sndfile.h>
#include <portaudio.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
SNDFILE *sndFile, *injFile;
SF_INFO sfInfo, injInfo;
volatile char* filepath;
volatile int inject_audio;
int position;
volatile double volume;
volatile double deltavol;
volatile int volch;
volatile int thread_complete;
volatile int thread_idle;
} adata_t;
int Callback(const void *input,
void *output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo* paTimeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
adata_t *data = (adata_t *)userData; /* we passed a data structure into the callback so we have something to work with */
int *cursor; /* current pointer into the output */
int *inject_buff;
int *out = (int *)output;
int thisSize = frameCount;
int thisRead;
cursor = out; /* set the output cursor to the beginning */
while (thisSize > 0)
{
/* seek to our current file position */
sf_seek(data->sndFile, data->position, SEEK_SET);
/* are we going to read past the end of the file?*/
if (thisSize > (data->sfInfo.frames - data->position))
{
/*if we are, only read to the end of the file*/
thisRead = data->sfInfo.frames - data->position;
/* and then loop to the beginning of the file */
data->position = 0;
}
else
{
/* otherwise, we'll just fill up the rest of the output buffer */
thisRead = thisSize;
/* and increment the file position */
data->position += thisRead;
}
/* since our output format and channel interleaving is the same as sf_readf_int's requirements */
/* we'll just read straight into the output buffer */
sf_readf_int(data->sndFile, cursor, thisRead);
int i;
for (i = 0; i < thisRead; i++)
{
if (data->volch)
{
if (data->deltavol > 0)
{
data->volume += 0.001;
data->deltavol -= 0.001;
}
if (data->deltavol < 0)
{
data->volume -= 0.001;
data->deltavol += 0.001;
}
if (data->deltavol == 0)
{
data->volch = 0;
}
}
cursor[i] *= data->volume;
}
/* increment the output cursor*/
cursor += thisRead;
/* decrement the number of samples left to process */
thisSize -= thisRead;
}
return paContinue;
}
void handle_sound(adata_t *data)
{
PaStream *stream;
PaError error;
PaStreamParameters outputParameters;
/* initialize our data structure */
data->position = 0;
data->thread_idle = 0;
data->sfInfo.format = SF_FORMAT_OGG;
/* try to open the file */
data->sndFile = sf_open(data->filepath, SFM_READ, &data->sfInfo);
if (!data->sndFile)
{
printf("error opening file\n");
exit(1);
}
/* start portaudio */
Pa_Initialize();
/* set the output parameters */
outputParameters.device = Pa_GetDefaultOutputDevice(); /* use the default device */
outputParameters.channelCount = data->sfInfo.channels; /* use the same number of channels as our sound file */
outputParameters.sampleFormat = paInt32; /* 32bit int format */
outputParameters.suggestedLatency = 0.2; /* 200 ms ought to satisfy even the worst sound card */
outputParameters.hostApiSpecificStreamInfo = 0; /* no api specific data */
/* try to open the output */
error = Pa_OpenStream(&stream, /* stream is a 'token' that we need to save for future portaudio calls */
0, /* no input */
&outputParameters,
data->sfInfo.samplerate, /* use the same sample rate as the sound file */
paFramesPerBufferUnspecified, /* let portaudio choose the buffersize */
paNoFlag, /* no special modes (clip off, dither off) */
Callback, /* callback function defined above */
data ); /* pass in our data structure so the callback knows what's up */
/* if we can't open it, then bail out */
if (error)
{
printf("error opening output, error code = %i\n", error);
Pa_Terminate();
}
/* when we start the stream, the callback starts getting called */
Pa_StartStream(stream);
long ms_length = ((double)data->sfInfo.frames / (double)data->sfInfo.samplerate) * 1000.;
Pa_Sleep(ms_length);
Pa_CloseStream(stream); // stop the stream
(*data).thread_complete = 1;
Pa_Terminate(); // and shut down portaudio
}
void inject_audio(adata_t* adata, char* filepath)
{
adata->injInfo.format = SF_FORMAT_OGG;
adata->injFile = sf_open(filepath, SFM_READ, &adata->injInfo);
adata->inject_audio = 1;
}