src/audio/psp/SDL_pspaudio.c
author Ryan C. Gordon
Fri, 05 Aug 2016 01:44:41 -0400
changeset 10255 9530fc07da6c
parent 10058 d141e7945ddf
child 10256 620329de23d4
permissions -rw-r--r--
audio: Clean up some CloseDevice() interface details.

- It's now always called if device->hidden isn't NULL, even if OpenDevice()
failed halfway through. This lets implementation code not have to clean up
itself on every possible failure point; just return an error and SDL will
handle it for you.

- Implementations can assume this->hidden != NULL and not check for it.

- implementations don't have to set this->hidden = NULL when done, because
the caller is always about to free(this).

- Don't reset other fields that are in a block of memory about to be free()'d.

- Implementations all now free things like internal mix buffers last, after
closing devices and such, to guarantee they definitely aren't in use anymore
at the point of deallocation.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_AUDIO_DRIVER_PSP
    24 
    25 #include <stdio.h>
    26 #include <string.h>
    27 #include <stdlib.h>
    28 #include <malloc.h>
    29 
    30 #include "SDL_audio.h"
    31 #include "SDL_error.h"
    32 #include "SDL_timer.h"
    33 #include "../SDL_audiomem.h"
    34 #include "../SDL_audio_c.h"
    35 #include "../SDL_audiodev_c.h"
    36 #include "../SDL_sysaudio.h"
    37 #include "SDL_pspaudio.h"
    38 
    39 #include <pspaudio.h>
    40 #include <pspthreadman.h>
    41 
    42 /* The tag name used by PSP audio */
    43 #define PSPAUD_DRIVER_NAME         "psp"
    44 
    45 static int
    46 PSPAUD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
    47 {
    48     int format, mixlen, i;
    49     this->hidden = (struct SDL_PrivateAudioData *)
    50         SDL_malloc(sizeof(*this->hidden));
    51     if (this->hidden == NULL) {
    52         return SDL_OutOfMemory();
    53     }
    54     SDL_memset(this->hidden, 0, sizeof(*this->hidden));
    55     switch (this->spec.format & 0xff) {
    56         case 8:
    57         case 16:
    58             this->spec.format = AUDIO_S16LSB;
    59             break;
    60         default:
    61             return SDL_SetError("Unsupported audio format");
    62     }
    63 
    64     /* The sample count must be a multiple of 64. */
    65     this->spec.samples = PSP_AUDIO_SAMPLE_ALIGN(this->spec.samples);
    66     this->spec.freq = 44100;
    67 
    68     /* Update the fragment size as size in bytes. */
    69     SDL_CalculateAudioSpec(&this->spec);
    70 
    71     /* Allocate the mixing buffer.  Its size and starting address must
    72        be a multiple of 64 bytes.  Our sample count is already a multiple of
    73        64, so spec->size should be a multiple of 64 as well. */
    74     mixlen = this->spec.size * NUM_BUFFERS;
    75     this->hidden->rawbuf = (Uint8 *) memalign(64, mixlen);
    76     if (this->hidden->rawbuf == NULL) {
    77         return SDL_SetError("Couldn't allocate mixing buffer");
    78     }
    79 
    80     /* Setup the hardware channel. */
    81     if (this->spec.channels == 1) {
    82         format = PSP_AUDIO_FORMAT_MONO;
    83     } else {
    84         format = PSP_AUDIO_FORMAT_STEREO;
    85     }
    86     this->hidden->channel = sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL, this->spec.samples, format);
    87     if (this->hidden->channel < 0) {
    88         free(this->hidden->rawbuf);
    89         this->hidden->rawbuf = NULL;
    90         return SDL_SetError("Couldn't reserve hardware channel");
    91     }
    92 
    93     memset(this->hidden->rawbuf, 0, mixlen);
    94     for (i = 0; i < NUM_BUFFERS; i++) {
    95         this->hidden->mixbufs[i] = &this->hidden->rawbuf[i * this->spec.size];
    96     }
    97 
    98     this->hidden->next_buffer = 0;
    99     return 0;
   100 }
   101 
   102 static void PSPAUD_PlayDevice(_THIS)
   103 {
   104     Uint8 *mixbuf = this->hidden->mixbufs[this->hidden->next_buffer];
   105 
   106     if (this->spec.channels == 1) {
   107         sceAudioOutputBlocking(this->hidden->channel, PSP_AUDIO_VOLUME_MAX, mixbuf);
   108     } else {
   109         sceAudioOutputPannedBlocking(this->hidden->channel, PSP_AUDIO_VOLUME_MAX, PSP_AUDIO_VOLUME_MAX, mixbuf);
   110     }
   111 
   112     this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
   113 }
   114 
   115 /* This function waits until it is possible to write a full sound buffer */
   116 static void PSPAUD_WaitDevice(_THIS)
   117 {
   118     /* Because we block when sending audio, there's no need for this function to do anything. */
   119 }
   120 static Uint8 *PSPAUD_GetDeviceBuf(_THIS)
   121 {
   122     return this->hidden->mixbufs[this->hidden->next_buffer];
   123 }
   124 
   125 static void PSPAUD_CloseDevice(_THIS)
   126 {
   127     if (this->hidden->channel >= 0) {
   128         sceAudioChRelease(this->hidden->channel);
   129     }
   130     free(this->hidden->rawbuf);  /* this uses memalign(), not SDL_malloc(). */
   131     SDL_free(this->hidden);
   132 }
   133 
   134 static void PSPAUD_ThreadInit(_THIS)
   135 {
   136     /* Increase the priority of this audio thread by 1 to put it
   137        ahead of other SDL threads. */
   138     SceUID thid;
   139     SceKernelThreadInfo status;
   140     thid = sceKernelGetThreadId();
   141     status.size = sizeof(SceKernelThreadInfo);
   142     if (sceKernelReferThreadStatus(thid, &status) == 0) {
   143         sceKernelChangeThreadPriority(thid, status.currentPriority - 1);
   144     }
   145 }
   146 
   147 
   148 static int
   149 PSPAUD_Init(SDL_AudioDriverImpl * impl)
   150 {
   151     /* Set the function pointers */
   152     impl->OpenDevice = PSPAUD_OpenDevice;
   153     impl->PlayDevice = PSPAUD_PlayDevice;
   154     impl->WaitDevice = PSPAUD_WaitDevice;
   155     impl->GetDeviceBuf = PSPAUD_GetDeviceBuf;
   156     impl->WaitDone = PSPAUD_WaitDevice;
   157     impl->CloseDevice = PSPAUD_CloseDevice;
   158     impl->ThreadInit = PSPAUD_ThreadInit;
   159 
   160     /* PSP audio device */
   161     impl->OnlyHasDefaultOutputDevice = 1;
   162 /*
   163     impl->HasCaptureSupport = 1;
   164 
   165     impl->OnlyHasDefaultInputDevice = 1;
   166 */
   167     /*
   168     impl->DetectDevices = DSOUND_DetectDevices;
   169     impl->Deinitialize = DSOUND_Deinitialize;
   170     */
   171     return 1;   /* this audio target is available. */
   172 }
   173 
   174 AudioBootStrap PSPAUD_bootstrap = {
   175     "psp", "PSP audio driver", PSPAUD_Init, 0
   176 };
   177 
   178  /* SDL_AUDI */
   179 
   180 #endif /* SDL_AUDIO_DRIVER_PSP */
   181 
   182 /* vi: set ts=4 sw=4 expandtab: */