line in/out distorted

Discussion about writing software for VS1005 and the VSOS Operating System. Also posts about VS1005-related hardware design and device drivers should be posted here.
Post Reply
Fridolin
User
Posts: 17
Joined: Mon 2015-11-30 12:54

line in/out distorted

Post by Fridolin » Wed 2017-10-11 8:02

Hello,

I have a simple Line in/out thread. That works.

But if I once start a MP3 decoder "StartTask(TASK_DECODER, MP3Thread); Yield();" And Stop it to restart the LineThread. The output from the line Thread is very distorted.
Do you have any idea where the problem might be?

Tasks and Libs are closed correctyl (checked this with TASKS and LIBLIST2)

Thanks for your help.

Fridolin


Start Line Thread

Code: Select all

	if( tasLine == NULL ) {
		ioresult ior; //line		
		ior = ioctl(stdaudioin, IOCTL_AUDIO_SELECT_INPUT,(void *)( AID_LINE1_1 | AID_LINE1_2 ));
		lineTaskRunning = LINE_TASK_IS_RUNNING;
		tasLine = CreateTaskAndStack(LineThread, "Line", 320, 8); // Function, name, stack, priority //320
	}
Line Thread

Code: Select all


	while( lineTaskRunning == LINE_TASK_IS_RUNNING ) { //can be set to LINE_TASK_PLEASE_STOP by another task
							
		fread(myBuf, sizeof(s_int16), BUFSIZE, stdaudioin);
		fwrite(myBuf, sizeof(s_int16), BUFSIZE, stdaudioout);

	}
	lineTaskRunning = LINE_TASK_NOT_RUNNING;

Stop Line Thread

Code: Select all


	if( tasLine != NULL ){

		lineTaskRunning = LINE_TASK_PLEASE_STOP;
		
		while(tasLine->task.tc_State && tasLine->task.tc_State != TS_REMOVED) {
			Delay(10);
		}
		
		FreeTaskAndStack(tasLine); // Task execution has ended, now we can free its stack and other resources
		tasLine = NULL; // Invalidate pointer	
	}

Start MP3 Thread

Code: Select all

StartTask(TASK_DECODER, MP3Thread); Yield(); //Start the MP3 thread and allow it to initialize (Yield() = in the background)
MP3 Thread

Code: Select all

	FILE *inFp = NULL;	
	const char *eStr = NULL;
	int eCode = 0;	
	int result = S_OK;

	...
	
	inFp = fopen( buff, "rb");
	if (!inFp) {
		result = S_ERROR;

		goto finally;
	}
	
	if(auDec) {
		DeleteAudioDecoder(auDecLib, auDec);
		auDec = NULL;
	}
	auDec = CreateAudioDecoder(auDecLib, inFp, stdaudioout, NULL, auDecFGuess);
	if(!auDec) {
		result = S_ERROR;
		goto finally;
	}

	eCode = DecodeAudio(auDecLib, auDec, &eStr);
	//Delay(1000);
	
	finally:
	if(auDec) {
		DeleteAudioDecoder(auDecLib, auDec);
		auDec = NULL;
	}
	
	if(inFp) {
		fclose(inFp);
		inFp = NULL;
	}
	

Stop MP3 Thread

Code: Select all

	Forbid();
	if(auDec) {
		auDec->cs.cancel = 1;
		auDec->pause = 0;
	}
	Permit();
	
	while(pSysTasks[TASK_DECODER].task.tc_State && pSysTasks[TASK_DECODER].task.tc_State != TS_REMOVED) {
		Yield();
	}

User avatar
Henrik
VLSI Staff
Posts: 1037
Joined: Tue 2010-06-22 14:10

Re: line in/out distorted

Post by Henrik » Thu 2017-10-12 11:38

Hello Fridolin!

Your question is a very good one, and I think the code snippets you provided makes it possible to solve your issue.

I think your issue is that you don't set the sample rate. The default sample rate for stdaudioin and stdaudioout is 48 kHz so initially your line in/out task works well. However, when you decode an MP3 file, the output sample rate gets set by the MP3 file, typically to 44.1 kHz. After that, your line in/out task won't work as you expect anymore.

To fix this, add the following code to the startup of your line in/out task:

Code: Select all

u_int32 sampleRate = 48000;
ioctl(stdaudioin,  IOCTL_AUDIO_SET_IRATE, (void *)(&sampleRate));
ioctl(stdaudioout, IOCTL_AUDIO_SET_ORATE, (void *)(&sampleRate));
This should solve your problem.

Kind regards,
- Henrik
Good signatures never die. They just fade away.

Fridolin
User
Posts: 17
Joined: Mon 2015-11-30 12:54

Re: line in/out distorted

Post by Fridolin » Mon 2017-10-16 8:49

Hello Henrik,

yes, it works. Perfekt. Smallness with great effect :roll:

Thanks a lot.

Post Reply

Who is online

Users browsing this forum: No registered users