OpusFilePlay.c
14.4 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "JZsdkLib.h"
#include "../../Megaphone.h"
#include "../Megaphone_Opus.h"
#include "version_choose.h"
#include "BaseConfig.h"
#include "AudioDeal/AudioDeal.h"
#include "Megaphone/PcmAudio/PcmAudioFile.h"
#include "opus.h"
#define OPUS_MAX_FRAME_SIZE (6 * 960)
#define OPUS_CHANNELS (1)
#define OPUS_MAX_PACKET_SIZE (3 * 1276)
#define PCM_SAVE_FILE_NAME "/root/save.pcm" //用于保存的pcm
#define PCM_PLAY_FILE_NAME "/root/test_audio.pcm" //用于播放的pcm
static int g_opus_decodeBitrate = 16000; //解码码率
static int g_opus_decodeChannels = 1; //解码通道数
static int g_opus_sampleRate = 16000; //解码采样率
static int g_Opus_play_Flag = JZ_FLAGCODE_OFF; //opus的播放标志位
static int g_Opus_decode_Flag = JZ_FLAGCODE_OFF; //opus的解码标志位
static T_JZTaskHandle g_DecodeOpus_task = NULL;
static T_JZTaskHandle g_OpusPlay_task = NULL;
typedef struct OpusDecodeFile
{
unsigned char inputFilePath[JZSDK_FILE_PATH_SIZE_MAX];
unsigned char outputFilePath[JZSDK_FILE_PATH_SIZE_MAX];
int saveMode;
}OpusDecodeFile;
T_JZsdkReturnCode OpusFile_SetDecodeBitrate(int bitrate)
{
g_opus_decodeBitrate = bitrate;
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_JZsdkReturnCode OpusFile_SetDecodeChannels(int channels)
{
g_opus_decodeChannels = channels;
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_JZsdkReturnCode OpusFile_SetDecodeSampleRate(int sampleRate)
{
g_opus_sampleRate = sampleRate;
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/***************************************************************************************************************
*
* 播放部分
*
*
****************************************************************************************************************/
static T_JZsdkReturnCode FixedFilePlay()
{
FILE *PlayFixedFile_pcm_fp = NULL;
unsigned int nbBytes;
uint8_t cbits[3 * 1276];
int timeNum = 0;
int loop = JZ_FLAGCODE_OFF;
int loop_interval;
double total_duration = 0;
int last_printed_second = -1; // 上次打印的秒数,初始化为-1表示还没有打印过
if(g_Opus_decode_Flag != JZ_FLAGCODE_OFF)
{
JZSDK_LOG_ERROR("opus解码超时,无法播放");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
g_Opus_play_Flag = JZ_FLAGCODE_ON;
JZSDK_LOG_INFO("播放指定位置的opus pcm文件\n");
PlayFixedFile_pcm_fp = fopen(PCM_PLAY_FILE_NAME, "r");
if (PlayFixedFile_pcm_fp == NULL)
{
JZSDK_LOG_ERROR("打开固定opus pcm文件失败\n");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
// 将文件指针移动到文件开头
if (fseek(PlayFixedFile_pcm_fp, 0, SEEK_SET) != 0)
{
JZSDK_LOG_ERROR("Failed to move file pointer to the beginning of the file");
fclose(PlayFixedFile_pcm_fp);
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
while (Megaphone_MegDataGenFlag(JZ_FLAGCODE_GET, 0) == JZ_FLAGCODE_ON)
{
nbBytes = fread(cbits, 1, 3 * 1276, PlayFixedFile_pcm_fp);
if(nbBytes > 0)
{
AudioDeal_PcmDataInput(g_opus_sampleRate, (unsigned char *)cbits, nbBytes, 1);
}
int totalSamples = nbBytes / (2);
// 计算当前帧的持续时间(秒)
double frame_duration = (double)totalSamples / g_opus_sampleRate;
total_duration += frame_duration;
int current_second = (int)floor(total_duration); // 获取当前总时间的整数秒部分
if (current_second > last_printed_second)
{
printf("nb:%d rate:%d duration:%f total_duration:%f\n", totalSamples, g_opus_sampleRate, frame_duration, current_second);
//回传播放时间
Megaphone_PlayBackTime(current_second);
last_printed_second = current_second; // 更新上次打印的秒数
}
if (feof(PlayFixedFile_pcm_fp))
{
total_duration = 0;
last_printed_second = -1;
//如果循环播放没打开
Megaphone_param(JZ_FLAGCODE_GET, MEGAPHONE_LOOP, &loop);
Megaphone_param(JZ_FLAGCODE_GET, MEGAPHONE_LOOP_INTERVAL, &loop_interval);
if (loop == JZ_FLAGCODE_OFF)
{
timeNum = 0;
// //播放结束延迟
// while ((timeNum <=3000) && (Megaphone_MegDataGenFlag(JZ_FLAGCODE_GET, 0) == JZ_FLAGCODE_ON))
// {
// delayMs(10);
// timeNum +=10;
// }
break;
}
//如果循环播放打开
else if (loop == JZ_FLAGCODE_ON)
{
timeNum = 0;
JZSDK_LOG_INFO("循环播放opus");
// //播放结束延迟
// while ((loop == 1) && (Megaphone_MegDataGenFlag(JZ_FLAGCODE_GET, 0) == JZ_FLAGCODE_ON))
// {
// delayMs(10);
// timeNum +=10;
// }
//重置文件光标 继续播放
fseek(PlayFixedFile_pcm_fp, 0, SEEK_SET);
//循环播放延时
while (loop_interval > 0 && (loop == JZ_FLAGCODE_ON) && (Megaphone_MegDataGenFlag(JZ_FLAGCODE_GET, 0) == JZ_FLAGCODE_ON))
{
delayMs(50);
loop_interval -= 50;
}
continue;
}
}
}
fclose(PlayFixedFile_pcm_fp);
g_Opus_play_Flag = JZ_FLAGCODE_OFF;
}
/*
*
* 播放固定位置pcm线程
*
*/
static void *PlayFixedFile_task(void *arg)
{
//JZSDK_LOG_DEBUG("opus播放开始\n");
Megaphone_MegDataGenFlag(JZ_FLAGCODE_SET, JZ_FLAGCODE_ON);
Megaphone_MegDataGenFinshFlag(JZ_FLAGCODE_SET, JZ_FLAGCODE_ON);
//广播开始
Megaphone_status_Broadcast(AUDIO_PLAY_STATUS_OPUS_RECORD);
int amplifier = JZ_FLAGCODE_ON;
Megaphone_Amplifier_param(JZ_FLAGCODE_SET, &lifier);
FixedFilePlay();
amplifier = JZ_FLAGCODE_OFF;
Megaphone_Amplifier_param(JZ_FLAGCODE_SET, &lifier);
Megaphone_MegDataGenFlag(JZ_FLAGCODE_SET, JZ_FLAGCODE_OFF);
Megaphone_MegDataGenFinshFlag(JZ_FLAGCODE_SET, JZ_FLAGCODE_OFF);
//广播关闭
Megaphone_status_Broadcast(JZ_FLAGCODE_OFF);
//JZSDK_LOG_DEBUG("opus播放结束\n");
}
/*******************************
*
* 播放规定位置的opus文件
* 旧接口,无需定位传入的位置
*
************************************/
T_JZsdkReturnCode OpusFile_PlayFixedFile()
{
T_JZsdkOsalHandler *OsalHandler = JZsdk_Platform_GetOsalHandler();
OsalHandler->TaskCreate("PiayFilexdFile", PlayFixedFile_task, 8192, NULL, &g_DecodeOpus_task);
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/*************************************************************************************************************/
/*
解码函数
*/
static T_JZsdkReturnCode OpusDecodeFun(unsigned char *OpusFile, unsigned char *SaveFile)
{
FILE *fin = NULL;
FILE *fout = NULL;
OpusDecoder *decoder = NULL;
opus_int16 out[OPUS_MAX_FRAME_SIZE * OPUS_CHANNELS];
uint8_t cbits[OPUS_MAX_PACKET_SIZE];
int32_t nbBytes;
int32_t err;
if (g_Opus_decode_Flag != JZ_FLAGCODE_OFF)
{
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
g_Opus_decode_Flag = JZ_FLAGCODE_ON;
printf("Decode Start 解码开始\n"); //解码开始
//打开解码的opus文件
//TODO: need delete
//attention: you can use "ffmpeg -i xxx.mp3 -ar 16000 -ac 1 out.wav" and "./test_opus_tool out.wav out.opus" to generate opus file to test
fin = fopen(OpusFile, "r");
if (fin == NULL) {
JZSDK_LOG_ERROR("fin打开失败 :%s",OpusFile);
goto DecodeFinsh;
}
//创建一个解码器
decoder = opus_decoder_create(g_opus_sampleRate, OPUS_CHANNELS, &err);
if (err < 0) {
JZSDK_LOG_ERROR("解码器创建失败 %s",opus_strerror(err));
goto DecodeFinsh;
}
//创建一个opus输出文件
fout = fopen(SaveFile, "w");
if (fout == NULL) {
JZSDK_LOG_ERROR("创建opus输出文件失败");
goto DecodeFinsh;
}
printf("opus解码初始化完成\n");
int frame_size;
while (1)
{
int i;
opus_int16 TempPcm[OPUS_MAX_FRAME_SIZE * OPUS_CHANNELS];
frame_size = 0;
//读取opus内容
/* Read a 16 bits/sample audio frame. */
nbBytes = fread(cbits, 1, g_opus_decodeBitrate / 8000 * 40, fin);
if (feof(fin))
{
break;
}
printf("fread nbBytes=%d\n",nbBytes);
//printf("s_decodeBitrate=%d",s_decodeBitrate / OPUS_DECODE_BITRATE_8KBPS * OPUS_DECODE_FRAME_SIZE_8KBPS);
/* Decode the data. In this example, frame_size will be constant because
the encoder is using a constant frame size. However, that may not
be the case for all encoders, so the decoder must always check
the frame size returned. */
//向解码器输入opus内容
frame_size = opus_decode(decoder, cbits, nbBytes, out, OPUS_MAX_FRAME_SIZE, 0);
if (frame_size < 0) {
JZSDK_LOG_ERROR("decoder failed: %s",opus_strerror(frame_size));
goto DecodeFinsh;
}
//解码完成
printf("decode data to file: %d\r\n", frame_size * OPUS_CHANNELS);
/* Convert to little-endian ordering. */
for (i = 0; i < OPUS_CHANNELS * frame_size; i++) {
TempPcm [i] = out[i] & 0xFF | (out[i] >> 8) << 8;
//TempPcm[i] = PcmNoiseReduction(TempPcm[i]);
}
fwrite(TempPcm, sizeof(short), frame_size * OPUS_CHANNELS, fout);
}
DecodeFinsh:
//释放解码器
if (decoder != NULL)
{
opus_decoder_destroy(decoder);
printf("opus_decoder_destroy(decoder)\n");
}
//关闭文件
if (fin != NULL)
{
fclose(fin);
fin = NULL;
printf("fclose(fin);\n");
}
//关闭文件
if (fout != NULL)
{
fclose(fout);
fout = NULL;
printf("fclose(fout);\n");
}
if (frame_size < 0)
{
JZSDK_LOG_ERROR("opus解码失败");
g_Opus_decode_Flag = JZ_FLAGCODE_OFF;
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
//解码完成
JZSDK_LOG_INFO("Decode Finished...\n");
g_Opus_decode_Flag = JZ_FLAGCODE_OFF;
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/*******************
*
* opus解码线程
*
*******************/
static void *DecodeOpus_task(void *arg)
{
unsigned char *opusFile = (unsigned char *)arg;
T_JZsdkReturnCode ret = OpusDecodeFun(opusFile, PCM_PLAY_FILE_NAME);
if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
{
//广播解码失败
Megaphone_Broadcast_OpusDecodeStatus(1);
}
else
{
Megaphone_Broadcast_OpusDecodeStatus(0);
}
free(opusFile);
}
T_JZsdkReturnCode OpusFile_DecodeOpus(unsigned char *opusFile)
{
T_JZsdkOsalHandler *OsalHandler = JZsdk_Platform_GetOsalHandler();
T_JZsdkReturnCode ret = JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
//因为播放的流通pcm文件只有一份,所以播放时,不能进行解码,不然会导致相当多问题
int i = 0;
while (i <= 4 && g_Opus_play_Flag == JZ_FLAGCODE_ON)
{
i++;
delayMs(50);
}
//判断上一次播放是否结束 判断解码是否完成
if(i == 4)
{
JZSDK_LOG_ERROR("超时200ms,上一次opus录音播放未结束,无法解码");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
i = 0;
while (i<=4 && g_Opus_decode_Flag == JZ_FLAGCODE_ON)
{
i++;
delayMs(50);
}
if(i == 4)
{
JZSDK_LOG_ERROR("超时,上一次opus仍在解码,无法开始新的解码");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
unsigned char *DecodeFile = OsalHandler->Malloc(strlen(opusFile) + 1);
if(DecodeFile == NULL)
{
JZSDK_LOG_ERROR("DecodeFile malloc failed");
return JZ_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
memset(DecodeFile, 0, strlen(opusFile) + 1);
memcpy(DecodeFile, opusFile, strlen(opusFile));
OsalHandler->TaskCreate("DecodeOpus", DecodeOpus_task, 8192, (void *)DecodeFile, &g_DecodeOpus_task);
//JZSDK_LOG_INFO("decode %s %dto %s %d\n", opusFile, strlen(opusFile), DecodeFile, strlen(DecodeFile));
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/*******************
*
* opus解码线程
*
*******************/
static void *DecodeOpus_task1(void *arg)
{
OpusDecodeFile *opusDecodeInfo = (OpusDecodeFile *)arg;
T_JZsdkReturnCode ret = OpusDecodeFun(opusDecodeInfo->inputFilePath, PCM_SAVE_FILE_NAME);
if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
{
//广播解码失败
Megaphone_Broadcast_OpusDecodeStatus(1);
}
else
{
Megaphone_Broadcast_OpusDecodeStatus(0);
}
//如果存在存储情况
if (opusDecodeInfo->saveMode == 0x01)
{
//将解码得到的pcm文件存到指定路径
JZsdk_Osal_Rename(PCM_SAVE_FILE_NAME, opusDecodeInfo->outputFilePath);
//刷新pcm文件列表
PcmFile_FlushList();
}
free(opusDecodeInfo);
}
T_JZsdkReturnCode OpusFile_DecodeOpusAndSave(int SaveMode, unsigned char *InFile,unsigned char *OutFile)
{
T_JZsdkOsalHandler *OsalHandler = JZsdk_Platform_GetOsalHandler();
T_JZsdkReturnCode ret = JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
int i = 0;
while (i<=4 && g_Opus_decode_Flag == JZ_FLAGCODE_ON)
{
i++;
delayMs(50);
}
if(i == 4)
{
JZSDK_LOG_ERROR("超时,上一次opus仍在解码,无法开始新的解码");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
OpusDecodeFile *OpusDecodeFileInfo = (OpusDecodeFile *)OsalHandler->Malloc(sizeof(OpusDecodeFile));
if(OpusDecodeFileInfo == NULL)
{
JZSDK_LOG_ERROR("OpusDecodeFileInfo malloc failed");
return JZ_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
memset(OpusDecodeFileInfo, 0, sizeof(OpusDecodeFile));
OpusDecodeFileInfo->saveMode = SaveMode;
memcpy(OpusDecodeFileInfo->inputFilePath, InFile, strlen(InFile));
memcpy(OpusDecodeFileInfo->outputFilePath, OutFile, strlen(OutFile));
OsalHandler->TaskCreate("DecodeOpus", DecodeOpus_task1, 8192, (void *)OpusDecodeFileInfo, &g_DecodeOpus_task);
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}