Beispiel #1
0
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    connector = new Server(this);



    myself.setName(QString("user ") + QString::number(qrand()));

    // connect(connector,SIGNAL(massageReceived(QByteArray)),this,SLOT(audioOutput(QByteArray))); \\

    connect(connector,SIGNAL(massageReceived(QByteArray)),this,SLOT(audioOutput(QByteArray))); \

    decoder = new AudioDecoder(this);
    recorder = new AudioRecorder(this);
    connect(recorder,SIGNAL(audioBufferder(QByteArray)), connector, SLOT(sendMessage(QByteArray)));

    ui->message_output->setReadOnly(true);
    ui->text_log->setReadOnly(true);


    QString host_rand = QString::number(rand() * 255) + QString(".") +
            QString::number(rand() * 255) + QString(".") +
            QString::number(rand() * 255) + QString(".") +
            QString::number(rand() * 255);
    // QHostAddress host = QHostAddress(host_rand);
    // QHostAddress host =  QHostAddress::LocalHost ;;
    QHostAddress host =  QHostAddress::Broadcast;
    connector->setPort(qrand());
    connector->setHostAdress(host);
    connector->bindHostPort();

    // connector->joinMulticastGroup(host);

    client_tcp = new ClientTcp(this);
    server_tcp = new ServerTCP(this);
    connect(client_tcp,SIGNAL(sendedMessage(QByteArray)),this,SLOT(messageOutput(QByteArray)));
    connect(client_tcp,SIGNAL(receivedMessage(QByteArray)),this,SLOT(messageOutput(QByteArray)));
    connect(client_tcp,SIGNAL(connected()),this,SLOT(serverFounded()));

    //connect(server_tcp,SIGNAL(),this,SLOT());
    connect(server_tcp,SIGNAL(sendedMessage(QByteArray)),this,SLOT(messageOutput(QByteArray)));
    connect(server_tcp,SIGNAL(receivedMessage(QByteArray)),this,SLOT(messageOutput(QByteArray)));
    connect(server_tcp,SIGNAL(newConnection()),this,SLOT(clientConnected()));

    QHostAddress hosta("127.15.10.12");

    quint16 port = 135665;
    client_tcp->setHostAdress(hosta);
    client_tcp->setPort(port);

    server_tcp->setHostAdress(hosta);
    server_tcp->setPort(port);
    //    connecor->ho



}
Beispiel #2
0
void	audioBase::audioOut_48000	(int16_t *V, int32_t amount) {
float *buffer = (float *)alloca (2 * amount * sizeof (float));
int32_t	i, n;

	for (i = 0; i < amount; i ++) {
	   buffer [2 * i]	= V [2 * i] / 32767.0;
	   buffer [2 * i + 1]	= V [2 * i + 1] / 32767.0;
	}

	audioOutput (buffer, amount);
}
Beispiel #3
0
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Private functions:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Decode thread:
int decodeThread(SceSize args, void *argp){
    int res;
    unsigned char MP3ME_header_buf[4];
    int MP3ME_header;
    int version;
    int bitrate;
    int padding;
    int frame_size;
    int size;
	int offset = 0;

	sceAudiocodecReleaseEDRAM(MP3ME_codec_buffer); //Fix: ReleaseEDRAM at the end is not enough to play another mp3.
	MP3ME_threadActive = 1;
    MP3ME_threadExited = 0;
    OutputBuffer_flip = 0;
    OutputPtrME = OutputBuffer[0];

    MP3ME_handle = sceIoOpen(MP3ME_Name, PSP_O_RDONLY, 0777);
    if (MP3ME_handle < 0)
        MP3ME_threadActive = 0;

	//now search for the first sync byte, tells us where the mp3 stream starts
	size = sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_END);
	sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_SET);
	data_start = ID3v2TagSize(MP3ME_Name);
	sceIoLseek32(MP3ME_handle, data_start, PSP_SEEK_SET);
    data_start = MP3ME_SeekNextFrameMP3(MP3ME_handle);

	if (data_start < 0)
		MP3ME_threadActive = 0;

    size -= data_start;

    memset(MP3ME_codec_buffer, 0, sizeof(MP3ME_codec_buffer));

    if ( sceAudiocodecCheckNeedMem(MP3ME_codec_buffer, 0x1002) < 0 )
        MP3ME_threadActive = 0;

    if ( sceAudiocodecGetEDRAM(MP3ME_codec_buffer, 0x1002) < 0 )
        MP3ME_threadActive = 0;

    getEDRAM = 1;

    if ( sceAudiocodecInit(MP3ME_codec_buffer, 0x1002) < 0 )
        MP3ME_threadActive = 0;

    MP3ME_eof = 0;

	while (MP3ME_threadActive){
		while( !MP3ME_eof && MP3ME_isPlaying )
		{
            MP3ME_filePos = sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_CUR);
			if ( sceIoRead( MP3ME_handle, MP3ME_header_buf, 4 ) != 4 ){
				MP3ME_isPlaying = 0;
				MP3ME_threadActive = 0;
				continue;
			}

			MP3ME_header = MP3ME_header_buf[0];
			MP3ME_header = (MP3ME_header<<8) | MP3ME_header_buf[1];
			MP3ME_header = (MP3ME_header<<8) | MP3ME_header_buf[2];
			MP3ME_header = (MP3ME_header<<8) | MP3ME_header_buf[3];

			bitrate = (MP3ME_header & 0xf000) >> 12;
			padding = (MP3ME_header & 0x200) >> 9;
			version = (MP3ME_header & 0x180000) >> 19;
			samplerate = samplerates[version][ (MP3ME_header & 0xC00) >> 10 ];

			if ((bitrate > 14) || (version == 1) || (samplerate == 0) || (bitrate == 0))//invalid frame, look for the next one
			{
				data_start = MP3ME_SeekNextFrameMP3(MP3ME_handle);
				if(data_start < 0)
				{
					MP3ME_eof = 1;
					continue;
				}
				size -= (data_start - offset);
				offset = data_start;
				continue;
			}

			if (version == 3) //mpeg-1
			{
				sample_per_frame = 1152;
				frame_size = 144000*bitrates[bitrate]/samplerate + padding;
			}else{
				sample_per_frame = 576;
				frame_size = 72000*bitrates_v2[bitrate]/samplerate + padding;
			}

			sceIoLseek32(MP3ME_handle, data_start, PSP_SEEK_SET); //seek back

			size -= frame_size;
			if ( size <= 0)
			{
			   MP3ME_eof = 1;
			   continue;
			}

			//since we check for eof above, this can only happen when the file
			// handle has been invalidated by syspend/resume/usb
			if ( sceIoRead( MP3ME_handle, MP3ME_input_buffer, frame_size ) != frame_size ){
                //Resume from suspend:
                if ( MP3ME_handle >= 0 ){
                   sceIoClose(MP3ME_handle);
                   MP3ME_handle = -1;
                }
                MP3ME_handle = sceIoOpen(MP3ME_Name, PSP_O_RDONLY, 0777);
                if (MP3ME_handle < 0){
                    MP3ME_isPlaying = 0;
                    MP3ME_threadActive = 0;
                    continue;
                }
                size = sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_END);
                sceIoLseek32(MP3ME_handle, offset, PSP_SEEK_SET);
                data_start = offset;
				continue;
			}
			data_start += frame_size;
			offset = data_start;

			MP3ME_codec_buffer[6] = (unsigned long)MP3ME_input_buffer;
			MP3ME_codec_buffer[8] = (unsigned long)MP3ME_output_buffer;

			MP3ME_codec_buffer[7] = MP3ME_codec_buffer[10] = frame_size;
			MP3ME_codec_buffer[9] = sample_per_frame * 4;

			res = sceAudiocodecDecode(MP3ME_codec_buffer, 0x1002);

			if ( res < 0 )
			{
				//instead of quitting see if the next frame can be decoded
				//helps play files with an invalid frame
				//we must look for a valid frame, the offset above may be wrong
				data_start = MP3ME_SeekNextFrameMP3(MP3ME_handle);
				if(data_start < 0)
				{
					MP3ME_eof = 1;
					continue;
				}
				size -= (data_start - offset);
				offset = data_start;
				continue;
			}
            MP3ME_playingTime += (float)sample_per_frame/(float)samplerate;

            //Output:
			memcpy( OutputPtrME, MP3ME_output_buffer, sample_per_frame*4);
			OutputPtrME += (sample_per_frame * 4);
			if( OutputPtrME + (sample_per_frame * 4) > &OutputBuffer[OutputBuffer_flip][OUTPUT_BUFFER_SIZE])
			{

                audioOutput(PSP_AUDIO_VOLUME_MAX, OutputBuffer[OutputBuffer_flip]);

				OutputBuffer_flip ^= 1;
				OutputPtrME = OutputBuffer[OutputBuffer_flip];

			}
		}
		sceKernelDelayThread(10000); // Not sure if necessary or purpose
	}
    if (getEDRAM)
        sceAudiocodecReleaseEDRAM(MP3ME_codec_buffer);

    if ( MP3ME_handle >= 0){
      sceIoClose(MP3ME_handle);
      MP3ME_handle = -1;
    }
    MP3ME_threadExited = 1;
    return 0;
}
Beispiel #4
0
int main(int argc, char *argv[])
{
    SceCtrlData pad;
    int oldButtons = 0;
    int i;
#define SECOND       1000000
#define REPEAT_START (1 * SECOND)
#define REPEAT_DELAY (SECOND / 5)
    struct timeval repeatStart;
    struct timeval repeatDelay;

    repeatStart.tv_sec = 0;
    repeatStart.tv_usec = 0;
    repeatDelay.tv_sec = 0;
    repeatDelay.tv_usec = 0;

    for (i = 0; i < SAMPLE_SIZE; i += 2)
    {
	buffer1[i+0] = 0x00;
	buffer1[i+1] = 0x70;
	buffer2[i+0] = 0x00;
	buffer2[i+1] = 0x10;
    }

    pspDebugScreenInit();

    printf("Triangle - Exit\n");
    printf("Cross - Test Blocking Audio\n");
    printf("Square - Test non-Blocking Audio\n");
    printf("Circle - Test Audio2\n");

    channel = sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL, SAMPLE_SIZE, PSP_AUDIO_FORMAT_STEREO);
    if (channel < 0)
    {
        sceKernelExitGame();
    }

    while(!done)
    {
        sceCtrlReadBufferPositive(&pad, 1);
        int buttonDown = (oldButtons ^ pad.Buttons) & pad.Buttons;

	if (pad.Buttons == oldButtons)
	{
		struct timeval now;
		gettimeofday(&now, NULL);
		if (repeatStart.tv_sec == 0)
		{
			repeatStart.tv_sec = now.tv_sec;
			repeatStart.tv_usec = now.tv_usec;
			repeatDelay.tv_sec = 0;
			repeatDelay.tv_usec = 0;
		}
		else
		{
			long usec = (now.tv_sec - repeatStart.tv_sec) * SECOND;
			usec += (now.tv_usec - repeatStart.tv_usec);
			if (usec >= REPEAT_START)
			{
				if (repeatDelay.tv_sec != 0)
				{
					usec = (now.tv_sec - repeatDelay.tv_sec) * SECOND;
					usec += (now.tv_usec - repeatDelay.tv_usec);
					if (usec >= REPEAT_DELAY)
					{
						repeatDelay.tv_sec = 0;
					}
				}

				if (repeatDelay.tv_sec == 0)
				{
					buttonDown = pad.Buttons;
					repeatDelay.tv_sec = now.tv_sec;
					repeatDelay.tv_usec = now.tv_usec;
				}
			}
		}
	}
	else
	{
		repeatStart.tv_sec = 0;
	}

        if (buttonDown & PSP_CTRL_CROSS)
        {
		strcpy(text, "Start Test:\n");
		gettimeofday(&previousAudioOutput, NULL);
		for (i = 0; i < 10; i++)
		{
			audioOutput(buffer1, 1);
			audioOutput(buffer2, 1);
		}
		strcat(text, "\n");

		printf(text);
        }

        if (buttonDown & PSP_CTRL_SQUARE)
        {
		strcpy(text, "Start Test:\n");
		gettimeofday(&previousAudioOutput, NULL);
		for (i = 0; i < 10; i++)
		{
			audioOutput(buffer1, 0);
			audioOutput(buffer2, 0);
		}
		strcat(text, "\n");

		printf(text);
        }

        if (buttonDown & PSP_CTRL_CIRCLE)
        {
		sceAudioOutput2Reserve(SAMPLE_SIZE / 2);

		audioOutput2(buffer1, 0xFFFFF);
		sceKernelDelayThread(1 * 1000000);
		audioOutput2(buffer1, 0x20000);
		sceKernelDelayThread(1 * 1000000);
		audioOutput2(buffer1, 0x10000);
		sceKernelDelayThread(1 * 1000000);
		audioOutput2(buffer1, 0x08000);
		sceKernelDelayThread(1 * 1000000);
		audioOutput2(buffer1, 0x04000);
		sceKernelDelayThread(1 * 1000000);
		audioOutput2(buffer1, 0x02000);
		sceKernelDelayThread(1 * 1000000);
		audioOutput2(buffer1, 0x01000);
		sceKernelDelayThread(1 * 1000000);
		audioOutput2(buffer1, 0x00800);
        }

        if (buttonDown & PSP_CTRL_RIGHT)
        {
        }

        if (buttonDown & PSP_CTRL_UP)
        {
        }

        if (buttonDown & PSP_CTRL_DOWN)
        {
        }


        if (buttonDown & PSP_CTRL_TRIANGLE)
	{
        	done = 1;
	}

        oldButtons = pad.Buttons;
	sceDisplayWaitVblank();
    }

    sceKernelExitGame();
    return 0;
}
Beispiel #5
0
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Private functions:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Decode thread:
int decodeThread(SceSize args, void *argp){
    int res;
    unsigned char MP3ME_header_buf[4];
    int MP3ME_header;
    int version;
    int bitrate;
    int padding;
    int frame_size;
    int size;
    int total_size;
	int offset = 0;

	sceAudiocodecReleaseEDRAM(MP3ME_codec_buffer); //Fix: ReleaseEDRAM at the end is not enough to play another mp3.
	MP3ME_threadActive = 1;
    OutputBuffer_flip = 0;
    OutputPtrME = OutputBuffer[0];

    sceIoChdir(audioCurrentDir);
    MP3ME_handle = sceIoOpen(MP3ME_fileName, PSP_O_RDONLY, 0777);
    if (MP3ME_handle < 0)
        MP3ME_threadActive = 0;

	//now search for the first sync byte, tells us where the mp3 stream starts
	total_size = sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_END);
	size = total_size;
	sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_SET);
	data_start = ID3v2TagSize(MP3ME_fileName);
	sceIoLseek32(MP3ME_handle, data_start, PSP_SEEK_SET);
    data_start = SeekNextFrameMP3(MP3ME_handle);

	if (data_start < 0)
		MP3ME_threadActive = 0;

    size -= data_start;

    memset(MP3ME_codec_buffer, 0, sizeof(MP3ME_codec_buffer));
    memset(MP3ME_input_buffer, 0, sizeof(MP3ME_input_buffer));
    memset(MP3ME_output_buffer, 0, sizeof(MP3ME_output_buffer));

    if ( sceAudiocodecCheckNeedMem(MP3ME_codec_buffer, 0x1002) < 0 )
        MP3ME_threadActive = 0;

    if ( sceAudiocodecGetEDRAM(MP3ME_codec_buffer, 0x1002) < 0 )
        MP3ME_threadActive = 0;

    getEDRAM = 1;

    if ( sceAudiocodecInit(MP3ME_codec_buffer, 0x1002) < 0 )
        MP3ME_threadActive = 0;

    MP3ME_eof = 0;
	MP3ME_info.framesDecoded = 0;

	while (MP3ME_threadActive){
		while( !MP3ME_eof && MP3ME_isPlaying )
		{
            MP3ME_filePos = sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_CUR);
			if ( sceIoRead( MP3ME_handle, MP3ME_header_buf, 4 ) != 4 ){
				MP3ME_isPlaying = 0;
				MP3ME_threadActive = 0;
				continue;
			}

			MP3ME_header = MP3ME_header_buf[0];
			MP3ME_header = (MP3ME_header<<8) | MP3ME_header_buf[1];
			MP3ME_header = (MP3ME_header<<8) | MP3ME_header_buf[2];
			MP3ME_header = (MP3ME_header<<8) | MP3ME_header_buf[3];

			bitrate = (MP3ME_header & 0xf000) >> 12;
			padding = (MP3ME_header & 0x200) >> 9;
			version = (MP3ME_header & 0x180000) >> 19;
			samplerate = samplerates[version][ (MP3ME_header & 0xC00) >> 10 ];

			if ((bitrate > 14) || (version == 1) || (samplerate == 0) || (bitrate == 0))//invalid frame, look for the next one
			{
				data_start = SeekNextFrameMP3(MP3ME_handle);
				if(data_start < 0)
				{
					MP3ME_eof = 1;
					continue;
				}
				size -= (data_start - offset);
				offset = data_start;
				continue;
			}

			if (version == 3) //mpeg-1
			{
				sample_per_frame = 1152;
				frame_size = 144000*bitrates[bitrate]/samplerate + padding;
				MP3ME_info.instantBitrate = bitrates[bitrate] * 1000;
			}else{
				sample_per_frame = 576;
				frame_size = 72000*bitrates_v2[bitrate]/samplerate + padding;
				MP3ME_info.instantBitrate = bitrates_v2[bitrate] * 1000;
			}

			sceIoLseek32(MP3ME_handle, data_start, PSP_SEEK_SET); //seek back

            if (MP3ME_newFilePos >= 0)
            {
                if (!MP3ME_newFilePos)
                    MP3ME_newFilePos = ID3v2TagSize(MP3ME_fileName);

                long old_start = data_start;
                if (sceIoLseek32(MP3ME_handle, MP3ME_newFilePos, PSP_SEEK_SET) != old_start){
                    data_start = SeekNextFrameMP3(MP3ME_handle);
                    if(data_start < 0){
                        MP3ME_eof = 1;
                    }
                    MP3ME_playingTime = (float)data_start / (float)frame_size /  (float)samplerate / 1000.0f;
                    offset = data_start;
                    size = total_size - data_start;
                }
                MP3ME_newFilePos = -1;
                continue;
            }

			size -= frame_size;
			if ( size <= 0)
			{
			   MP3ME_eof = 1;
			   continue;
			}

			//since we check for eof above, this can only happen when the file
			// handle has been invalidated by syspend/resume/usb
			if ( sceIoRead( MP3ME_handle, MP3ME_input_buffer, frame_size ) != frame_size ){
                //Resume from suspend:
                if ( MP3ME_handle >= 0 ){
                   sceIoClose(MP3ME_handle);
                   MP3ME_handle = -1;
                }
                MP3ME_handle = sceIoOpen(MP3ME_fileName, PSP_O_RDONLY, 0777);
                if (MP3ME_handle < 0){
                    MP3ME_isPlaying = 0;
                    MP3ME_threadActive = 0;
                    continue;
                }
                size = sceIoLseek32(MP3ME_handle, 0, PSP_SEEK_END);
                sceIoLseek32(MP3ME_handle, offset, PSP_SEEK_SET);
                data_start = offset;
				continue;
			}
			data_start += frame_size;
			offset = data_start;

			MP3ME_codec_buffer[6] = (unsigned long)MP3ME_input_buffer;
			MP3ME_codec_buffer[8] = (unsigned long)MP3ME_output_buffer;

			MP3ME_codec_buffer[7] = MP3ME_codec_buffer[10] = frame_size;
			MP3ME_codec_buffer[9] = sample_per_frame * 4;

			res = sceAudiocodecDecode(MP3ME_codec_buffer, 0x1002);

			if ( res < 0 )
			{
				//instead of quitting see if the next frame can be decoded
				//helps play files with an invalid frame
				//we must look for a valid frame, the offset above may be wrong
				data_start = SeekNextFrameMP3(MP3ME_handle);
				if(data_start < 0)
				{
					MP3ME_eof = 1;
					continue;
				}
				size -= (data_start - offset);
				offset = data_start;
				continue;
			}
            MP3ME_playingTime += (float)sample_per_frame/(float)samplerate;
		    MP3ME_info.framesDecoded++;

            //Output:
			memcpy( OutputPtrME, MP3ME_output_buffer, sample_per_frame*4);
			OutputPtrME += (sample_per_frame * 4);
			if( OutputPtrME + (sample_per_frame * 4) > &OutputBuffer[OutputBuffer_flip][OUTPUT_BUFFER_SIZE])
			{
				//Volume Boost:
				if (MP3ME_volume_boost){
                    int i;
                    for (i=0; i<OUTPUT_BUFFER_SIZE; i++){
    					OutputBuffer[OutputBuffer_flip][i] = volume_boost(&OutputBuffer[OutputBuffer_flip][i], &MP3ME_volume_boost);
                    }
                }

                audioOutput(MP3ME_volume, OutputBuffer[OutputBuffer_flip]);

				OutputBuffer_flip ^= 1;
				OutputPtrME = OutputBuffer[OutputBuffer_flip];
		        //Check for playing speed:
                if (MP3ME_playingSpeed){
                    long old_start = data_start;
                    if (sceIoLseek32(MP3ME_handle, frame_size * MP3ME_playingSpeed, PSP_SEEK_CUR) != old_start){
                        data_start = SeekNextFrameMP3(MP3ME_handle);
                        if(data_start < 0){
                            MP3ME_eof = 1;
                            continue;
                        }
                        float framesSkipped = (float)abs(old_start - data_start) / (float)frame_size;
                        if (MP3ME_playingSpeed > 0)
                            MP3ME_playingTime += framesSkipped * (float)sample_per_frame/(float)samplerate;
                        else
                            MP3ME_playingTime -= framesSkipped * (float)sample_per_frame/(float)samplerate;

                        offset = data_start;
                        size = total_size - data_start;
                    }else
                        MP3ME_setPlayingSpeed(0);
                }
			}
		}
		sceKernelDelayThread(10000);
	}
    if (getEDRAM)
        sceAudiocodecReleaseEDRAM(MP3ME_codec_buffer);

    if ( MP3ME_handle >= 0){
      sceIoClose(MP3ME_handle);
      MP3ME_handle = -1;
    }
	sceKernelExitThread(0);
    return 0;
}
Beispiel #6
0
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Private functions:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Decode thread:
int AACdecodeThread(SceSize args, void *argp){
   u8* aac_data_buffer = NULL;
   u32 aac_data_start;
   u8 aac_getEDRAM;
   u32 aac_channels;
   u32 aac_samplerate;

   sceAudiocodecReleaseEDRAM(aac_codec_buffer);
   sceIoChdir(audioCurrentDir);

   AAC_threadActive = 1;
   AAC_handle = sceIoOpen(AAC_fileName, PSP_O_RDONLY, 0777);
   if (  ! AAC_handle ){
      AAC_threadActive = 0;
      goto end;
   }

   aac_output_index = 0;
   aac_channels = 2;
   aac_samplerate = AAC_info.hz;

   aac_data_start = 0;
   if (AAC_tagsize > 0)
       aac_data_start = AAC_tagsize + 10;

   sceIoLseek32(AAC_handle, aac_data_start, PSP_SEEK_SET);
   aac_data_start = SeekNextFrameMP3(AAC_handle);

   memset(aac_codec_buffer, 0, sizeof(aac_codec_buffer));

   if ( sceAudiocodecCheckNeedMem(aac_codec_buffer, 0x1003) < 0 ){
      AAC_threadActive = 0;
      goto end;
   }

   if ( sceAudiocodecGetEDRAM(aac_codec_buffer, 0x1003) < 0 ){
         AAC_threadActive = 0;
         goto end;
   }
   aac_getEDRAM = 1;

   aac_codec_buffer[10] = aac_samplerate;
   if ( sceAudiocodecInit(aac_codec_buffer, 0x1003) < 0 ){
      AAC_threadActive = 0;
      goto end;
   }

    int samplesdecoded;
    AAC_eof = 0;
    unsigned char aac_header_buf[7];
    int skip = 0;

	while (AAC_threadActive){
		while( !AAC_eof && AAC_isPlaying ){
              memset(aac_mix_buffer, 0, aac_sample_per_frame*2*2);
              if ( sceIoRead( AAC_handle, aac_header_buf, 7 ) != 7 ) {
                 AAC_eof = 1;
                 continue;
              }
              int aac_header = aac_header_buf[3];
              aac_header = (aac_header<<8) | aac_header_buf[4];
              aac_header = (aac_header<<8) | aac_header_buf[5];
              aac_header = (aac_header<<8) | aac_header_buf[6];

              int frame_size = aac_header & 67100672;
              frame_size = frame_size >> 13;
              frame_size = frame_size - 7;

              if ( aac_data_buffer ){
                 free(aac_data_buffer);
                 aac_data_buffer = NULL;
              }
              aac_data_buffer = (u8*)memalign(64, frame_size);

              if (AAC_newFilePos >= 0)
              {
                  if (!AAC_newFilePos)
                      AAC_newFilePos = AAC_tagsize + 10;

                  u32 old_start = aac_data_start;
                  if (sceIoLseek32(AAC_handle, AAC_newFilePos, PSP_SEEK_SET) != old_start){
                      aac_data_start = SeekNextFrameMP3(AAC_handle);
                      if(aac_data_start < 0){
                          AAC_eof = 1;
                      }
                      AAC_playingTime = (float)aac_data_start / (float)frame_size /  (float)aac_samplerate / 1000.0f;
                  }
                  AAC_newFilePos = -1;
                  continue;
              }

		      //Check for playing speed:
              if (AAC_playingSpeed){
                  if (skip){
                      u32 old_start = aac_data_start;
                      if (sceIoLseek32(AAC_handle, frame_size / 4 * AAC_playingSpeed, PSP_SEEK_CUR) != old_start){
                          aac_data_start = SeekNextFrameMP3(AAC_handle);
                          if(aac_data_start < 0){
                              AAC_eof = 1;
                              continue;
                          }
                          AAC_filePos = aac_data_start;
                          float framesSkipped = (float)aac_data_start / (float)frame_size;
                          AAC_playingTime = framesSkipped * (float)aac_sample_per_frame/(float)aac_samplerate;
                      }else
                          AAC_setPlayingSpeed(0);
                      skip = !skip;
                      continue;
                  }
                  skip = !skip;
              }

              if ( sceIoRead( AAC_handle, aac_data_buffer, frame_size ) != frame_size ) {
                 AAC_eof = 1;
                 continue;
              }

              aac_data_start += (frame_size+7);
              AAC_filePos = aac_data_start;

              aac_codec_buffer[6] = (unsigned long)aac_data_buffer;
              aac_codec_buffer[8] = (unsigned long)aac_mix_buffer;

              aac_codec_buffer[7] = frame_size;
              aac_codec_buffer[9] = aac_sample_per_frame * 4;


              int res = sceAudiocodecDecode(aac_codec_buffer, 0x1003);
              if ( res < 0 ) {
                 AAC_eof = 1;
                 continue;
              }
              memcpy(aac_output_buffer[aac_output_index], aac_mix_buffer, aac_sample_per_frame*4);

              //Volume Boost:
              if (AAC_volume_boost){
                  int i;
                  for (i=0; i<aac_sample_per_frame * 2; i++){
                    aac_output_buffer[aac_output_index][i] = volume_boost(&aac_output_buffer[aac_output_index][i], &AAC_volume_boost);
                  }
              }

              audioOutput(AAC_volume, aac_output_buffer[aac_output_index]);
              aac_output_index = (aac_output_index+1)%4;

              samplesdecoded = aac_sample_per_frame;

              AAC_playingTime += (float)aac_sample_per_frame/(float)aac_samplerate;
              AAC_info.framesDecoded++;
            }
            sceKernelDelayThread(10000);
    }
end:
   if ( AAC_handle )
      sceIoClose(AAC_handle);

   if ( aac_data_buffer)
   {
      free(aac_data_buffer);
      aac_data_buffer = NULL;
   }

   if ( aac_getEDRAM )
      sceAudiocodecReleaseEDRAM(aac_codec_buffer);

    sceKernelExitThread(0);
    return 0;
}