src/audio/dc/SDL_dcaudio.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 
    22 */
    23 #include "SDL_config.h"
    24 
    25 /* Output dreamcast aica */
    26 
    27 #include "SDL_timer.h"
    28 #include "SDL_audio.h"
    29 #include "../SDL_audiomem.h"
    30 #include "../SDL_audio_c.h"
    31 #include "../SDL_audiodev_c.h"
    32 #include "SDL_dcaudio.h"
    33 
    34 #include "aica.h"
    35 #include <dc/spu.h>
    36 
    37 /* Audio driver functions */
    38 static int DCAUD_OpenAudio(_THIS, SDL_AudioSpec * spec);
    39 static void DCAUD_WaitAudio(_THIS);
    40 static void DCAUD_PlayAudio(_THIS);
    41 static Uint8 *DCAUD_GetAudioBuf(_THIS);
    42 static void DCAUD_CloseAudio(_THIS);
    43 
    44 /* Audio driver bootstrap functions */
    45 static int
    46 DCAUD_Available(void)
    47 {
    48     return 1;
    49 }
    50 
    51 static void
    52 DCAUD_DeleteDevice(SDL_AudioDevice * device)
    53 {
    54     SDL_free(device->hidden);
    55     SDL_free(device);
    56 }
    57 
    58 static SDL_AudioDevice *
    59 DCAUD_CreateDevice(int devindex)
    60 {
    61     SDL_AudioDevice *this;
    62 
    63     /* Initialize all variables that we clean on shutdown */
    64     this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
    65     if (this) {
    66         SDL_memset(this, 0, (sizeof *this));
    67         this->hidden = (struct SDL_PrivateAudioData *)
    68             SDL_malloc((sizeof *this->hidden));
    69     }
    70     if ((this == NULL) || (this->hidden == NULL)) {
    71         SDL_OutOfMemory();
    72         if (this) {
    73             SDL_free(this);
    74         }
    75         return (0);
    76     }
    77     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    78 
    79     /* Set the function pointers */
    80     this->OpenAudio = DCAUD_OpenAudio;
    81     this->WaitAudio = DCAUD_WaitAudio;
    82     this->PlayAudio = DCAUD_PlayAudio;
    83     this->GetAudioBuf = DCAUD_GetAudioBuf;
    84     this->CloseAudio = DCAUD_CloseAudio;
    85 
    86     this->free = DCAUD_DeleteDevice;
    87 
    88     spu_init();
    89 
    90     return this;
    91 }
    92 
    93 AudioBootStrap DCAUD_bootstrap = {
    94     "dcaudio", "Dreamcast AICA audio",
    95     DCAUD_Available, DCAUD_CreateDevice
    96 };
    97 
    98 /* This function waits until it is possible to write a full sound buffer */
    99 static void
   100 DCAUD_WaitAudio(_THIS)
   101 {
   102     if (this->hidden->playing) {
   103         /* wait */
   104         while (aica_get_pos(0) / this->spec.samples == this->hidden->nextbuf) {
   105             thd_pass();
   106         }
   107     }
   108 }
   109 
   110 #define	SPU_RAM_BASE	0xa0800000
   111 
   112 static void
   113 spu_memload_stereo8(int leftpos, int rightpos, void *src0, size_t size)
   114 {
   115     uint8 *src = src0;
   116     uint32 *left = (uint32 *) (leftpos + SPU_RAM_BASE);
   117     uint32 *right = (uint32 *) (rightpos + SPU_RAM_BASE);
   118     size = (size + 7) / 8;
   119     while (size--) {
   120         unsigned lval, rval;
   121         lval = *src++;
   122         rval = *src++;
   123         lval |= (*src++) << 8;
   124         rval |= (*src++) << 8;
   125         lval |= (*src++) << 16;
   126         rval |= (*src++) << 16;
   127         lval |= (*src++) << 24;
   128         rval |= (*src++) << 24;
   129         g2_write_32(left++, lval);
   130         g2_write_32(right++, rval);
   131         g2_fifo_wait();
   132     }
   133 }
   134 
   135 static void
   136 spu_memload_stereo16(int leftpos, int rightpos, void *src0, size_t size)
   137 {
   138     uint16 *src = src0;
   139     uint32 *left = (uint32 *) (leftpos + SPU_RAM_BASE);
   140     uint32 *right = (uint32 *) (rightpos + SPU_RAM_BASE);
   141     size = (size + 7) / 8;
   142     while (size--) {
   143         unsigned lval, rval;
   144         lval = *src++;
   145         rval = *src++;
   146         lval |= (*src++) << 16;
   147         rval |= (*src++) << 16;
   148         g2_write_32(left++, lval);
   149         g2_write_32(right++, rval);
   150         g2_fifo_wait();
   151     }
   152 }
   153 
   154 static void
   155 DCAUD_PlayAudio(_THIS)
   156 {
   157     SDL_AudioSpec *spec = &this->spec;
   158     unsigned int offset;
   159 
   160     if (this->hidden->playing) {
   161         /* wait */
   162         while (aica_get_pos(0) / spec->samples == this->hidden->nextbuf) {
   163             thd_pass();
   164         }
   165     }
   166 
   167     offset = this->hidden->nextbuf * spec->size;
   168     this->hidden->nextbuf ^= 1;
   169     /* Write the audio data, checking for EAGAIN on broken audio drivers */
   170     if (spec->channels == 1) {
   171         spu_memload(this->hidden->leftpos + offset, this->hidden->mixbuf,
   172                     this->hidden->mixlen);
   173     } else {
   174         offset /= 2;
   175         if ((this->spec.format & 255) == 8) {
   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,
   181                                  this->hidden->rightpos + offset,
   182                                  this->hidden->mixbuf, this->hidden->mixlen);
   183         }
   184     }
   185 
   186     if (!this->hidden->playing) {
   187         int mode;
   188         this->hidden->playing = 1;
   189         mode = (spec->format == AUDIO_S8) ? SM_8BIT : SM_16BIT;
   190         if (spec->channels == 1) {
   191             aica_play(0, mode, this->hidden->leftpos, 0,
   192                       spec->samples * 2, spec->freq, 255, 128, 1);
   193         } else {
   194             aica_play(0, mode, this->hidden->leftpos, 0,
   195                       spec->samples * 2, spec->freq, 255, 0, 1);
   196             aica_play(1, mode, this->hidden->rightpos, 0,
   197                       spec->samples * 2, spec->freq, 255, 255, 1);
   198         }
   199     }
   200 }
   201 
   202 static Uint8 *
   203 DCAUD_GetAudioBuf(_THIS)
   204 {
   205     return (this->hidden->mixbuf);
   206 }
   207 
   208 static void
   209 DCAUD_CloseAudio(_THIS)
   210 {
   211     aica_stop(0);
   212     if (this->spec.channels == 2)
   213         aica_stop(1);
   214     if (this->hidden->mixbuf != NULL) {
   215         SDL_FreeAudioMem(this->hidden->mixbuf);
   216         this->hidden->mixbuf = NULL;
   217     }
   218 }
   219 
   220 static int
   221 DCAUD_OpenAudio(_THIS, SDL_AudioSpec * spec)
   222 {
   223     switch (spec->format & 0xff) {
   224     case 8:
   225         spec->format = AUDIO_S8;
   226         break;
   227     case 16:
   228         spec->format = AUDIO_S16LSB;
   229         break;
   230     default:
   231         SDL_SetError("Unsupported audio format");
   232         return (-1);
   233     }
   234 
   235     /* Update the fragment size as size in bytes */
   236     SDL_CalculateAudioSpec(spec);
   237 
   238     /* Allocate mixing buffer */
   239     this->hidden->mixlen = spec->size;
   240     this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   241     if (this->hidden->mixbuf == NULL) {
   242         return (-1);
   243     }
   244     SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   245     this->hidden->leftpos = 0x11000;
   246     this->hidden->rightpos = 0x11000 + spec->size;
   247     this->hidden->playing = 0;
   248     this->hidden->nextbuf = 0;
   249 
   250     /* We're ready to rock and roll. :-) */
   251     return (0);
   252 }
   253 
   254 /* vi: set ts=4 sw=4 expandtab: */