-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
executable file
·240 lines (175 loc) · 4.83 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <alsa/asoundlib.h>
#include <stdio.h>
#include "sound.h"
#include "atenzo.h"
#define PCM_DEVICE_NAME "default"
char *SoundBuffer;
char *SoundDeviceName;
int buff_size;
unsigned int s_tmp;
snd_pcm_t *SoundDeviceHandle;
snd_pcm_uframes_t frames;
unsigned int GetNumSoundCards()
{
unsigned int totalCards;
int cardIndex;
totalCards = 0;
cardIndex = -1;
for (;;)
{
if (snd_card_next(&cardIndex) < 0)
break;
if (cardIndex < 0)
break;
totalCards++;
}
snd_config_update_free_global();
return totalCards;
}
void InitializeSoundDevice( char* DeviceName )
{
snd_pcm_hw_params_t *params;
int rate, channels;
rate = 44100;
channels = 1;
SoundDeviceHandle = NULL;
SoundDeviceName = NULL;
if(GetNumSoundCards() == 0)
return;
/* Open the PCM device in playback mode */
if (snd_pcm_open(&SoundDeviceHandle, DeviceName ? DeviceName:PCM_DEVICE_NAME, SND_PCM_STREAM_PLAYBACK, 0) < 0)
return;
SoundDeviceName = DeviceName;
if (Debug)
{
snd_pcm_info_t *pcmInfo;
snd_ctl_t *audioHandle;
snd_ctl_card_info_t *cardInfo;
char str[64];
int cardNum;
int err;
snd_pcm_info_alloca(&pcmInfo);
memset(pcmInfo, 0, snd_pcm_info_sizeof());
snd_pcm_info(SoundDeviceHandle, pcmInfo);
audioHandle = 0;
cardNum = snd_pcm_info_get_card(pcmInfo);
sprintf(str, "hw:%i", cardNum);
if ((err = snd_ctl_open(&audioHandle, str, 0)) == 0)
{
snd_ctl_card_info_alloca(&cardInfo);
snd_ctl_card_info(audioHandle, cardInfo);
LogToFile("Initialize sound device %s", snd_ctl_card_info_get_name(cardInfo));
snd_ctl_close(audioHandle);
}
else
{
LogToFile("Can't open card %s: %s", snd_pcm_info_get_name(pcmInfo), snd_strerror(err));
}
}
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(SoundDeviceHandle, params);
/* Set parameters */
snd_pcm_hw_params_set_access(SoundDeviceHandle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(SoundDeviceHandle, params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(SoundDeviceHandle, params, channels);
snd_pcm_hw_params_set_rate_near(SoundDeviceHandle, params, &rate, 0);
/* Write parameters */
snd_pcm_hw_params(SoundDeviceHandle, params);
/* Get info */
snd_pcm_hw_params_get_channels(params, &s_tmp);
snd_pcm_hw_params_get_rate(params, &s_tmp, 0);
snd_pcm_hw_params_get_period_size(params, &frames, 0);
snd_pcm_hw_params_get_period_time(params, &s_tmp, NULL);
buff_size = frames * channels * 2 /* 2 -> sample size */;
}
unsigned int InitializeSoundFile( char* FileName, SOUND_FILE* SoundFile )
{
SoundFile->Handle = open(FileName, O_RDONLY);
if (SoundFile->Handle == 0)
{
//printf("Could not open source file\n");
return 0;
}
SoundFile->Size = lseek(SoundFile->Handle, 0, SEEK_END);
SoundFile->Buffer = (char *) malloc(SoundFile->Size);
lseek(SoundFile->Handle, 0, SEEK_SET);
read(SoundFile->Handle, SoundFile->Buffer, SoundFile->Size);
SoundFile->Interval = 0;
SoundFile->LastPlayed = 0;
//close(SoundFile->Handle);
return 1;
}
void TerminateSound()
{
snd_pcm_drain(SoundDeviceHandle);
snd_pcm_close(SoundDeviceHandle);
}
void CloseSoundFile( SOUND_FILE* SoundFile )
{
free(SoundFile->Buffer);
if (SoundFile->Handle)
{
close(SoundFile->Handle);
SoundFile->Handle = 0;
}
}
int PlaySoundFromFile( SOUND_FILE* SoundFile )
{
int bytesRead = 0;
int totalBytesRead = 0;
unsigned int pcm;
int bufferSize;
lseek(SoundFile->Handle, 0, SEEK_SET);
//snd_pcm_prepare(SoundDeviceHandle); //reset sound card buffer
bufferSize = buff_size;
while (totalBytesRead < SoundFile->Size)
{
if (totalBytesRead + bufferSize > SoundFile->Size)
bufferSize = SoundFile->Size - totalBytesRead;
bytesRead = read(SoundFile->Handle, SoundFile->Buffer, bufferSize);
totalBytesRead += bytesRead;
if (bytesRead == 0)
{
return 0;
}
if (snd_pcm_writei(SoundDeviceHandle, SoundFile->Buffer, frames) == -EPIPE)
{
snd_pcm_prepare(SoundDeviceHandle);
}
}
return 0;
}
int PlaySoundFromBuffer( SOUND_FILE* SoundFile )
{
int totalBytesRead = 0;
int bytesToRead = 0;
char* buff = 0;
buff = SoundFile->Buffer;
bytesToRead = buff_size;
while (totalBytesRead < SoundFile->Size)
{
if (snd_pcm_writei(SoundDeviceHandle, buff, frames) == -EPIPE)
{
snd_pcm_prepare(SoundDeviceHandle);
}
if (totalBytesRead + bytesToRead > SoundFile->Size)
bytesToRead = SoundFile->Size - totalBytesRead;
buff += bytesToRead;
totalBytesRead += bytesToRead;
}
return 0;
}
int PlaySound( SOUND_FILE* SoundFile )
{
long long time;
time = current_timestamp();
if (SoundFile->Interval && (time - SoundFile->LastPlayed < SoundFile->Interval))
return 0;
if (SoundDeviceHandle == NULL)
return 0;
snd_pcm_prepare(SoundDeviceHandle); //reset sound card buffer
PlaySoundFromFile(SoundFile);
SoundFile->LastPlayed = time;
return 0;
}