audiotest.c 4.42 KB
#include <ultra64.h>
#include "testDriver.h"

#define	AI_BUF_MAX 500  /* 440ɬ׽ʬ */
#define VOLMAX		5000

#define SEIGEN		ZARU_SINWAVE
#define NOKOGIRI	ZARU_SAWWAVE
#define SANKAKU		ZARU_TRIWAVE
#define KUKEI		  ZARU_RCTWAVE
#define ONLYL     ZARU_ONLYL
#define ONLYR     ZARU_ONLYR

#define PI		3.14159265358979

typedef struct
{
  s16	leftSound;
  s16	rightSound;
} soundElement_t;

typedef union
{
  soundElement_t	ele[AI_BUF_MAX];
  long long int		force_structure_alignment;
} aiBuffer;

aiBuffer	audioBuf;

void audioTest(u32 sound, u32 length, int freq_sample)
{
  u32		i;
  u32		j;
  u32		count=0;
  int           sin_sample=44;

  if (freq_sample > 0) sin_sample = 44100 / freq_sample;

  i=0;
  switch(sound)
  {
  case ONLYR:
    while(i<440)
    {
      for(j=0; j<sin_sample; j++)
      {
	audioBuf.ele[i].leftSound = 0;
	audioBuf.ele[i].rightSound = (s16)(VOLMAX*sinf(2*PI*j/44));
	i++;
      }
    }
    break;
  case ONLYL:
    while(i<440)
    {
      for(j=0; j<sin_sample; j++)
      {
	audioBuf.ele[i].leftSound = (s16)(VOLMAX*sinf(2*PI*j/44));
	audioBuf.ele[i].rightSound =0;
	i++;
      }
    }
    break;
  case SEIGEN:
    while(i<440)
    {
      for(j=0; j<sin_sample; j++)
      {
	audioBuf.ele[i].leftSound = (s16)(VOLMAX*sinf(2*PI*j/44));
	audioBuf.ele[i].rightSound = (s16)(VOLMAX*sinf(2*PI*j/44));
	i++;
      }
    }
    break;
  case NOKOGIRI:
#ifdef	NOKOGIRI_PLUS
    while(i<440)
    {
      for(j=0; j<44; j++)
      {
	audioBuf.ele[i].leftSound = (s16)(VOLMAX - (float)(VOLMAX*j/43));
	audioBuf.ele[i].rightSound = (s16)(VOLMAX - (float)(VOLMAX*j/43));
	i++;
      }
    }
#else
    while(i<440)
    {
      for(j=0; j<44; j++)
      {
	audioBuf.ele[i].leftSound = (s16)(VOLMAX - (float)(VOLMAX*j/21.5F));
	audioBuf.ele[i].rightSound = (s16)(VOLMAX - (float)(VOLMAX*j/21.5F));
	i++;
      }
    }
#endif    
    break;
  case SANKAKU:
    while(i<440)
    {
      for(j=0; j<22; j++)
      {
	audioBuf.ele[i].leftSound = (s16)(2*VOLMAX*j/21) - VOLMAX;
	audioBuf.ele[i].rightSound = (s16)(2*VOLMAX*j/21) - VOLMAX;
	i++;
      }
      for(j=0; j<22; j++)
      {
	audioBuf.ele[i].leftSound = -(s16)(2*VOLMAX*j/21) + VOLMAX;
	audioBuf.ele[i].rightSound = -(s16)(2*VOLMAX*j/21) + VOLMAX;
	i++;
      }
    }
    break;
  case KUKEI:
    while(i<440)
    {
      for(j=0; j<22; j++)
      {
	audioBuf.ele[i].leftSound=VOLMAX;
	audioBuf.ele[i].rightSound=VOLMAX;
	i++;
      }
      for(j=0; j<22; j++)
      {
	audioBuf.ele[i].leftSound=-VOLMAX;
	audioBuf.ele[i].rightSound=-VOLMAX;
	i++;
      }
    }
    break;
  }  

#if 0
  if(sound == NOKOGIRI)
  {
    for(i=0; i<440; i++)
    {
      rmonPrintf("ele[%d].left = %d\n", i, audioBuf.ele[i].leftSound);
    }
  }
#endif  

  osAiSetFrequency(44100);
  osWritebackDCacheAll();
  
  osAiSetNextBuffer((void *)&audioBuf, 440*4);
  osAiSetNextBuffer((void *)&audioBuf, 440*4);
  for(i=0; i<length; i++)
  {
    while(osAiGetStatus() & AI_STATUS_FIFO_FULL)
      ;
    osAiSetNextBuffer((void *)&audioBuf, 440*4);
    pause_start(0);
  }
}

#define AUDIO_PERIOD 256*4
#define REPEAT_TIMES 3
extern unsigned char CfbTestArea[];

#include "my_sound.h"

int my_aitest(int which)
{
    int len, i, repeat=REPEAT_TIMES;
    u16 *snd_buf = (u16 *) CfbTestArea;
    u8 *snd, *end;
    u64 wait;

    switch (which) {
        case ONLYR:
            len = sizeof(right_sound);
            snd = right_sound;
            for (i=0; i<len*2; i+=2) {
                snd_buf[i+1] = ((u16)snd[i+1] << 8) | snd[i];
                snd_buf[i] = 0;
            }
            break;
        case ONLYL:
            len = sizeof(left_sound);
            snd = left_sound;
            for (i=0; i<len*2; i+=2) {
                snd_buf[i] = ((u16)snd[i+1] << 8) | snd[i];
                snd_buf[i+1] = 0;
            }
            break;
        default: 
            len = sizeof(stero_sound);
            snd = stero_sound;
            for (i=0; i<len*2; i+=2) 
                snd_buf[i+1] = snd_buf[i] = ((u16)snd[i+1] << 8) | snd[i];
            repeat = 1;
            break;
    }

    osAiSetFrequency(22050);
    osWritebackDCacheAll();

    for (i=0; i<repeat; i++) {
        snd = (u8 *) CfbTestArea;
        end = snd + (len << 1);
        while (snd < end) {
            osAiSetNextBuffer(snd, AUDIO_PERIOD);
            while(osAiGetStatus() & AI_STATUS_FIFO_FULL);
            snd += AUDIO_PERIOD;
        }
        wait = OS_CYCLES_TO_USEC(osGetTime()) + 200000;
        while (OS_CYCLES_TO_USEC(osGetTime()) < wait);
    }

    return 0;
}