Pcm_AlsaPlay.c
11.6 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdio.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include "JZsdkLib.h"
#include "../AudioDeal.h"
#include "../FF_Statement.h"
#include "./pcm_AlsaPlay.h"
#include "../AudioDealThread/AudioDealThread.h"
#include "alsa/asoundlib.h"
#define FRAME_SIZE 4 // 假设是双声道16位PCM,即每个帧4字节
struct pcm_AlsaInfo
{
int SampleRate;
snd_pcm_t* g_handle;
snd_pcm_hw_params_t* g_params;
snd_pcm_sw_params_t* g_sw_params;
snd_pcm_status_t* pcm_status;
snd_pcm_uframes_t g_period_size; //实际处理的数数据长度
int g_gorup_size; //每次处理获取的数据长度
int dealFlag; //处理标志位,用于避免卡死
}pcm_AlsaInfo;
/**********
*
* 参数初始化
*
* *****/
static T_JZsdkReturnCode AlsaParamInit(struct pcm_AlsaInfo *AlsaInfo, int SampleRate)
{
//设置采样率
AlsaInfo->SampleRate = SampleRate;
//音频设备模式为 0 ,也可以用SND_PCM_ASYNC模式,该模式允许以异步方式进行数据的读写操作
static int open_mode = 0;
//open_mode = open_mode | SND_PCM_ASYNC;
//调用默认default音频设备
int ret = snd_pcm_open(&(AlsaInfo->g_handle), "default", SND_PCM_STREAM_PLAYBACK, open_mode);
if(ret < 0)
{
printf("open device failed\n");
return 0;
}
//设置解码的周期
//g_period_size 的值太大,可能会导致写入操作失败,返回一个负数的错误码;如果 g_period_size 的值太小,可能会导致写入操作成功,但音频播放可能会出现不连续或延迟。
AlsaInfo->g_period_size = 384;
//AlsaInfo->g_period_size = 512;
AlsaInfo->g_gorup_size = AlsaInfo->g_period_size * 2 * 16 / 8; //2声道数 16位深 8字节长度 即真正的一组数据的长度
/***************
*
* 硬件参数分配
*
* **************/
//为硬件参数分配内存空间,并将其指针保存在 g_params 中
snd_pcm_hw_params_alloca(&AlsaInfo->g_params);
//函数初始化硬件参数为默认值
snd_pcm_hw_params_any(AlsaInfo->g_handle, AlsaInfo->g_params);
/* 采样位数 Signed 16-bit little-endian format */
ret = snd_pcm_hw_params_set_format(AlsaInfo->g_handle, AlsaInfo->g_params, SND_PCM_FORMAT_S16);
if (ret < 0)
{
JZSDK_LOG_ERROR("unable to set hw format: %s\n",snd_strerror(ret));
}
/* 交错模式 Interleaved mode */
ret = snd_pcm_hw_params_set_access(AlsaInfo->g_handle, AlsaInfo->g_params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (ret < 0)
{
JZSDK_LOG_ERROR("unable to set hw access: %s\n",snd_strerror(ret));
}
/* 通道数 */
ret = snd_pcm_hw_params_set_channels(AlsaInfo->g_handle, AlsaInfo->g_params, 2);
if (ret < 0) {
JZSDK_LOG_ERROR("unable to set hw channels: %s\n",snd_strerror(ret));
}
// ret = snd_pcm_hw_params_set_channels(AlsaInfo->g_handle, AlsaInfo->g_params, 1);
// if (ret < 0) {
// JZSDK_LOG_ERROR("unable to set hw channels: %s\n",snd_strerror(ret));
// }
/* 采样率 44100 bits/second sampling rate (CD quality) */
ret = snd_pcm_hw_params_set_rate_near(AlsaInfo->g_handle, AlsaInfo->g_params, &AlsaInfo->SampleRate, NULL);
if (ret < 0)
{
JZSDK_LOG_ERROR("unable to set hw sampleRate: %s\n",snd_strerror(ret));
}
//设置一个周期的多少帧
ret = snd_pcm_hw_params_set_period_size_near(AlsaInfo->g_handle, AlsaInfo->g_params, &AlsaInfo->g_period_size, NULL);
if (ret < 0) {
JZSDK_LOG_ERROR("unable to set hw period_size: %s\n",snd_strerror(ret));
}
/* 将设置好的参数写入驱动 */
ret = snd_pcm_hw_params(AlsaInfo->g_handle, AlsaInfo->g_params);
if (ret < 0) {
JZSDK_LOG_ERROR("unable to set hw parameters: %s\n",snd_strerror(ret));
return 0;
}
/***************
*
* 软件参数分配
*
* **************/
snd_pcm_sw_params_alloca(&AlsaInfo->g_sw_params);
snd_pcm_uframes_t start_t = 1;
snd_pcm_uframes_t stop_t = 65536 * 2;
ret = snd_pcm_sw_params_current(AlsaInfo->g_handle, AlsaInfo->g_sw_params);
if (ret < 0) {
JZSDK_LOG_ERROR("unable to get current sw params: %s\n",snd_strerror(ret));
}
ret = snd_pcm_sw_params_set_start_threshold(AlsaInfo->g_handle, AlsaInfo->g_sw_params, start_t);
if (ret < 0) {
JZSDK_LOG_ERROR("unable to set sw start_threshold: %s\n",snd_strerror(ret));
}
ret = snd_pcm_sw_params_set_stop_threshold(AlsaInfo->g_handle, AlsaInfo->g_sw_params, stop_t);
if (ret < 0) {
JZSDK_LOG_ERROR("unable to set sw stop_threshold: %s\n",snd_strerror(ret));
}
ret = snd_pcm_sw_params(AlsaInfo->g_handle, AlsaInfo->g_sw_params);/* 将设置好的参数写入驱动 */
if (ret < 0) {
JZSDK_LOG_ERROR("unable to set sw parameters: %s\n",snd_strerror(ret));
return 0;
}
//分配音频设备的状态结构体内存
snd_pcm_status_malloc(&AlsaInfo->pcm_status);
printf("播放参数初始化完毕\n");
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**********
*
* 播放等待参数
*
* *****/
static T_JZsdkReturnCode AlsaWaitPlayEnd(struct pcm_AlsaInfo *AlsaInfo)
{
while(1){
snd_pcm_sframes_t delay_samples = 0;
snd_pcm_delay(AlsaInfo->g_handle, &delay_samples);
//未播放的组小于15个就可以返还了
if(delay_samples <= (AlsaInfo->g_period_size * 15))
{
break;
}
delayUs(100);
}
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**********
*
* pcm播放线程
*
* *****/
static void *Alsa_CheckThread(void *arg)
{
struct AudioDealInfo *IndexInfo = (struct AudioDealInfo *) arg;
struct pcm_AlsaInfo *AlsaInfo = (struct pcm_AlsaInfo *)IndexInfo->AlsaInfo;
int realLen = 0;//实际数据读取长度
int LastStatus = JZ_FLAGCODE_OFF;
//每10ms检测一次
while (1)
{
snd_pcm_sframes_t delay_samples = 0;
snd_pcm_delay(AlsaInfo->g_handle, &delay_samples);
//JZSDK_LOG_INFO("当前alsa存放了%d组",delay_samples);
if ( (delay_samples > 0) && (LastStatus == JZ_FLAGCODE_OFF))
{
//设置alsa状态
Set_AudioDeal_Alsa_Flag(IndexInfo , JZ_FLAGCODE_ON);
LastStatus = JZ_FLAGCODE_ON;
}
else if ( ( delay_samples == 0 ) && (LastStatus == JZ_FLAGCODE_ON))
{
//设置alsa状态
Set_AudioDeal_Alsa_Flag(IndexInfo , JZ_FLAGCODE_OFF);
LastStatus = JZ_FLAGCODE_OFF;
}
delayMs(10);
}
}
/**********
*
* alsa play反初始化
*
* *****/
// T_JZsdkReturnCode Pcm_Play_deInit(struct pcm_AlsaInfo *AlsaInfo)
// {
// //printf("snd_pcm_hw_params_free\n");
// //snd_pcm_hw_params_free(g_params);
// printf("snd_pcm_drain\n");
// snd_pcm_drain(AlsaInfo->g_handle);
// printf("snd_pcm_close\n");
// snd_pcm_close(AlsaInfo->g_handle);
// }
/**********
*
* alsa play初始化
*
* *****/
T_JZsdkReturnCode AlsaPlay_Init(struct AudioDealInfo *IndexInfo)
{
// 尝试获取或分配ResampleInfo
struct pcm_AlsaInfo **AlsaInfoPtr = (struct pcm_AlsaInfo **)&IndexInfo->AlsaInfo;
if (*AlsaInfoPtr == NULL) {
// 分配内存
*AlsaInfoPtr = (struct pcm_AlsaInfo *)malloc(sizeof(struct pcm_AlsaInfo));
if (*AlsaInfoPtr == NULL) {
// 内存分配失败处理
return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; // 使用更具体的错误码
}
}
else
{
JZsdk_Free(*AlsaInfoPtr);
// 分配内存
*AlsaInfoPtr = (struct pcm_AlsaInfo *)malloc(sizeof(struct pcm_AlsaInfo));
if (*AlsaInfoPtr == NULL) {
// 内存分配失败处理
return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; // 使用更具体的错误码
}
}
struct pcm_AlsaInfo *AlsaInfo = *AlsaInfoPtr;
//设置alsa参数
AlsaParamInit(AlsaInfo, IndexInfo->Target_SampleRate);
//创建pcm检测线程
pthread_t alsa_check_task;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
int tts_ret = pthread_create(&alsa_check_task,&task_attribute,Alsa_CheckThread,(void *)IndexInfo); //TTS线程
if(tts_ret != 0)
{
JZSDK_LOG_ERROR("创建pcm检测线程失败!\n");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_JZsdkReturnCode Pcm_AlsaPlay(struct AudioDealInfo *IndexInfo, unsigned char *buf, int num_samples)
{
struct pcm_AlsaInfo *AlsaInfo = (struct pcm_AlsaInfo *)IndexInfo->AlsaInfo;
//打开alsa播放标志
IndexInfo->AudioDeal_Alsa_Execute_Flag = JZ_FLAGCODE_ON;
int ret = 0;
//printf("num_samples: %d, g_period_size: %d\n", num_samples, (int)AlsaInfo->g_period_size);
//当输入的数据长度 》 每次写入音频设备时处理的帧数量384
int UnDeal_samples = num_samples;
// 无论哪种都是暂时不行
// //加入一个降噪
// if (1)
// {
// //降噪处理
// //把数据组成一个数组
// short TempPcm;
// for (int i = 0; i < num_samples; i+=2)
// {
// // TempPcm = (buf[i] & 0xFF) | ((short)buf[i+1] << 8);
// // TempPcm = PcmNoiseReduction(TempPcm);
// // buf[i] = TempPcm & 0xFF;
// // buf[i+1] = (TempPcm >> 8) & 0xFF;
// TempPcm = (buf[i+1] & 0xFF) | ((short)buf[i] << 8);
// TempPcm = PcmNoiseReduction(TempPcm);
// buf[i+1] = TempPcm & 0xFF;
// buf[i] = (TempPcm >> 8) & 0xFF;
// }
// }
while(UnDeal_samples > 0 && IndexInfo->AudioDeal_Alsa_Execute_Flag != JZ_FLAGCODE_OFF)
{
int dealSamples = 0;
if (UnDeal_samples >= AlsaInfo->g_period_size)
{
dealSamples = AlsaInfo->g_period_size;
UnDeal_samples = UnDeal_samples - dealSamples;
}
else
{
dealSamples = UnDeal_samples;
UnDeal_samples = UnDeal_samples - dealSamples;
}
//将数据写入alsa音频设备
ret = snd_pcm_writei(AlsaInfo->g_handle, (void*)buf, dealSamples);
//当前设备不可用
if(ret == -EAGAIN)
{
//等待一段时间后重试
printf("again");
snd_pcm_wait(AlsaInfo->g_handle, 1000);
}
//音频设备溢出错误
else if(ret == -EPIPE)
{
printf("snd_pcm_prepare 1\n");
//重置音频设备
snd_pcm_prepare(AlsaInfo->g_handle);
}
//音频设备需要挂起 忙时
else if(ret == -ESTRPIPE)
{
printf("Need suspend!\n");
}
//出现其他错误
else if(ret < 0)
{
printf("snd_pcm_writei error:%s\n", snd_strerror(ret));
}
//位移数据
//buf + 解码的帧数*单帧长度 //单帧长度 2个16bit长度的数据 即2*16/8 = 4字节
buf += dealSamples * FRAME_SIZE;
}
//等待播放完毕
AlsaWaitPlayEnd(AlsaInfo);
//暂停播放
while(AudioDeal_GetPauseAndContinuePlayStatus() == JZ_FLAGCODE_ON)
{
delayMs(1);
}
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**********
*
* 清空pcm数据
*
*
* ***********/
T_JZsdkReturnCode Alsa_DropPcm(struct AudioDealInfo *IndexInfo)
{
struct pcm_AlsaInfo *AlsaInfo = (struct pcm_AlsaInfo *)IndexInfo->AlsaInfo;
snd_pcm_drop(AlsaInfo->g_handle);
snd_pcm_prepare(AlsaInfo->g_handle);
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}