src/audio/dc/SDL_dcaudio.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    33 
    33 
    34 #include "aica.h"
    34 #include "aica.h"
    35 #include <dc/spu.h>
    35 #include <dc/spu.h>
    36 
    36 
    37 /* Audio driver functions */
    37 /* Audio driver functions */
    38 static int DCAUD_OpenAudio (_THIS, SDL_AudioSpec * spec);
    38 static int DCAUD_OpenAudio(_THIS, SDL_AudioSpec * spec);
    39 static void DCAUD_WaitAudio (_THIS);
    39 static void DCAUD_WaitAudio(_THIS);
    40 static void DCAUD_PlayAudio (_THIS);
    40 static void DCAUD_PlayAudio(_THIS);
    41 static Uint8 *DCAUD_GetAudioBuf (_THIS);
    41 static Uint8 *DCAUD_GetAudioBuf(_THIS);
    42 static void DCAUD_CloseAudio (_THIS);
    42 static void DCAUD_CloseAudio(_THIS);
    43 
    43 
    44 /* Audio driver bootstrap functions */
    44 /* Audio driver bootstrap functions */
    45 static int
    45 static int
    46 DCAUD_Available (void)
    46 DCAUD_Available(void)
    47 {
    47 {
    48     return 1;
    48     return 1;
    49 }
    49 }
    50 
    50 
    51 static void
    51 static void
    52 DCAUD_DeleteDevice (SDL_AudioDevice * device)
    52 DCAUD_DeleteDevice(SDL_AudioDevice * device)
    53 {
    53 {
    54     SDL_free (device->hidden);
    54     SDL_free(device->hidden);
    55     SDL_free (device);
    55     SDL_free(device);
    56 }
    56 }
    57 
    57 
    58 static SDL_AudioDevice *
    58 static SDL_AudioDevice *
    59 DCAUD_CreateDevice (int devindex)
    59 DCAUD_CreateDevice(int devindex)
    60 {
    60 {
    61     SDL_AudioDevice *this;
    61     SDL_AudioDevice *this;
    62 
    62 
    63     /* Initialize all variables that we clean on shutdown */
    63     /* Initialize all variables that we clean on shutdown */
    64     this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice));
    64     this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
    65     if (this) {
    65     if (this) {
    66         SDL_memset (this, 0, (sizeof *this));
    66         SDL_memset(this, 0, (sizeof *this));
    67         this->hidden = (struct SDL_PrivateAudioData *)
    67         this->hidden = (struct SDL_PrivateAudioData *)
    68             SDL_malloc ((sizeof *this->hidden));
    68             SDL_malloc((sizeof *this->hidden));
    69     }
    69     }
    70     if ((this == NULL) || (this->hidden == NULL)) {
    70     if ((this == NULL) || (this->hidden == NULL)) {
    71         SDL_OutOfMemory ();
    71         SDL_OutOfMemory();
    72         if (this) {
    72         if (this) {
    73             SDL_free (this);
    73             SDL_free(this);
    74         }
    74         }
    75         return (0);
    75         return (0);
    76     }
    76     }
    77     SDL_memset (this->hidden, 0, (sizeof *this->hidden));
    77     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    78 
    78 
    79     /* Set the function pointers */
    79     /* Set the function pointers */
    80     this->OpenAudio = DCAUD_OpenAudio;
    80     this->OpenAudio = DCAUD_OpenAudio;
    81     this->WaitAudio = DCAUD_WaitAudio;
    81     this->WaitAudio = DCAUD_WaitAudio;
    82     this->PlayAudio = DCAUD_PlayAudio;
    82     this->PlayAudio = DCAUD_PlayAudio;
    83     this->GetAudioBuf = DCAUD_GetAudioBuf;
    83     this->GetAudioBuf = DCAUD_GetAudioBuf;
    84     this->CloseAudio = DCAUD_CloseAudio;
    84     this->CloseAudio = DCAUD_CloseAudio;
    85 
    85 
    86     this->free = DCAUD_DeleteDevice;
    86     this->free = DCAUD_DeleteDevice;
    87 
    87 
    88     spu_init ();
    88     spu_init();
    89 
    89 
    90     return this;
    90     return this;
    91 }
    91 }
    92 
    92 
    93 AudioBootStrap DCAUD_bootstrap = {
    93 AudioBootStrap DCAUD_bootstrap = {
    95     DCAUD_Available, DCAUD_CreateDevice
    95     DCAUD_Available, DCAUD_CreateDevice
    96 };
    96 };
    97 
    97 
    98 /* This function waits until it is possible to write a full sound buffer */
    98 /* This function waits until it is possible to write a full sound buffer */
    99 static void
    99 static void
   100 DCAUD_WaitAudio (_THIS)
   100 DCAUD_WaitAudio(_THIS)
   101 {
   101 {
   102     if (this->hidden->playing) {
   102     if (this->hidden->playing) {
   103         /* wait */
   103         /* wait */
   104         while (aica_get_pos (0) / this->spec.samples == this->hidden->nextbuf) {
   104         while (aica_get_pos(0) / this->spec.samples == this->hidden->nextbuf) {
   105             thd_pass ();
   105             thd_pass();
   106         }
   106         }
   107     }
   107     }
   108 }
   108 }
   109 
   109 
   110 #define	SPU_RAM_BASE	0xa0800000
   110 #define	SPU_RAM_BASE	0xa0800000
   111 
   111 
   112 static void
   112 static void
   113 spu_memload_stereo8 (int leftpos, int rightpos, void *src0, size_t size)
   113 spu_memload_stereo8(int leftpos, int rightpos, void *src0, size_t size)
   114 {
   114 {
   115     uint8 *src = src0;
   115     uint8 *src = src0;
   116     uint32 *left = (uint32 *) (leftpos + SPU_RAM_BASE);
   116     uint32 *left = (uint32 *) (leftpos + SPU_RAM_BASE);
   117     uint32 *right = (uint32 *) (rightpos + SPU_RAM_BASE);
   117     uint32 *right = (uint32 *) (rightpos + SPU_RAM_BASE);
   118     size = (size + 7) / 8;
   118     size = (size + 7) / 8;
   124         rval |= (*src++) << 8;
   124         rval |= (*src++) << 8;
   125         lval |= (*src++) << 16;
   125         lval |= (*src++) << 16;
   126         rval |= (*src++) << 16;
   126         rval |= (*src++) << 16;
   127         lval |= (*src++) << 24;
   127         lval |= (*src++) << 24;
   128         rval |= (*src++) << 24;
   128         rval |= (*src++) << 24;
   129         g2_write_32 (left++, lval);
   129         g2_write_32(left++, lval);
   130         g2_write_32 (right++, rval);
   130         g2_write_32(right++, rval);
   131         g2_fifo_wait ();
   131         g2_fifo_wait();
   132     }
   132     }
   133 }
   133 }
   134 
   134 
   135 static void
   135 static void
   136 spu_memload_stereo16 (int leftpos, int rightpos, void *src0, size_t size)
   136 spu_memload_stereo16(int leftpos, int rightpos, void *src0, size_t size)
   137 {
   137 {
   138     uint16 *src = src0;
   138     uint16 *src = src0;
   139     uint32 *left = (uint32 *) (leftpos + SPU_RAM_BASE);
   139     uint32 *left = (uint32 *) (leftpos + SPU_RAM_BASE);
   140     uint32 *right = (uint32 *) (rightpos + SPU_RAM_BASE);
   140     uint32 *right = (uint32 *) (rightpos + SPU_RAM_BASE);
   141     size = (size + 7) / 8;
   141     size = (size + 7) / 8;
   143         unsigned lval, rval;
   143         unsigned lval, rval;
   144         lval = *src++;
   144         lval = *src++;
   145         rval = *src++;
   145         rval = *src++;
   146         lval |= (*src++) << 16;
   146         lval |= (*src++) << 16;
   147         rval |= (*src++) << 16;
   147         rval |= (*src++) << 16;
   148         g2_write_32 (left++, lval);
   148         g2_write_32(left++, lval);
   149         g2_write_32 (right++, rval);
   149         g2_write_32(right++, rval);
   150         g2_fifo_wait ();
   150         g2_fifo_wait();
   151     }
   151     }
   152 }
   152 }
   153 
   153 
   154 static void
   154 static void
   155 DCAUD_PlayAudio (_THIS)
   155 DCAUD_PlayAudio(_THIS)
   156 {
   156 {
   157     SDL_AudioSpec *spec = &this->spec;
   157     SDL_AudioSpec *spec = &this->spec;
   158     unsigned int offset;
   158     unsigned int offset;
   159 
   159 
   160     if (this->hidden->playing) {
   160     if (this->hidden->playing) {
   161         /* wait */
   161         /* wait */
   162         while (aica_get_pos (0) / spec->samples == this->hidden->nextbuf) {
   162         while (aica_get_pos(0) / spec->samples == this->hidden->nextbuf) {
   163             thd_pass ();
   163             thd_pass();
   164         }
   164         }
   165     }
   165     }
   166 
   166 
   167     offset = this->hidden->nextbuf * spec->size;
   167     offset = this->hidden->nextbuf * spec->size;
   168     this->hidden->nextbuf ^= 1;
   168     this->hidden->nextbuf ^= 1;
   169     /* Write the audio data, checking for EAGAIN on broken audio drivers */
   169     /* Write the audio data, checking for EAGAIN on broken audio drivers */
   170     if (spec->channels == 1) {
   170     if (spec->channels == 1) {
   171         spu_memload (this->hidden->leftpos + offset, this->hidden->mixbuf,
   171         spu_memload(this->hidden->leftpos + offset, this->hidden->mixbuf,
   172                      this->hidden->mixlen);
   172                     this->hidden->mixlen);
   173     } else {
   173     } else {
   174         offset /= 2;
   174         offset /= 2;
   175         if ((this->spec.format & 255) == 8) {
   175         if ((this->spec.format & 255) == 8) {
   176             spu_memload_stereo8 (this->hidden->leftpos + offset,
   176             spu_memload_stereo8(this->hidden->leftpos + offset,
       
   177                                 this->hidden->rightpos + offset,
       
   178                                 this->hidden->mixbuf, this->hidden->mixlen);
       
   179         } else {
       
   180             spu_memload_stereo16(this->hidden->leftpos + offset,
   177                                  this->hidden->rightpos + offset,
   181                                  this->hidden->rightpos + offset,
   178                                  this->hidden->mixbuf, this->hidden->mixlen);
   182                                  this->hidden->mixbuf, this->hidden->mixlen);
   179         } else {
       
   180             spu_memload_stereo16 (this->hidden->leftpos + offset,
       
   181                                   this->hidden->rightpos + offset,
       
   182                                   this->hidden->mixbuf, this->hidden->mixlen);
       
   183         }
   183         }
   184     }
   184     }
   185 
   185 
   186     if (!this->hidden->playing) {
   186     if (!this->hidden->playing) {
   187         int mode;
   187         int mode;
   188         this->hidden->playing = 1;
   188         this->hidden->playing = 1;
   189         mode = (spec->format == AUDIO_S8) ? SM_8BIT : SM_16BIT;
   189         mode = (spec->format == AUDIO_S8) ? SM_8BIT : SM_16BIT;
   190         if (spec->channels == 1) {
   190         if (spec->channels == 1) {
   191             aica_play (0, mode, this->hidden->leftpos, 0,
   191             aica_play(0, mode, this->hidden->leftpos, 0,
   192                        spec->samples * 2, spec->freq, 255, 128, 1);
   192                       spec->samples * 2, spec->freq, 255, 128, 1);
   193         } else {
   193         } else {
   194             aica_play (0, mode, this->hidden->leftpos, 0,
   194             aica_play(0, mode, this->hidden->leftpos, 0,
   195                        spec->samples * 2, spec->freq, 255, 0, 1);
   195                       spec->samples * 2, spec->freq, 255, 0, 1);
   196             aica_play (1, mode, this->hidden->rightpos, 0,
   196             aica_play(1, mode, this->hidden->rightpos, 0,
   197                        spec->samples * 2, spec->freq, 255, 255, 1);
   197                       spec->samples * 2, spec->freq, 255, 255, 1);
   198         }
   198         }
   199     }
   199     }
   200 }
   200 }
   201 
   201 
   202 static Uint8 *
   202 static Uint8 *
   203 DCAUD_GetAudioBuf (_THIS)
   203 DCAUD_GetAudioBuf(_THIS)
   204 {
   204 {
   205     return (this->hidden->mixbuf);
   205     return (this->hidden->mixbuf);
   206 }
   206 }
   207 
   207 
   208 static void
   208 static void
   209 DCAUD_CloseAudio (_THIS)
   209 DCAUD_CloseAudio(_THIS)
   210 {
   210 {
   211     aica_stop (0);
   211     aica_stop(0);
   212     if (this->spec.channels == 2)
   212     if (this->spec.channels == 2)
   213         aica_stop (1);
   213         aica_stop(1);
   214     if (this->hidden->mixbuf != NULL) {
   214     if (this->hidden->mixbuf != NULL) {
   215         SDL_FreeAudioMem (this->hidden->mixbuf);
   215         SDL_FreeAudioMem(this->hidden->mixbuf);
   216         this->hidden->mixbuf = NULL;
   216         this->hidden->mixbuf = NULL;
   217     }
   217     }
   218 }
   218 }
   219 
   219 
   220 static int
   220 static int
   221 DCAUD_OpenAudio (_THIS, SDL_AudioSpec * spec)
   221 DCAUD_OpenAudio(_THIS, SDL_AudioSpec * spec)
   222 {
   222 {
   223     switch (spec->format & 0xff) {
   223     switch (spec->format & 0xff) {
   224     case 8:
   224     case 8:
   225         spec->format = AUDIO_S8;
   225         spec->format = AUDIO_S8;
   226         break;
   226         break;
   227     case 16:
   227     case 16:
   228         spec->format = AUDIO_S16LSB;
   228         spec->format = AUDIO_S16LSB;
   229         break;
   229         break;
   230     default:
   230     default:
   231         SDL_SetError ("Unsupported audio format");
   231         SDL_SetError("Unsupported audio format");
   232         return (-1);
   232         return (-1);
   233     }
   233     }
   234 
   234 
   235     /* Update the fragment size as size in bytes */
   235     /* Update the fragment size as size in bytes */
   236     SDL_CalculateAudioSpec (spec);
   236     SDL_CalculateAudioSpec(spec);
   237 
   237 
   238     /* Allocate mixing buffer */
   238     /* Allocate mixing buffer */
   239     this->hidden->mixlen = spec->size;
   239     this->hidden->mixlen = spec->size;
   240     this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem (this->hidden->mixlen);
   240     this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   241     if (this->hidden->mixbuf == NULL) {
   241     if (this->hidden->mixbuf == NULL) {
   242         return (-1);
   242         return (-1);
   243     }
   243     }
   244     SDL_memset (this->hidden->mixbuf, spec->silence, spec->size);
   244     SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   245     this->hidden->leftpos = 0x11000;
   245     this->hidden->leftpos = 0x11000;
   246     this->hidden->rightpos = 0x11000 + spec->size;
   246     this->hidden->rightpos = 0x11000 + spec->size;
   247     this->hidden->playing = 0;
   247     this->hidden->playing = 0;
   248     this->hidden->nextbuf = 0;
   248     this->hidden->nextbuf = 0;
   249 
   249