//wavヘッダ読み込みとコールバック設定なくせば
//すっきりするので、PCM再生は簡単です。
//読みにくいのとかは気にしないでください。
//あとwavheadreadは大昔に作ったものなので当時
//getw()とか知らなかったんで…あとキャストも怪しいですけど
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspaudiolib.h>
#include <pspaudio.h>
#include <pspdisplay.h>
#include <stdio.h>
#include <string.h>


PSP_MODULE_INFO("TEST",0,1,1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);


int endflag=0;
FILE *in;

int exit_callback(int arg1,int arg2,void *common)
{
endflag=1;
return 0;
}
int CallbackThread(SceSize args,void *argp)
{
int cbid = sceKernelCreateCallback("Exit Callback",exit_callback,NULL);
sceKernelRegisterExitCallback(cbid);

sceKernelSleepThreadCB();

return 0;
}
int setupCallbacks(void)
{
int thid = 0;

thid = sceKernelCreateThread("update_thread",CallbackThread,0x11,0xFA0,0,0);

if(thid >= 0)
  sceKernelStartThread(thid,0,0);
  
return thid;
}


int wavfileheadread(){
char read[50],str[50];/*tmp*/
unsigned int id;/*tmp*/
unsigned long size;/*tmp*/
int lengthofwavefmtchunk,startofdatasecinwavfile;
int datasize;/*datachunksize*/


/* //////////////////////////header check//////////////////////////// */


fgets(read,5,in);
if(strcmp(read,"RIFF")!=0)
  {
  pspDebugScreenSetXY(0,0);
  sprintf(str,"riff wavhead er.");
  pspDebugScreenPrintf(str);
  }
/* //////////////////////////file size/////////////////////////////// */

size=(long)fgetc(in)|(long)fgetc(in)<<8|(long)fgetc(in)<<16|(long)fgetc(in)<<24;
size+=8;


/* ////////////////////////wave file check/////////////////////////// */

fgets(read,9,in);
if(strcmp(read,"WAVEfmt ")!=0)
  {
  pspDebugScreenSetXY(0,0);
  sprintf(str,"wavefmt wav head er.");
  pspDebugScreenPrintf(str);
  }

/* /////////////////////////length of 'wavefmt 'chank//////////////// */


lengthofwavefmtchunk=(long)fgetc(in)|(long)fgetc(in)<<8|(long)fgetc(in)<<16|(long)fgetc(in)<<24;



/* /////////////////////////format id/////////////////////////////// */


id=(long)fgetc(in)|(long)fgetc(in)<<8;


if(id!=0x1)
{
  pspDebugScreenSetXY(0,0);
  sprintf(str,"id wavhead er.");
  pspDebugScreenPrintf(str);
}


/* ////////////////////////channel///////////////////////////////// */

id=(long)fgetc(in)|(long)fgetc(in)<<8;


/* //////////////////////sampling rate/////////////////////////// */

size=(long)fgetc(in)|(long)fgetc(in)<<8|(long)fgetc(in)<<16|(long)fgetc(in)<<24;

if(size!=44100)
{
  pspDebugScreenSetXY(0,0);
  sprintf(str,"sr wavhead er.");
  pspDebugScreenPrintf(str);
}

/* /////////////////////data speed //////////////////////////// */

size=(long)fgetc(in)|(long)fgetc(in)<<8|(long)fgetc(in)<<16|(long)fgetc(in)<<24;


/* /////////////////////brock size//////////////////////////// */

id=(long)fgetc(in)|(long)fgetc(in)<<8;

/* ////////////////////bits of one sample///////////////////// */

id=(long)fgetc(in)|(long)fgetc(in)<<8;


fseek(in,0x14+lengthofwavefmtchunk,SEEK_SET);

startofdatasecinwavfile=0x14+lengthofwavefmtchunk;

/*factとかある場合を考えた*/
datasize=0x14+lengthofwavefmtchunk;
fseek(in,datasize,SEEK_SET);

while((char)fgetc(in)!='d'){
fseek(in,0x3,SEEK_CUR);
datasize=((long)fgetc(in)|(long)fgetc(in)<<8|(long)fgetc(in)<<16|(long)fgetc(in)<<24);
fseek(in,datasize,SEEK_CUR);
startofdatasecinwavfile+=8+datasize;
}/*while*/
fseek(in,0x3,SEEK_CUR);
datasize=(long)fgetc(in)|(long)fgetc(in)<<8|(long)fgetc(in)<<16|(long)fgetc(in)<<24;

startofdatasecinwavfile+=8;


return(datasize);



}//func


int main(int argc,char *argv[])
{
int ret=0,pcmhandle;
char str[50],pcmbuf[128000];

//homeボタンコールバック
setupCallbacks();
//pspDebugScreenなんたら使うのの初期化
pspDebugScreenInit();

//カレントディレクトリのd_03.wavというファイルを再生する。
if((in=fopen("d_03.wav","rb"))==NULL){
  pspDebugScreenSetXY(0,0);
  sprintf(str,"file open failed.");
  pspDebugScreenPrintf(str);
  endflag=1;
}

//wavファイルヘッダーを処理。
wavfileheadread();

//オーディオ出力初期化割り当て バッファ最大、ステレオで
pcmhandle=ret=sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL,PSP_AUDIO_SAMPLE_MAX,PSP_AUDIO_FORMAT_STEREO);

if(ret<0){//失敗
  pspDebugScreenSetXY(0,0);
  sprintf(str,"pcm open failed.");
  pspDebugScreenPrintf(str);
  endflag=1;
}
else{//成功
  pspDebugScreenSetXY(0,0);
  sprintf(str,"pcm open ok.");
  pspDebugScreenPrintf(str);
}


//再生するループ
while(feof(in)==0&&endflag==0){//homeボタンで終わりを指定するか、ファイルが最後まで行ったらループ抜ける。
//ファイルから読み込み。サンプル指定なので*2(ステレオ)*2(16bit)なんかおかしい気もするけどこうなってる。
fread(pcmbuf,1,PSP_AUDIO_SAMPLE_MAX*2*2,in);
//ボリューム最大で(これも最大かはあやしい。)再生。
sceAudioOutputBlocking(pcmhandle,PSP_AUDIO_VOLUME_MAX,pcmbuf);
}


//解放
sceAudioChRelease(pcmhandle);
//終了
sceKernelExitGame();
return 0;
}

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2008年11月24日 01:13