Help with libAAC, please.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Help with libAAC, please.

Post by sakya »

Hi! :)

I'm trying to use libAAC, but this test program plays the track too fast (really fast).
My test file is a AAC samplerate=44.10Khz, bitrate=48Kb

I'm using sceAudioOutputBlocking with (I think) correct number of samples and reserved the channel with sceAudioChReserve with the same sample number...

Can someone help me, please?

Code: Select all

#include <pspkernel.h>
#include <pspctrl.h>
#include <psppower.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <pspaudio.h>
#include <pspthreadman.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define __ALLEGREX__
#include <aacdec.h>

#ifdef AAC_ENABLE_SBR
#define SBR_MUL		2
#else
#define SBR_MUL		1
#endif

#define SAMPLES_TO_WRITE 1024
#define PSP_VOLUME_MAX 0x8000

PSP_MODULE_INFO&#40;"libAAC test", 0, 1, 0&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;
PSP_HEAP_SIZE_KB&#40;10*1024&#41;;


//////////////////////////////////////////////////////////////////////////////////////////////////////
//Globals&#58;
//////////////////////////////////////////////////////////////////////////////////////////////////////
SceCtrlData input;
static int runningFlag = 1;
char fileName&#91;50&#93; = "ms0&#58;/MUSIC/AAA_TEST/test.aac";
int eos = 0;
u8 *AACSourceBuffer;
u8 *readPtr;
int audio_channel = 0;
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Callbacks&#58;
//////////////////////////////////////////////////////////////////////////////////////////////////////
/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
    runningFlag = 0;
    return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
    int cbid;

    cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
    sceKernelRegisterExitCallback&#40;cbid&#41;;
    sceKernelSleepThreadCB&#40;&#41;;

    return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
    int thid = 0;

    thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0&#41;;
    if&#40;thid >= 0&#41;
        sceKernelStartThread&#40;thid, 0, 0&#41;;
    return thid;
&#125;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Read tag data length&#58;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
int swapInt32BigToHost&#40;int arg&#41;
&#123;
   int i=0;
   int checkEndian = 1;
   if&#40; 1 == *&#40;char *&#41;&checkEndian &#41;
   &#123;
      // Intel &#40;little endian&#41;
      i=arg;
      i=&#40;&#40;i&0xFF000000&#41;>>24&#41;|&#40;&#40;i&0x00FF0000&#41;>>8&#41;|&#40;&#40;i&0x0000FF00&#41;<<8&#41;|&#40;&#40;i&0x000000FF&#41;<<24&#41;;
   &#125;
   else
   &#123;
      // PPC &#40;big endian&#41;
      i=arg;
   &#125;
   return i;
&#125;

int ID3v2TagSize&#40;const char *mp3path&#41;
&#123;
   FILE *fp;
   int size;
   char sig&#91;3&#93;;

   fp = fopen&#40;mp3path, "rb"&#41;;
   if &#40;fp == NULL&#41; return 0;

   fread&#40;sig, sizeof&#40;char&#41;, 3, fp&#41;;
   if &#40;strncmp&#40;"ID3",sig,3&#41; != 0&#41; &#123;
      fclose&#40;fp&#41;;
      return 0;
   &#125;

   fseek&#40;fp, 6, SEEK_SET&#41;;
   fread&#40;&size, sizeof&#40;unsigned int&#41;, 1, fp&#41;;
   /*
    *  The ID3 tag size is encoded with four bytes where the first bit
    *  &#40;bit 7&#41; is set to zero in every byte, making a total of 28 bits. The zeroed
    *  bits are ignored, so a 257 bytes long tag is represented as $00 00 02 01.
    */

   size = &#40;unsigned int&#41; swapInt32BigToHost&#40;&#40;int&#41;size&#41;;
   size = &#40; &#40; &#40;size & 0x7f000000&#41; >> 3 &#41; | &#40; &#40;size & 0x7f0000&#41; >> 2 &#41; | &#40; &#40;size & 0x7f00&#41; >> 1 &#41; | &#40;size & 0x7f&#41; &#41;;
   fclose&#40;fp&#41;;
   return size;
&#125;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Decoder thread&#58;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
int decoderThread&#40;SceSize args, void *argp&#41;&#123;
    HAACDecoder *hAACDecoder;
    AACFrameInfo aacFrameInfo;
    int samplesAvailable = 0;
    short outputBuffer&#91;SAMPLES_TO_WRITE * 4&#93;__attribute__&#40;&#40;aligned&#40;64&#41;&#41;&#41;;

    int AAC_fd = sceIoOpen&#40;fileName, PSP_O_RDONLY, 0777&#41;;
    if &#40;AAC_fd < 0&#41;&#123;
        pspDebugScreenPrintf&#40;"File not found!\n"&#41;;
        return -1;
    &#125;
    double AAC_fileSize = sceIoLseek32&#40;AAC_fd, 0, PSP_SEEK_END&#41;;
	double AAC_filePos  = ID3v2TagSize&#40;fileName&#41;;
    sceIoLseek32&#40;AAC_fd, AAC_filePos, PSP_SEEK_SET&#41;;

    int AACSourceBufferSize = AAC_fileSize - AAC_filePos;
	AACSourceBuffer = &#40;unsigned char *&#41; malloc&#40;AACSourceBufferSize + 8&#41;;
	memset&#40;AACSourceBuffer, 0, AACSourceBufferSize + 8&#41;;
	if &#40;AACSourceBuffer != 0&#41; &#123;		// Read file in
	    sceIoRead&#40;AAC_fd, AACSourceBuffer, AACSourceBufferSize&#41;;
	    readPtr = AACSourceBuffer;
	&#125; else &#123;
        pspDebugScreenPrintf&#40;"Error malloc!\n"&#41;;
	    sceIoClose&#40;AAC_fd&#41;;
	    return 0;
	&#125;

    hAACDecoder = &#40;HAACDecoder *&#41; AACInitDecoder&#40;&#41;;

    while &#40;runningFlag&#41;&#123;
        samplesAvailable = 0;
        memset&#40;outputBuffer, 0, sizeof&#40;outputBuffer&#41;&#41;;
        while &#40;samplesAvailable < SAMPLES_TO_WRITE&#41;&#123;
            unsigned long ret = AACDecode&#40;hAACDecoder, &readPtr, &AACSourceBufferSize, &outputBuffer&#91;samplesAvailable * 2&#93;&#41;;
            if &#40;ret&#41; &#123;
                pspDebugScreenPrintf&#40;"Decoder error &#58;%li\n", ret&#41;;
                eos = 1;
                break;
            &#125; else &#123;
                AACGetLastFrameInfo&#40;hAACDecoder, &aacFrameInfo&#41;;
            &#125;
            samplesAvailable += aacFrameInfo.outputSamps;
        &#125;
        sceAudioOutputBlocking&#40;audio_channel, PSP_VOLUME_MAX, outputBuffer&#41;;
    &#125;

    eos = 1;
    free&#40;AACSourceBuffer&#41;;
    if &#40;hAACDecoder&#41; &#123;
        AACFreeDecoder&#40;hAACDecoder&#41;;
        hAACDecoder = NULL;
    &#125;
    sceIoClose&#40;AAC_fd&#41;;
    return 0;

&#125;

//////////////////////////////////////////////////////////////////////////////////////////////////////
//Main&#58;
//////////////////////////////////////////////////////////////////////////////////////////////////////
int main&#40;void&#41;&#123;
    pspDebugScreenInit&#40;&#41;;
    SetupCallbacks&#40;&#41;;
    scePowerSetCpuClockFrequency&#40;80&#41;;
    scePowerSetBusClockFrequency&#40;54&#41;;

    pspDebugScreenPrintf&#40;"File name  &#58;%s\n", fileName&#41;;
    audio_channel = sceAudioChReserve&#40;0, SAMPLES_TO_WRITE, PSP_AUDIO_FORMAT_STEREO&#41;;
    if &#40;audio_channel < 0&#41;&#123;
        pspDebugScreenPrintf&#40;"Error reserving channel!\n"&#41;;
        return 0;
    &#125;

    //Start decoder thread&#58;
    int thid = sceKernelCreateThread&#40;"decoderThread", decoderThread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL&#41;;
    if&#40;thid < 0&#41;
        return 0;
    sceKernelStartThread&#40;thid, 0, NULL&#41;;
    pspDebugScreenPrintf&#40;"Decoder started.\n"&#41;;

    while&#40;runningFlag&#41;
    &#123;
        //Check Eof&#58;
        if &#40;eos&#41;&#123;
            pspDebugScreenPrintf&#40;"Finished playing\n"&#41;;
            sceKernelDelayThread&#40;4000000&#41;;
            break;
        &#125;

        sceCtrlReadBufferPositive&#40;&input, 1&#41;;
        if &#40;input.Buttons & PSP_CTRL_TRIANGLE&#41;&#123;
          runningFlag = 0;
          break;
        &#125;
        sceKernelDelayThread&#40;10000&#41;;
    &#125;

    sceAudioChRelease&#40;audio_channel&#41;;
    sceKernelExitGame&#40;&#41;;
    return 0;
&#125;
Makefile

Code: Select all

TARGET = libAAC_test
OBJS = main.o

#To build for custom firmware&#58;
BUILD_PRX = 1
PSP_FW_VERSION=371

CFLAGS =  -O3 -frename-registers -ffast-math -fomit-frame-pointer -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LDFLAGS =
LIBS= -laac -lpsppower -lpspaudio

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = libAAC Test

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
Ciaooo
Sakya
Post Reply