zmaee_tts_audio.c 10.8 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
#ifdef __ZMAEE_APP_TTS__

#include "MMIDataType.h"
#include "MMI_features.h"

#include "kal_release.h"

#include "med_utility.h"
#include "app_mem.h"
#include "aud_main.h"

#include "MedAdapt.h"
#include "med_global.h"
#include "aud_defs.h"
#include "mdi_audio.h"
#include "med_context.h"

#include "zmaee_tts_audio.h"
#include "zmaee_tts_typedef.h"


#define ZMAEE_TTS_PCM_RATE 8000

static char glb_zmaee_tts_bgsuspended = 0;			//背景音被本接口挂起标志
static char glb_zmaee_tts_PcmDeviceOpen = 0;		//pcm设备是否已经打开
static MHdl *glb_zmaee_tts_mhdl_handle = 0;			//pcm设备句柄
static U8 glb_zmaee_tts_CurResult = MED_RES_OK;		//当前操作结果

static U8 glb_zmaee_tts_CurMod = AUD_MODE_NORMAL;
static U8 glb_zmaee_tts_SetMod = AUD_MODE_NORMAL;

static kal_uint8 zmaee_tts_pcm_play_start(kal_uint16 SampleFreq);
static kal_uint8 zmaee_tts_pcm_play_stop(void);
static kal_uint8 zmaee_tts_pcm_push_data(short *Pcm, long *len);
static void zmaee_tts_pcm_release_device(void);


extern aud_context_struct *aud_context_p;
extern int glb_zmaee_tts_stop;

//#define ZMAEE_TTS_DUMP_FILE

#ifdef ZMAEE_TTS_DUMP_FILE
#include "zmaee_typedef.h"
#include "zmaee_priv.h"

static int file_number = 0;
static AEE_IFile *pFile;

extern AEE_IShell*		ZMAEE_GetShell(void);
extern AEE_IFile* ZMAEE_IFileMgr_OpenFile(AEE_IFileMgr* po, const char* pszFile, int mode);
extern int ZMAEE_IFileMgr_Release(AEE_IFileMgr* po);
extern int ZMAEE_IFile_Release(AEE_IFile* po);
extern int ZMAEE_IFile_Write(AEE_IFile * po, void * pSrc, unsigned long nWant);


void zmaee_tts_audio_open_dump_file(void)
{
	AEE_IShell *pShell = ZMAEE_GetShell();
	AEE_IFileMgr *pFileMgr;
	char sz_path[32] = {0};

	ZMAEE_IShell_CreateInstance(pShell, ZM_AEE_CLSID_FILEMGR, (void**)&pFileMgr);
	sprintf(sz_path, "E:\\zmaee\\%d.pcm", file_number++);
	pFile = ZMAEE_IFileMgr_OpenFile(pFileMgr, sz_path, ZM_OFM_TRUNCATE | ZM_OFM_READWRITE);

	ZMAEE_IFileMgr_Release(pFileMgr);
}

void zmaee_tts_audio_close_dump_file(void)
{
	ZMAEE_DebugPrint("zmaee_tts_audio_close_dump_file");
	ZMAEE_IFile_Release(pFile);
}

void zmaee_tts_audio_write_dump_file(void *ptr, int size)
{
	ZMAEE_DebugPrint("zmaee_tts_audio_write_dump_file: ptr = 0x%08x, size = %d", ptr, size);

	ZMAEE_IFile_Write(pFile, ptr, size);
}
#endif

/**
  * 打开audio 接口,提供给mmi task使用
  * 该接口会打开pcm device,并且暂停掉目前在播放的任何声音接口
  * 返回成功(ZMAEE_TTS_AUDIO_SUCCESS)或者失败(ZMAEE_TTS_AUDIO_FAIL)
  **/
int zmaee_tts_audio_open(void)
{
#ifdef ZMAEE_TTS_DUMP_FILE
	zmaee_tts_audio_close_dump_file();
	zmaee_tts_audio_open_dump_file();
#endif

	/* 后台没有阻塞 */
	if(glb_zmaee_tts_bgsuspended == 0)
	{
		mdi_audio_suspend_background_play();
		/* 后台由tts阻塞 */
		glb_zmaee_tts_bgsuspended = 1;
	}
	/* pcm设备没有开启 */
	if(0 == glb_zmaee_tts_PcmDeviceOpen)
	{
		char err;
		/* 开启播音 */
		err = zmaee_tts_pcm_play_start(ZMAEE_TTS_PCM_RATE);
		/* pcm 设备打开失败 */
		if(err != MED_RES_OK) 
		{
			//打开pcm设备错误,停止语音王
			zmaee_tts_audio_close();
			return ZMAEE_TTS_RET_FAILURE;
		}
		else /* 播音开启成功 */
		{
			glb_zmaee_tts_PcmDeviceOpen = 1; 
		}
	}
	return ZMAEE_TTS_RET_SUCCESS;
}


/**
  * 关闭由zmaee_tts_audio_open打开的pcm接口,并恢复由open暂停的后台音乐
  */
void zmaee_tts_audio_close(void)
{
    /* pcm设备开启 */
    if(glb_zmaee_tts_PcmDeviceOpen)
    {
        zmaee_tts_pcm_play_stop();
        glb_zmaee_tts_PcmDeviceOpen = 0;
    }
    /* 后台由tts阻塞的,则恢复到阻塞前的状态 */
    if(glb_zmaee_tts_bgsuspended)
    {
        mdi_audio_resume_background_play();
        glb_zmaee_tts_bgsuspended = 0;
    } 
}



/**
  * 向当前打开的pcm device 发送播放数据
  * 返回成功或者失败
  **/
int zmaee_tts_audio_send_data(void *data, long size)
{
    long pcm_size = size >> 1;
    short *lpSrc = data;
    int nPushDateTimes = 0;

#ifdef ZMAEE_TTS_DUMP_FILE
	zmaee_tts_audio_write_dump_file(data, size);
#endif
	
	
    if(0 == glb_zmaee_tts_PcmDeviceOpen)
    {
    	return ZMAEE_TTS_RET_FAILURE;
    } 
    
    //有pcm数据且没有停止
    while(pcm_size && glb_zmaee_tts_stop == 0)
    {
		char err;
		long tmp = pcm_size;
		err = zmaee_tts_pcm_push_data(lpSrc, &tmp);
		++nPushDateTimes;
		//塞满了停5秒
		if(tmp != pcm_size)
		{
			kal_sleep_task(5);
		}
		//没塞满停1秒
		else
		{
			kal_sleep_task(1);
		}
		lpSrc += tmp;
		pcm_size -= tmp;  
		/*
		连续push的次数超过200次
		推送数据出现错误
		让该次合成结束
		*/
		if(nPushDateTimes > ZMAEE_TTS_AUD_MAX_PUSH_COUNT || err == 0)
		{
			return ZMAEE_TTS_RET_FAILURE;
		}
    }
    return ZMAEE_TTS_RET_SUCCESS;
}


/**
  * 调节设备音量
  * volume 音量级别,0 - 6 
  **/
int zmaee_tts_audio_set_volume(int volume)
{
	mdi_audio_set_volume(MDI_VOLUME_MEDIA, volume);
	
	return ZMAEE_TTS_RET_SUCCESS;
}

/**
  * 设置audio 播放模式
  * flg暂时不使用,全部设置成普通模式
  **/
int zmaee_tts_audio_set_mode(char flg)
{
	/* 检查pcm设备开启 */
	if(1)
	{
		/* 记录使用tts前的mod */
		glb_zmaee_tts_CurMod = mdi_audio_get_audio_mode();
		glb_zmaee_tts_SetMod = glb_zmaee_tts_CurMod;

		/* 耳机模式下设置播音设备*/
		if(srv_earphone_is_plug_in())
		{
			U8 mode = AUD_MODE_NORMAL;
			/* 低线程朗读只使用耳机 */
			if(0 == flg)
			{
				mode = AUD_MODE_HEADSET;
				Media_SetOutputDevice(custom_cfg_hw_aud_output_path(AUDIO_DEVICE_SPEAKER2));
			}
			if(glb_zmaee_tts_CurMod != mode)
			{
				glb_zmaee_tts_SetMod = mode;
				mdi_audio_set_audio_mode(mode);
			}
		}
		else
		{
			if(glb_zmaee_tts_CurMod !=  AUD_MODE_NORMAL)
			{
				glb_zmaee_tts_SetMod = AUD_MODE_NORMAL;
				mdi_audio_set_audio_mode(AUD_MODE_NORMAL); 
			}
		}	  
	}
	return ZMAEE_TTS_RET_SUCCESS;
}

/**
  *恢复audio模式
  **/
int zmaee_tts_audio_resume_mod(void)
{
    if( glb_zmaee_tts_SetMod != glb_zmaee_tts_CurMod && mdi_audio_get_audio_mode() == glb_zmaee_tts_SetMod )
    {
        mdi_audio_set_audio_mode(glb_zmaee_tts_CurMod);
    }

	return ZMAEE_TTS_RET_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////////

static kal_uint8 zmaee_tts_pcm_play_start(kal_uint16 SampleFreq)
{
	ilm_struct *send_ilm;

	glb_zmaee_tts_CurResult = MED_RES_OK;
	/* send msg to play pcm */
	send_ilm = allocate_ilm(MOD_MMI);
	send_ilm->src_mod_id = MOD_MMI;
	send_ilm->dest_mod_id = MOD_MED;
	send_ilm->msg_id = MSG_ID_L4AUD_AUDIO_PLAY_BY_ZMAEETTS_REQ;
	msg_send_ext_queue(send_ilm);
	kal_sleep_task(10);

	return glb_zmaee_tts_CurResult;	
}

static kal_uint8 zmaee_tts_pcm_play_stop(void)
{
	ilm_struct *send_ilm;

	glb_zmaee_tts_CurResult = MED_RES_OK;
	/* send msg to play pcm */
	send_ilm = allocate_ilm(MOD_MMI);
	send_ilm->src_mod_id = MOD_MMI;
	send_ilm->dest_mod_id = MOD_MED;
	send_ilm->msg_id = MSG_ID_L4AUD_AUDIO_STOP_BY_ZMAEETTS_REQ;
	msg_send_ext_queue(send_ilm);
	kal_sleep_task(10);	  
	return glb_zmaee_tts_CurResult;
}

static kal_uint8 zmaee_tts_pcm_push_data(short *Pcm, long *len)
{
	kal_uint8 ret = 0;
	do
	{
		MHdl *Mhdl = glb_zmaee_tts_mhdl_handle;
		char *buf_pcm;
		long buf_len;
		long write_sample;
		void*data = (void*)Pcm;
		MHPB_Internal* ihdl = (MHPB_Internal*)Mhdl;
		if(0 == Mhdl)
		{
			break;
		}
		ACU_GetWriteBuffer(ihdl->ParNode.curHdl, &buf_pcm, &buf_len);
		buf_len >>= 1;
		write_sample = MIN(buf_len,*len);
		buf_len = write_sample  << 1;
		if(buf_len)
			memcpy(buf_pcm,data,buf_len);
		ACU_WriteDataDone(ihdl->ParNode.curHdl,buf_len);
		*len = write_sample;
		ret = 1;
	}
	while(0);

	if(0 == ret)
	{
		*len = 0;
	}

	return ret;
}


//////////////////////////////////////////////////////////////////////////////////////
//PCM播放接口回调
static void zmaee_tts_pcm_play_callback(MHdl *mhdl, Media_Event event)
{	
	switch (event)
	{
		case MEDIA_ERROR:
			ZMAEE_DebugPrint("zmaee_tts_pcm_play_callback: media error");
			glb_zmaee_tts_mhdl_handle = 0;
			break;
		case MEDIA_TERMINATED:
			ZMAEE_DebugPrint("zmaee_tts_pcm_play_callback: MEDIA_TERMINATED");
			glb_zmaee_tts_mhdl_handle = 0;
			break;
		case MEDIA_END:
			ZMAEE_DebugPrint("zmaee_tts_pcm_play_callback: MEDIA_END");
			break;
		case MEDIA_DATA_REQUEST:
			ZMAEE_DebugPrint("zmaee_tts_pcm_play_callback: MEDIA_DATA_REQUEST");
			break;
		default:
			ZMAEE_DebugPrint("zmaee_tts_pcm_play_callback: event = %d", event);
			break;
	}
	
}

static void zmaee_tts_pcm_open_device(void)
{	
	if(0 == glb_zmaee_tts_mhdl_handle)
	{
		do
		{
			Media_PCM_Stream_Param vpFormat = {0};	

			vpFormat.isStereo = 0;
			vpFormat.bitPerSample = 16;
			vpFormat.sampleFreq = ZMAEE_TTS_PCM_RATE;

			if(aud_context_p->state != AUD_MEDIA_IDLE)
			{
				break;
			}
			if (aud_context_p->speech_on
			#if defined(__MED_VID_MOD__) || defined (__MED_MJPG_MOD__)
			|| !vid_is_audio_channel_available()
			#endif 
			)
			{
				break;
			}
			aud_keytone_stop();

			if (aud_context_p->tone_playing)
			{
				aud_tone_stop();
			}

			#ifdef __MED_MMA_MOD__
			/* close all mma tasks */
			aud_mma_close_all();
			#endif /* __MED_MMA_MOD__ */ 

			if (aud_context_p->state == AUD_MEDIA_RECORD
			||aud_context_p->state == AUD_MEDIA_RECORD_PAUSED
			|| aud_context_p->state == AUD_MEDIA_WAIT_RECORD
			||aud_context_p->state == AUD_VM_RECORD 
			)
			{
				break;
			}
			aud_stop_unfinished_process();

			if ( aud_context_p->state != AUD_MEDIA_IDLE )
			{
				break;
			}
			#ifdef aud_util_alloc_ring_buffer
			if(0 == aud_context_p->ring_buf)
			{
				aud_util_alloc_ring_buffer();
			}
			#endif			
			if(0 == aud_context_p->ring_buf)
			{
				break;
			}		
			glb_zmaee_tts_mhdl_handle = PCM_Strm_Open(zmaee_tts_pcm_play_callback, &vpFormat);
			if(0 == glb_zmaee_tts_mhdl_handle)
			{
				glb_zmaee_tts_CurResult = MED_RES_FAIL;
				break;
			}

			memset(aud_context_p->ring_buf,0,AUD_RING_BUFFER_LEN * 2);
			glb_zmaee_tts_mhdl_handle->SetBuffer(glb_zmaee_tts_mhdl_handle, (kal_uint8*)aud_context_p->ring_buf, AUD_RING_BUFFER_LEN * 2);

			/* 开启播放失败 */
			if(MEDIA_SUCCESS != glb_zmaee_tts_mhdl_handle->Play(glb_zmaee_tts_mhdl_handle))
			{
				zmaee_tts_pcm_release_device();
				glb_zmaee_tts_CurResult = MED_RES_FAIL;
				break;
			}

			aud_context_p->state  = AUD_MEDIA_PLAY;
			aud_context_p->source_type = AUD_TTS;
		}
		while(0);


		if(0 == glb_zmaee_tts_mhdl_handle)
		{
			#ifdef aud_util_free_ring_buffer		
			if(aud_context_p->ring_buf)
			{
				aud_util_free_ring_buffer();
			}
			#endif		

			glb_zmaee_tts_CurResult = MED_RES_FAIL;
		}
		else
		{
			glb_zmaee_tts_CurResult = MED_RES_OK;
		}
	}
}

static void zmaee_tts_pcm_release_device(void)
{
	if (glb_zmaee_tts_mhdl_handle)
	{
		glb_zmaee_tts_mhdl_handle->Stop(glb_zmaee_tts_mhdl_handle);
		glb_zmaee_tts_mhdl_handle->Close(glb_zmaee_tts_mhdl_handle);
		glb_zmaee_tts_mhdl_handle = 0;
		#ifdef aud_util_free_ring_buffer		
		if(aud_context_p->ring_buf)
		{
			aud_util_free_ring_buffer();
		}
		#endif		
		aud_context_p->state  = AUD_MEDIA_IDLE;
	}
}


/**
  *med task 处理开启pcm device 接口请求
  **/
void zmaee_tts_pcm_play_req_hdlr(void *ilm_ptr)
{
	zmaee_tts_pcm_open_device();
}

/**
  * med task处理关闭pcm device接口请求
  **/
void zmaee_tts_pcm_stop_req_hdlr(void *ilm_ptr)
{
	zmaee_tts_pcm_release_device();
}


#endif//__ZMAEE_TTS__