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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "music.h"
#include "Interface.h"
#include "ircut.h"
#include "tts_sample.h"
#include "crc16.h"
#include "string.h"
#include <pthread.h>
#include <opus.h>
#include "JZ_widget.h"
#include "wiringPi.h"
#include "uav_widget.h"

int music_sum=0;//歌曲总数
int music_num=0;//当前播放的歌曲编号
char musiclist[128][128];//最多存取128首歌曲
int musicname_len[128];//每首歌曲名字长度

int record_sum=0;//歌曲总数
int record_num=0;//当前播放的录音编号
char recordlist[128][128];//最多存取128首录音
int record_len[128];//每首录音名字长度

int play_mode=0;//播放模式,play_mode==1单曲循环,play_mode==0单曲播放
int play_tts_flag=0;//播放TTS是2
int now_volume=55;//默认音量10--》50+10/2==55
int music_nowtime=0;
int music_time=0;

int set_volume_value=0;//音量值

uint8_t ttsdata[1024];//tts数据

int tts_tone=1;
int tts_volume=100;
int tts_speed=50;

uint8_t ttsdata_mobie[2048];//APP数据
int tts_tone_mobie=1;
int tts_volume_mobie=100;
int tts_speed_mobie=50;
int set_volume_flag=0;//默认是0
int tts_add=0;//默认是0,不追加
static int aplay_flag=0;//默认是0
static int TTS_flag=0;//默认是0
int Volume_protection_mode=1;
struct EQValues eq_values;
int WidgetPlayFlag = 1;

int TTS_delay = 2;//TTS播放后的延迟时间

int lock_play = 0;//0未锁,1锁住无法再调用播放

extern int reset_play;
//extern T_GduWidgetSpeakerState s_speakerState;
int now_play_stat = 0;//无播放 1播放音乐 2播放文本 3播放录音

static int speakerMode =0;//喊话模式 0:无播放 1:实时喊话 2:文本喊话 3:录音喊话
extern int playback_delay;//播放延时
extern char record_file_name[128];
extern int playStatusQueryFlag;
bool thread_created = false;//判断喊话线程是否已经创建


static void *TTS_flie(void *arg);
static void *TTS_flie_mobie(void *arg);
static void *TTS_first(void *arg);
static void *SET_volume(void *arg);
static void *Volume_protection(void *arg);

//没有这三函数也能正常使用 ?
extern void set_eq_rawplay(bool enable);
extern void set_eq_streamplay(bool enable);
extern void set_eq_fileplay(bool enable);

pthread_t TTS_flie_task;

// #define MAX_VOLUME			78
// #define MAX_TTS_VOLUME		82


void music_init(void)//初始化音乐
{
	pthread_t TTS_first_task;
	pthread_attr_t task_attribute; //线程属性
	pthread_attr_init(&task_attribute);  //初始化线程属性
	pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED);      //设置线程属性
    //滤波器初始化
	struct EQValues eq_values;	  

    //道通U3S    
     
	// eq_values.GAIN_31Hz=-48; //31~125提高听起来没这么响       
	// eq_values.GAIN_62Hz=-48;
	// eq_values.GAIN_125Hz= -48; //
	// eq_values.GAIN_250Hz= -24; //   250~500Hz  -32:感觉在呐喊
	// eq_values.GAIN_500Hz= -24;                            //500Hz-2kH,z的人声区域    
	// eq_values.GAIN_1KHz=  -20;             
	// eq_values.GAIN_2KHz=  -20;    
	// eq_values.GAIN_4KHz= -16;           //4kHz以上高频杂音尖锐噪声   -8的话听起来响,-16还行
	// eq_values.GAIN_8KHz= -16;   
    // eq_values.GAIN_16KHz= -16;
	
   
	eq_values.GAIN_31Hz=-48; //31~125提高听起来没这么响       
	eq_values.GAIN_62Hz=-48;
	eq_values.GAIN_125Hz= -48; //
	eq_values.GAIN_250Hz= -24; //   250~500Hz  -32:感觉在呐喊
	eq_values.GAIN_500Hz= -24;                            //500Hz-2kH,z的人声区域    
	eq_values.GAIN_1KHz=  -16;             
	eq_values.GAIN_2KHz=  -16;    
	eq_values.GAIN_4KHz= 0;           //4kHz以上高频杂音尖锐噪声   -8的话听起来响,-16还行
	eq_values.GAIN_8KHz= 0;   
    eq_values.GAIN_16KHz= 0;

 
	//set_amplifier(1);//开启功放
	printf("初始化音乐准备\n");
	
	init(MP2,eq_values,returnframe);//初始化
	set_eq_rawplay(true); 
    set_eq_streamplay(true);
    set_eq_fileplay(true);
	delay(50);
	get_music_list();//查询列表
	music_num=0;//当前播放为第一首歌
	set_volume(30);//初始音量100
	char start_tip[]="喊话器启动。";
	
	//TTS(1,50,50,sizeof(start_tip),start_tip);
	int tts_ret = pthread_create(&TTS_first_task,&task_attribute,TTS_first,NULL);		//TTS mobie
	if(tts_ret != 0)
	{
		printf("创建TTS_first线程失败!\n");
		return;
	}
	 
}

void get_music_list(void)//刷新音乐列表 
{	
	for(int i=0;i<music_sum;i++){
		memset(musiclist[i],0,128);
	}//清空数组
	music_sum=0;//歌曲总数
	char readlist[11]={0x5a,0x5a,0x77,0x00,0x0c,0x01,0x00,0x53,0x02,0x1b,0x13};
	sendFrame(readlist,11);//调用查询
	printf("刷新列表\n");
}

void get_recordlist(void){
	for(int i=0;i<record_sum;i++){
		memset(recordlist[i],0,128);
	}//清空数组
	record_sum=0;//歌曲总数
	char readlist[11]={0x5a,0x5a,0x77,0x00,0x0c,0x01,0x00,0x53,0x03,0x1b,0x13};
	sendFrame(readlist,11);//调用查询
	printf("查询录音开始\n");
}

void play_music(char* music_name,int datasize)//播放指定音乐,music_name文件名
{
	Opus_RealTimeSpeak_AbnormalInterrupt(); //喊话异常中断
	//停止-》重播
	/*if(now_play_stat==1){
		stop_music();
		delay(200);
	}
	if(now_play_stat==3){
		stop_music();
		delay(200);
	}
	if(now_play_stat==2){
		stop_TTS_paly();
		delay(200);
	}*/
	play_tts_flag = 1;
	set_paly_state(1);//设置为音乐播放
	uint8_t playdata[128]={0x5a,0x5a,0x77,0x00,0x00,0x01,0x00,0x52,0x02};
	memcpy(&playdata[9],music_name,datasize);
	playdata[4]=(uint8_t)(11+datasize);//补全长度位

	set_amplifier(1);//打开功放


	for(int i=0;i<music_sum;i++){
		if(strcmp(music_name, musiclist[i]) == 0){
			printf("歌曲%d相同,歌曲名:%s\n",i,musiclist[i]);
			music_num=i;
			sendFrame(playdata,9+datasize);//播放指定歌曲
			Jz_set_widget_value(2,0);
			return;
		}	
		if(now_volume>50){
			Protection(1);//音量大于五十执行保护
		}
	}
	printf("播放错误\n");
	return;
}

void play_record(char* record_name,int datasize)//播放指定录音,record_name文件名
{
	reset_play = 0;//播放音乐重播
		//停止-》重播
	if(now_play_stat==1){
		stop_music();
		delay(200);
	}
	if(now_play_stat==3){
		stop_music();
		delay(200);
	}
	if(now_play_stat==2){
		stop_TTS_paly();
		delay(200);
	}
	Opus_RealTimeSpeak_AbnormalInterrupt(); //喊话异常中断
	play_tts_flag = 3;
	set_paly_state(3);//设置为录音播放
	uint8_t playdata[128]={0x5a,0x5a,0x77,0x00,0x00,0x01,0x00,0x52,0x03};
	memcpy(&playdata[9],record_name,datasize);
	playdata[4]=(uint8_t)(11+datasize);//补全长度位

	set_amplifier(1);//打开功放
	sendFrame(playdata,9+datasize);//播放指定歌曲
	Jz_set_widget_value(2,0);
	
	return;
}

void play_TTS(char* music_name[],int datasize)//播放指定音乐,music_name文件名
{
	uint8_t playdata[128]={0x5a,0x5a,0x77,0x00,0x00,0x01,0x00,0x52,0x03};
	memcpy(&playdata[9],music_name,datasize);
	playdata[4]=(uint8_t)(11+datasize);//补全长度位
	set_amplifier(1);//打开功放
	if(now_volume>50){
		Protection(1);//音量大于五十执行保护
	}
	sendFrame(playdata,9+datasize);//播放指定歌曲

}

void stop_music(void)//暂停音乐播放
{   
	
	Opus_RealTimeSpeak_AbnormalInterrupt(); //喊话异常中断		
	 
	if(now_play_stat == 1||now_play_stat == 3){
		set_amplifier(0);//关闭功放
		uint8_t playdata[]={0x5a,0x5a,0x77,0x00,0x0b,0x01,0x00,0x52,0x05,0x49,0x53};
		sendFrame(playdata,11);//停止播放
		set_paly_state(0);
		printf("stop music\n");
	}
	else if(now_play_stat == 2){
		printf("stop music-TTS\n");
		stop_TTS_paly();//停止播放
	}
}

void continue_music(void)//继续音乐播放
{
	Opus_RealTimeSpeak_AbnormalInterrupt(); //喊话异常中断
	
	char pusedata[]={0x5a,0x5a,0x77,0x00,0x0b,0x01,0x00,0x52,0x04,0x89,0x92};
	sendFrame(pusedata,11);//播放功能

	set_amplifier(1);//打开功放

	if(now_volume>50){
		Protection(1);//音量大于五十执行保护
	}
	Jz_set_widget_value(2,0);
}

void last_music(void)//上一曲
{
	stop_music();
	set_amplifier(0);//关闭功放
	if(music_num==0){
		music_num=music_sum-1;//第一首的上一曲为最后一首
	}
	else{
		music_num-=1;
	}
	play_music(musiclist[music_num],musicname_len[music_num]);
}

void next_music(void)//下一曲
{
	stop_music();
	set_amplifier(0);//关闭功放
	if(music_num==music_sum-1){
		music_num=0;//最后一首的下一曲为第一首
	}
	else{
		music_num+=1;
	}
	play_music(musiclist[music_num],musicname_len[music_num]);
}

void set_mode(int mode)//设置播放模式,mode==0单曲播放。mode==1单曲循环
{
	if(mode==1)
	{
		play_mode=1;
		printf("打开循环模式\n");
		Jz_set_widget_value(11,1);
	}
	else{
		play_mode=0;
		printf("关闭循环模式\n");
		Jz_set_widget_value(11,0);
	}
}

void set_volume(int value)//设置音量,音量0-100,默认音量value==10。
{
	pthread_t SET_volume_task;
	pthread_attr_t task_attribute; //线程属性
	pthread_attr_init(&task_attribute);  //初始化线程属性
	pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED);      //设置线程属性
	printf("传入的值:%d\n",value);
	//now_volume=50+value/2;//获取当前音量
	now_volume=value;//获取当前音量	

	Jz_set_widget_value(3,value);//音量同步
	Jz_set_widget_value(7,value);//音量同步

	//printf("now_volume:%d",now_volume);
	//0~100转换为0~63
	if(value< 0){
		value = 0;
	}
	else if(value>100)
	{
		value = 91*255/100;
	}
	else
	{   
		value = value*91/100*255/100;//实际255
	}

	set_volume_value = value;
   
	if(set_volume_flag==0){
		int volume_ret = pthread_create(&SET_volume_task,&task_attribute,SET_volume,NULL);		//TTS线程
		if(volume_ret != 0)
		{
			printf("创建volume线程失败!\n");
			return;
		}
	}
	//printf("设置音量为:%d",value);
}

void set_ture_volume(int value)//设置绝对音量,绝对音量0-100无转换
{   
	pthread_t SET_volume_task;
	pthread_attr_t task_attribute; //线程属性
	pthread_attr_init(&task_attribute);  //初始化线程属性
	pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED);      //设置线程属性
	if(value<=0){
		value=0;
	}
	else if(value>=100){
		value=93*255/100;
	}
	else if(value>0&&value<100){
		value=value*93/100*255/100;
	}
	set_volume_value = value;
	if(set_volume_flag==0){
		int volume_ret = pthread_create(&SET_volume_task,&task_attribute,SET_volume,NULL);		//TTS线程
		if(volume_ret != 0)
		{
			printf("创建volume线程失败!\n");
			return;
		}
	}
}

void Protection(int mode){	
	Volume_protection_mode=mode;
	pthread_t volume_Protection_task;
	pthread_attr_t task_attribute; //线程属性
	pthread_attr_init(&task_attribute);  //初始化线程属性
	pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED);      //设置线程属性
	/*int volume_Protection = pthread_create(&volume_Protection_task,&task_attribute,Volume_protection,NULL);		//线程
		if(volume_Protection != 0)
		{
			printf("创建Protection线程失败!\n");
			return;
		}*/
}

void TTS(int name,int volume,int speed,int len,char *data)//文本播放UTF-8,name发音人默认name==1;volume合成音量默认合成音量100;speed语速默认speed==50;argv文本内容。
{ 
	
	pthread_attr_t task_attribute; //线程属性
	pthread_attr_init(&task_attribute);  //初始化线程属性
	pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED);      //设置线程属性
	Opus_RealTimeSpeak_AbnormalInterrupt(); //喊话异常中断
	//停止-》重播
	if(now_play_stat==1){
		stop_music();
	}
	if(now_play_stat==3){
		stop_music();
	}
	// if(now_play_stat==2){
	// 	stop_TTS_paly();
	// }
	if(GetTtsStatus() ==0){
		set_paly_state(2);//设置为文本播放
		tts_tone=name;		
		tts_speed=speed;
		memset(ttsdata,0,sizeof(ttsdata));//清空数组 
		/*memcpy(ttsdata,data,len);//复制文本内容
		ttsdata[len]=0xe3;
		ttsdata[len+1]=0x80;
		ttsdata[len+2]=0x82;*/
		snprintf(ttsdata,len+8,"..%s..",data);
		set_ture_volume(now_volume);
		if(len<20){
			TTS_delay=2;
		}
		else{
			TTS_delay=len/10;
		}
		if(play_mode == 1)
		{
			TTS_delay += playback_delay;
		}
        

		int tts_ret = pthread_create(&TTS_flie_task,&task_attribute,TTS_flie,NULL);		//TTS线程
		if(tts_ret != 0)
		{  SetTtsStatus(0);
			printf("创建TTS线程失败!\n");
			return;
		}
	}
	else
	{
		//TTS正在播放中
		playStatusQueryFlag = 1;
		
	}
}


void TTS_mobie(int len,uint8_t* data)//文本播放Unicode
{
        char get_data[2048];
		pthread_t TTS_flie_mobie_task;
		pthread_attr_t task_attribute; //线程属性
		pthread_attr_init(&task_attribute);  //初始化线程属性
		pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED);      //设置线程属性		
		memcpy(get_data,data,len);
        switch(get_data[4]){//发音人
		case 0x33:
                    tts_tone_mobie=1;
					printf("发音人选择1\n");
                    break;
		default:
                    tts_tone_mobie=2;
					printf("发音人选择2\n");
                    printf("get_data[4]=%x",get_data[4]);
                    break;
		}
		int get_i=0;
        if(get_data[4]!=0x33){
			get_i+=2;
		}
		//tts_volume_mobie=((data[get_i+12]-48)*10);//获取音量
        if(get_data[get_i+12]==0x31&&get_data[get_i+14]==0x30){//音量
			//tts_volume_mobie=100;
			get_i+=2;
		}
        tts_speed_mobie=(get_data[get_i+20]-48)*10;//设置速度
        if(get_data[get_i+20]==0x31&&get_data[get_i+22]==0x30){
			get_i+=2;
			tts_speed_mobie=100;
		}
		printf("tts_speed_mobie:%d\n",tts_speed_mobie);
        //memcpy(ttsdata_mobie,&get_data[get_i+40],len-(get_i+40));//复制文本内容
		memset(ttsdata_mobie,0,sizeof(ttsdata_mobie));//复制文本内容
		for(int i=0;i<len-(get_i+40);i++){
			ttsdata_mobie[i]=get_data[i+get_i+40];
			printf("ttsdata_mobie[%d]=%x \n",i,ttsdata_mobie[i]);
		}
		ttsdata_mobie[len-(get_i+40)]=0x02;
		ttsdata_mobie[len-(get_i+40)+1]=0x30;
		printf("ttsdata_mobie = %s \n",ttsdata_mobie);
		printf("TTS线程\n");
		int tts_ret = pthread_create(&TTS_flie_mobie_task,&task_attribute,TTS_flie_mobie,NULL);		//TTS mobie
		if(tts_ret != 0)
		{
			printf("创建TTS_mobie线程失败!\n");
			return;
		}
}

void returnframe(char* data, int datasize)//回调函数,监听播放状态
{
	printf("returnframe:");
	for(int i=0;i<datasize;i++){
			printf("%x ",data[i]);
		}
	printf("\n");
	if(data[6]==0x41){//查询列表返回音频文件	
		musicname_len[music_sum]=datasize-10;
		memcpy(musiclist[music_sum],&data[7],datasize-10);
		printf("mp3: %s   datasize:%d\n",musiclist[music_sum],musicname_len[music_sum]);
		music_sum+=1;
	}
	if(data[6]==0x42){//录音文件	
		record_len[record_sum]=datasize-10;
		memcpy(recordlist[record_sum],&data[7],datasize-10);
		printf("record: %s   datasize:%d\n",recordlist[record_sum],record_len[record_sum]);
		record_sum+=1;
	}
	if(data[6]==0x50){//开始播放获取音频总时长
                music_time=0;
		for(int i=7;data[i]!=0;i++){
			music_time=music_time*16+data[i];
		}
		printf("总时长:%d\n",music_time);
	}
	if(data[6]==0x51){//当前播放时长
                music_nowtime=0;
		for(int i=7;data[i]!=0;i++){
			music_nowtime=music_nowtime*16+data[i];
		}
		printf("当前时长:%d\n",music_nowtime);
	}
	if(data[6]==0xff){//播放结束
		printf("播放结束\n");
		if(music_nowtime>=music_time-1){
			printf("播放整首歌结束\n");
			if(play_mode==1){
				if(play_tts_flag==1){
					play_music(musiclist[music_num],musicname_len[music_num]);
				}
				else if(play_tts_flag==2){
					char tts_name[]="2.wav";
					play_TTS(tts_name,sizeof(tts_name));//再次播放
				}
				else if(play_tts_flag==3){
					char recordmp3name[]="record.mp3";
					play_record(recordmp3name,sizeof(recordmp3name));
				}
			}
			else{
				if(play_tts_flag==1){
					play_tts_flag=0;
				}
				/*E_DjiChannelAddress channelAddress;
				channelAddress = DJI_CHANNEL_ADDRESS_MASTER_RC_APP;
				DjiLowSpeedDataChannel_SendData(channelAddress,data,sizeof(data));
				channelAddress = DJI_CHANNEL_ADDRESS_EXTENSION_PORT;
				DjiLowSpeedDataChannel_SendData(channelAddress,data,sizeof(data));	
				reset_play=0;*/
				delay(1000);
				printf("music end\n");
				set_paly_state(0);
			}
		}
	}
	if(data[6]==0x43){//当前播放状态
		char get_now_start[datasize-7];
		if(data[7]==1){
			printf("正在播放\n");
		}
		else if(data[7]==2){
			printf("正在暂停\n");
		}
		if(data[9]==0){
			printf("音乐列表\n");
		}
		else{
			printf("录音列表\n");
		}
		printf("当前音量%x\n",data[11]);
		for(int i=0;i<datasize-13;i++){
			get_now_start[i]=data[i+13];
		}
		printf("当前歌曲:%s\n",get_now_start);
	}
}

void send_quality(char* data,int datasize)//转发数据,用于直接转发数据至库
{
	sendFrame(data,datasize);
}

//TTS合成线程
void *TTS_first(void *arg)
{  
	delay(500);
	set_amplifier(1);
	first_tts(tts_tone,tts_volume,tts_speed,"喊话器准备就绪。");//生成文件
	
	set_amplifier(0);
	
}

//TTS合成线程
void *TTS_flie(void *arg)
{  
	
	SetTtsStatus(1);
	set_amplifier(1);
	
	play_tts(tts_tone,tts_volume,tts_speed,ttsdata);//生成文件
	
	set_amplifier(0);
	SetTtsStatus(0);
	set_volume(now_volume);//恢复音量
	delay(500);
}

//TTS_mobie合成线程
void *TTS_flie_mobie(void *arg)
{
	play_tts_mobie(tts_tone_mobie,tts_volume_mobie,tts_speed_mobie,ttsdata_mobie);//生成文件
}

void *SET_volume(void *arg){
	set_volume_flag=1;
	char cmdBuffer[128];
	snprintf(cmdBuffer,128, "amixer sset -c 0 'Headphone' %d unmute",set_volume_value);
	//snprintf(cmdBuffer,128, "amixer -c 0 sset 'Master',0 %d\% unmute ",set_volume_value);
	printf("音量为%s\n",cmdBuffer);
	Test_RunSystemCmd(cmdBuffer);
	printf("线程设置音量为:%d\n",set_volume_value);
	set_volume_flag=0;
}

void *Volume_protection(void *arg){//音量保护线程
	int get_volume=now_volume;
	if(Volume_protection_mode==2)
	{
	for(int i=50;i<get_volume;i+=10){
		set_volume(i);
		delay(100);//慢慢上升
	}
	}
	else if(Volume_protection_mode==1)
	{
	for(int i=50;i<get_volume;i+=5){
		set_volume(i);
		delay(300);//慢慢上升
	}
	}
	set_volume(get_volume);	
}


void Set_WidgePlayFlag(int flag)
{
    WidgetPlayFlag = flag;
}

int aplay_test(void){
	FILE *fn;
	int32_t nbBytes;
	uint8_t cbits[3 * 1276];
	SetRecordStatus(1);
	stop_music();//停止播放
	
	set_amplifier(1);//打开功放
	
	fn = fopen("/root/test_audio.pcm", "r");
	if (fn == NULL) {
        fprintf(stderr,"open file error\n");
		
        return EXIT_FAILURE;
    }

	while(WidgetPlayFlag){
		nbBytes = fread(cbits, 1, 3 * 1276, fn);
		if(nbBytes > 0){
			rawPlay(16000,cbits,nbBytes);//播放合成的音频文件
			
		}
		if (feof(fn))
		{
			break;
		}
	}
	WidgetPlayFlag = 1;
	fclose(fn);

	//rawplay 播放 播放完后关闭功放
	delay(100);

	SetRecordStatus(0);
	set_amplifier(0);//关闭功放
	printf("录音播放已关闭功放\n");

}




//实时喊话
#define WIDGET_SPEAKER_AUDIO_OPUS_MAX_FRAME_SIZE     (6 * 960)
#define WIDGET_SPEAKER_AUDIO_OPUS_SAMPLE_RATE        (16000)
#define WIDGET_SPEAKER_AUDIO_OPUS_CHANNELS           (1)

OpusDecoder *decoder;
uint8_t TTS_RealTimeSpeak_Loop_Data[ (128 + 1) ][80];//缓存池
int TTS_RealTimeSpeak_Loop_Head=0;				//池数头
int TTS_RealTimeSpeak_Loop_End=0;				//池数尾
int TTS_RTS_LoopResidue_Lenth; // 实时喊话池子残余物长度

int read_data_task_flag=0;//读取标志位0为停止,1为开始
int ReadTimeSpeak_Mode = 0; //实时喊话是否有打开

int Opus_play(int8_t *cbits,int32_t len){//解码opus并播放
    
	int i;
    char pcm_bytes[WIDGET_SPEAKER_AUDIO_OPUS_MAX_FRAME_SIZE * WIDGET_SPEAKER_AUDIO_OPUS_CHANNELS * 2 ];
    int frame_size;
	opus_int16 out[WIDGET_SPEAKER_AUDIO_OPUS_MAX_FRAME_SIZE * WIDGET_SPEAKER_AUDIO_OPUS_CHANNELS ];
	
	frame_size = opus_decode(decoder, cbits, len, out, WIDGET_SPEAKER_AUDIO_OPUS_MAX_FRAME_SIZE, 0);		//解码
    if (frame_size < 0) {
        fprintf(stderr, "decoder failed: %s\n", opus_strerror(frame_size));	
		Deinit_opus();
        return EXIT_FAILURE;
    }
	
	for (i = 0; i < WIDGET_SPEAKER_AUDIO_OPUS_CHANNELS * frame_size; i++) {									//转换		
            pcm_bytes[2 * i] = out[i] & 0xFF;
            pcm_bytes[2 * i + 1] = (out[i] >> 8) & 0xFF;
    }
	
	rawPlay(16000,pcm_bytes,frame_size*2);//播放音频
	
	return 1;
}

int Init_opus(){//初始化opus解码器
	int32_t err;

	//开始实时喊话模式
	ReadTimeSpeak_Mode = 1; 

	//1、创建opus解码器
	decoder = opus_decoder_create(WIDGET_SPEAKER_AUDIO_OPUS_SAMPLE_RATE, WIDGET_SPEAKER_AUDIO_OPUS_CHANNELS, &err);
    if (err < 0) {
        fprintf(stderr, "failed to create decoder: %s\n", opus_strerror(err));
		printf("opus解码器创建err\n");
        return -1;
    }

	//2、打开功放
	set_amplifier(1);
	clear_loop();//清空数据池

	//3、创建循环播放线程
	pthread_t loop_play_task,pcm_loop_play_task;
	pthread_attr_t task_attribute; //线程属性
	pthread_attr_init(&task_attribute);  //初始化线程属性
	pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED);      //设置线程分离属性
	int opus_Protection = pthread_create(&loop_play_task,&task_attribute,loop_play,NULL);		//线程
	if(opus_Protection != 0)
	{
		printf("创建自动解码线程失败!\n");
		return -2;
	}
	printf("init opus succes!\n");

	return 0 ;
}



void Deinit_opus(){//关闭opus解码器
	
	//如果喊话模式没被打开,就没必要跑了
	if(ReadTimeSpeak_Mode == 0)
	{   
		return;
	} 
	printf("\t关闭opus解码器\n");
	read_data_task_flag=0;//关闭数据池自读
	delay(500);
	set_amplifier(0);//关闭功放
	
	//将数据池的残余数据清0
	TTS_RTS_LoopResidue_Lenth = 0;

	opus_decoder_destroy(decoder);

	//关闭文本讲话模式
	ReadTimeSpeak_Mode = 0;
}

// 池子尾部满了,重置池尾
static void TTS_RealTimeSpeak_LoopEndReset()
{
	if (TTS_RealTimeSpeak_Loop_End >= 128)
	{
		TTS_RealTimeSpeak_Loop_End = 0;
	}
}

// 池子头部到底了,重置池头
static void TTS_RealTimeSpeak_LoopHeadReset()
{
	if (TTS_RealTimeSpeak_Loop_Head >= 128)
	{
		TTS_RealTimeSpeak_Loop_Head = 0;
	}
}

//TTS_RealTimeSpeak_Loop_Data
//数据池
void write_loop(uint8_t *buf,int length)//写入数据进缓存池
{

	int temp_lenth = 0;  //临时长度

	//写入处理
	//① 、检查池子当前组内有无剩余数据,如果池子当前组有剩  上次写入的语音数据, 接着往里面写入
	if (TTS_RTS_LoopResidue_Lenth) //如果残留物有长度
	{
		//数据进池,每组80数据
		for(  ; TTS_RTS_LoopResidue_Lenth < 80 ; TTS_RTS_LoopResidue_Lenth ++)
		{
			TTS_RealTimeSpeak_Loop_Data [TTS_RealTimeSpeak_Loop_End] [TTS_RTS_LoopResidue_Lenth] = buf [temp_lenth];
			temp_lenth ++;
		}
		TTS_RealTimeSpeak_Loop_End+=1;    //池尾后移一组
		TTS_RTS_LoopResidue_Lenth = 0;       //残留物长度归0
	}

	TTS_RealTimeSpeak_LoopEndReset(); //池满判断

	//②、从新的组处理数据
	//待处理数据超过80长度时
	while( (length - temp_lenth) >= 80)
	{
		//数据进池,每组80数据
		for(int ucLen = 0 ; ucLen < 80 ; ucLen ++)
		{
			TTS_RealTimeSpeak_Loop_Data [TTS_RealTimeSpeak_Loop_End] [ucLen] = buf [temp_lenth];
			temp_lenth ++;
		}
		TTS_RealTimeSpeak_Loop_End+=1;  //池尾后移一组
	}

	TTS_RealTimeSpeak_LoopEndReset(); //池满判断

	//③、如果长度还有剩,把数据存到池子内的新组,并记录残留物长度
	if( (length - temp_lenth) > 0)
	{
		for (   ; temp_lenth < length ; TTS_RTS_LoopResidue_Lenth++)
		{
			TTS_RealTimeSpeak_Loop_Data [TTS_RealTimeSpeak_Loop_End] [TTS_RTS_LoopResidue_Lenth] = buf [temp_lenth];
			temp_lenth ++;
		}	
	}

	TTS_RealTimeSpeak_LoopEndReset(); //池满判断

}

int read_loop(uint8_t *buf)//读取数据出缓存池
{
	int ucLen;



	//头尾一致为空
	if(TTS_RealTimeSpeak_Loop_Head==TTS_RealTimeSpeak_Loop_End)
	{
		fprintf(stderr,"TTS_RealTimeSpeak_Loop_Head==TTS_RealTimeSpeak_Loop_End");
		return -1;
	}

	//从池子中取出数据,一组数据为80
	for(ucLen=0 ; ucLen< 80 ; ucLen++) //数据进池
	{
		buf[ucLen] = TTS_RealTimeSpeak_Loop_Data[TTS_RealTimeSpeak_Loop_Head][ucLen];
	}
	TTS_RealTimeSpeak_Loop_Head+=1;

	//池头到底时,需要重置池头
	 TTS_RealTimeSpeak_LoopHeadReset();

	return ucLen;
}

void clear_loop()//清空缓存池
{
	TTS_RealTimeSpeak_Loop_Head=0;
	TTS_RealTimeSpeak_Loop_End=0;
}

static int Opus_PlayDeal(int datahead)
{
	int data_len = 0;                   //解码的数据
	int data_haveread = 0;  //已解码的音频长度
	unsigned int  get_TTS_RealTimeSpeak_Loop_Data[1024];	//数据池自取

	data_len=read_loop(get_TTS_RealTimeSpeak_Loop_Data);
	printf("读取%d Opus_play处理   data=%d\n",datahead,data_len);

	if(data_len == 0)
	{
		return -1;
	}

	if (data_len ==80)
	{
		//数据等于80时,说明读取数据成功,向解码器传递80数据
		Opus_play(&get_TTS_RealTimeSpeak_Loop_Data[data_haveread],80);
	}
	
	return 0;
}

void *loop_play(void *arg)
{

	read_data_task_flag=1;
	int ret;
	printf("loopplay线程建立\n");
	
	sleep(1);
	while(read_data_task_flag!=0){
		if(TTS_RealTimeSpeak_Loop_Head!=TTS_RealTimeSpeak_Loop_End)
		{
			ret = Opus_PlayDeal(TTS_RealTimeSpeak_Loop_Head);
			if(ret < 0)
			{
				printf("读取data为空\n");
			}
		}
		delay(10);
	}
}

/*
	Opus 实时语音异常中断
	客户在打开实时语音下,忘记关闭,进行其他操作,需要及时关闭语音,并清除内存
*/
int Opus_RealTimeSpeak_AbnormalInterrupt()
{
	//如果喊话模式没被打开,就没必要跑了
	if(ReadTimeSpeak_Mode == 0)
	{
		return;
	} 

	printf("\t实时语音异常中断\n");

	//1、关闭功放
	set_amplifier(0);

	//2、清空opus的数据池
	clear_loop();

	//3、释放解码器
	Deinit_opus();
}

void set_paly_state(int play_state){//设置播放状态
	if(play_state == 0){//无播放
		now_play_stat = 0 ;
		lock_play = 0; //解锁
		Jz_set_widget_value(2,1);//按钮恢复▶
		//SetSpeakerState(UAV_WIDGET_SPEAKER_STATE_IDEL);
		printf("设置无播放\n");
	}
	else if(play_state == 1){//音乐播放
		now_play_stat = 1 ;
		lock_play = 1; //锁住
		Jz_set_widget_value(2,0);//按钮恢复⏸
		//SetSpeakerState(UAV_WIDGET_SPEAKER_STATE_PLAYING);
		printf("设置播放音乐\n");
	}
	else if(play_state == 2){//文本播放
		now_play_stat = 2 ;
		lock_play = 1; //锁住
		Jz_set_widget_value(2,0);//按钮恢复⏸
		//SetSpeakerState(UAV_WIDGET_SPEAKER_STATE_PLAYING);
		printf("设置播放文本\n");
	}
	else if(play_state == 3){//录音播放
		now_play_stat = 3 ;
		lock_play = 1; //锁住
		Jz_set_widget_value(2,0);//按钮恢复⏸
		//SetSpeakerState(UAV_WIDGET_SPEAKER_STATE_PLAYING);
		printf("设置播放录音\n");
	}
	
}


//道通录音播放
void JZ_play_record(char* record_name)
{  
    if(GetRecordStatus() == 1)
	{
		return;
	}

    FILE *fn;
	int32_t nbBytes;
	uint8_t cbits[3 * 1276];
	SetRecordStatus(1);
	stop_music();//停止播放
	set_paly_state(3);//设置为录音播放
	set_amplifier(1);//打开功放
	
	fn = fopen(record_name, "r");
	if (fn == NULL) {
        fprintf(stderr,"open file error\n");
		
        return EXIT_FAILURE;
    }
	WidgetPlayFlag = 1;
	while(WidgetPlayFlag){
		
		nbBytes = fread(cbits, 1, 3 * 1276, fn);		
		if(nbBytes > 0 ){
			rawPlay(16000,cbits,nbBytes);//播放合成的音频文件			
		}
		
		if (feof(fn))
		{
			break;
		}
	}	
	fclose(fn);

	//rawplay 播放 播放完后关闭功放		
	delay(3000);			

	set_amplifier(0);//关闭功放
	SetRecordStatus(0);
	printf("录音播放已关闭功放\n");

}

//循环播放线程
static void *JZ_loop_playback_Task(void *arg)
{   
	if(play_mode == 0)//无循环且没有在录音播放
	{  
		char play_name[128];
		sprintf(play_name,"record/%s.pcm",record_file_name);                  	
		JZ_play_record(play_name);
		if(play_mode == 1)//播放时切换循环模式
		{
		  delay(playback_delay*1000);
		}
	}
	while(play_mode == 1)//循环播放
	{				
		if(GetSpeakerMode() == 3 )//录音喊话
		{   			
			char play_name[128];
			sprintf(play_name,"record/%s.pcm",record_file_name);
			JZ_play_record(play_name);
		}
		else
		{
			break;
		}	
		delay(playback_delay*1000);			
	}
	thread_created = false;
}

/************************************
*录音喊话线程创建 
*函数名:JZ_loop_playback_create
*函数参数:
         
*返回值:成功 0 失败-1
*函数作者:wzy
*************************************/
int JZ_loop_playback_create(void)
{
	pthread_t thread;
    pthread_attr_t task_attr;    
    int delay_flag = 0;
	if(thread_created == true)
	{   
		printf("\t线程已创建\n");
		return -1;
	}
	while (GetRecordStatus() == 1 || GetTtsStatus() == 1)
	{   
		delay_flag = 1;
		delay(100);
	}
	delay(900);	

	if(GetSpeakerMode() != 3)
	{
		return -1;
	}	
	pthread_attr_init(&task_attr);  //初始化线程属性
	pthread_attr_setdetachstate(&task_attr, PTHREAD_CREATE_DETACHED);      //设置线程分离属性
    // 创建线程
    int result = pthread_create(&thread, &task_attr, JZ_loop_playback_Task,NULL);
    if (result != 0) {
        printf("线程创建失败\n");
        return -1;
    }
	thread_created = true;
	return 0;
}
/************************************
*设置喊话器模式
*函数名:SetSpeakerMode
*函数参数:
         @mode: 0:无播放 1:实时喊话 2:文本喊话 3:录音喊话
*返回值:
*函数作者:wzy
*************************************/
void SetSpeakerMode(uint8_t mode)
{
	speakerMode = mode;
}
/************************************
*获取喊话器模式
*函数名:GetSpeakerMode
*函数参数:
         
*返回值:
		0:无播放 1:实时喊话 2:文本喊话 3:录音喊话
*函数作者:wzy
*************************************/
uint8_t GetSpeakerMode(void)
{
    return speakerMode;
}
/************************************
*设置文本喊话状态
*函数名:SetTtsStatus
*函数参数:
         @status: 0:无播放 1:播放中
*返回值:
*函数作者:wzy
*************************************/
void SetTtsStatus(uint8_t status)
{
	TTS_flag = status;
}
/************************************
*获取文本喊话状态
*函数名:GetTtsStatus
*函数参数:
        
*返回值:
		0:无播放 1:播放中
*函数作者:wzy
*************************************/
uint8_t GetTtsStatus(void)
{
	return TTS_flag;
}
/************************************
*设置录音喊话状态
*函数名:SetRecordStatus
*函数参数:
         @status: 0:无播放 1:播放中
*返回值:
*函数作者:wzy
*************************************/
void SetRecordStatus(uint8_t status)
{
	aplay_flag = status;
}
/************************************
*获取录音喊话状态
*函数名:GetRecordStatus
*函数参数:
        
*返回值:
		0:无播放 1:播放中
*函数作者:wzy
*************************************/
uint8_t GetRecordStatus(void)
{
	return aplay_flag;
}