src/audio/psp/SDL_pspaudio.c
author Captain Lex
Sun, 17 Mar 2013 20:07:02 +0800
changeset 7009 161b7b6a5303
child 7038 7f22b9ba218f
permissions -rw-r--r--
Add PSP support
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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 
    22 #include <stdio.h>
    23 #include <string.h>
    24 #include <stdlib.h>
    25 #include <malloc.h>
    26 
    27 #include "SDL_audio.h"
    28 #include "SDL_error.h"
    29 #include "SDL_timer.h"
    30 #include "../SDL_audiomem.h"
    31 #include "../SDL_audio_c.h"
    32 #include "../SDL_audiodev_c.h"
    33 #include "../SDL_sysaudio.h"
    34 #include "SDL_pspaudio.h"
    35 
    36 #include <pspaudio.h>
    37 #include <pspthreadman.h>
    38 
    39 /* The tag name used by PSP audio */
    40 #define PSPAUD_DRIVER_NAME         "psp"
    41 
    42 static int
    43 PSPAUD_OpenDevice(_THIS, const char *devname, int iscapture)
    44 {
    45 	int format, mixlen, i;
    46     this->hidden = (struct SDL_PrivateAudioData *)
    47         SDL_malloc(sizeof(*this->hidden));
    48     if (this->hidden == NULL) {
    49         SDL_OutOfMemory();
    50         return 0;
    51     }
    52     SDL_memset(this->hidden, 0, sizeof(*this->hidden));
    53 	switch (this->spec.format & 0xff) {
    54 		case 8:
    55 		case 16:
    56 			this->spec.format = AUDIO_S16LSB;
    57 			break;
    58 		default:
    59 			SDL_SetError("Unsupported audio format");
    60 			return 0;
    61 	}
    62 
    63 	/* The sample count must be a multiple of 64. */
    64 	this->spec.samples = PSP_AUDIO_SAMPLE_ALIGN(this->spec.samples);
    65 	this->spec.freq = 44100;
    66 
    67 	/* Update the fragment size as size in bytes. */
    68 //	SDL_CalculateAudioSpec(this->spec); MOD
    69     switch (this->spec.format) {
    70     case AUDIO_U8:
    71         this->spec.silence = 0x80;
    72         break;
    73     default:
    74         this->spec.silence = 0x00;
    75         break;
    76     }
    77     this->spec.size = SDL_AUDIO_BITSIZE(this->spec.format) / 8;
    78     this->spec.size *= this->spec.channels;
    79     this->spec.size *= this->spec.samples;
    80     
    81 //==========================================
    82 
    83 	/* Allocate the mixing buffer.  Its size and starting address must
    84 	   be a multiple of 64 bytes.  Our sample count is already a multiple of
    85 	   64, so spec->size should be a multiple of 64 as well. */
    86 	mixlen = this->spec.size * NUM_BUFFERS;
    87 	this->hidden->rawbuf = (Uint8 *) memalign(64, mixlen);
    88 	if (this->hidden->rawbuf == NULL) {
    89 		SDL_SetError("Couldn't allocate mixing buffer");
    90 		return 0;
    91 	}
    92 
    93 	/* Setup the hardware channel. */
    94 	if (this->spec.channels == 1) {
    95 		format = PSP_AUDIO_FORMAT_MONO;
    96 	} else {
    97 		format = PSP_AUDIO_FORMAT_STEREO;
    98 	}
    99 	this->hidden->channel = sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL, this->spec.samples, format);
   100 	if (this->hidden->channel < 0) {
   101 		SDL_SetError("Couldn't reserve hardware channel");
   102 		free(this->hidden->rawbuf);
   103 		this->hidden->rawbuf = NULL;
   104 		return 0;
   105 	}
   106 
   107 	memset(this->hidden->rawbuf, 0, mixlen);
   108 	for (i = 0; i < NUM_BUFFERS; i++) {
   109 		this->hidden->mixbufs[i] = &this->hidden->rawbuf[i * this->spec.size];
   110 	}
   111 
   112 	this->hidden->next_buffer = 0;
   113 	return 1;
   114 }
   115 
   116 static void PSPAUD_PlayDevice(_THIS)
   117 {
   118 	Uint8 *mixbuf = this->hidden->mixbufs[this->hidden->next_buffer];
   119 
   120 	if (this->spec.channels == 1) {
   121 		sceAudioOutputBlocking(this->hidden->channel, PSP_AUDIO_VOLUME_MAX, mixbuf);
   122 	} else {
   123 		sceAudioOutputPannedBlocking(this->hidden->channel, PSP_AUDIO_VOLUME_MAX, PSP_AUDIO_VOLUME_MAX, mixbuf);
   124 	}
   125 
   126 	this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
   127 }
   128 
   129 /* This function waits until it is possible to write a full sound buffer */
   130 static void PSPAUD_WaitDevice(_THIS)
   131 {
   132 	/* Because we block when sending audio, there's no need for this function to do anything. */
   133 }
   134 static Uint8 *PSPAUD_GetDeviceBuf(_THIS)
   135 {
   136 	return this->hidden->mixbufs[this->hidden->next_buffer];
   137 }
   138 
   139 static void PSPAUD_CloseDevice(_THIS)
   140 {
   141 	if (this->hidden->channel >= 0) {
   142 		sceAudioChRelease(this->hidden->channel);
   143 		this->hidden->channel = -1;
   144 	}
   145 
   146 	if (this->hidden->rawbuf != NULL) {
   147 		free(this->hidden->rawbuf);
   148 		this->hidden->rawbuf = NULL;
   149 	}
   150 }
   151 static void PSPAUD_ThreadInit(_THIS)
   152 {
   153 	/* Increase the priority of this audio thread by 1 to put it
   154 	   ahead of other SDL threads. */
   155 	SceUID thid;
   156 	SceKernelThreadInfo status;
   157 	thid = sceKernelGetThreadId();
   158 	status.size = sizeof(SceKernelThreadInfo);
   159 	if (sceKernelReferThreadStatus(thid, &status) == 0) {
   160 		sceKernelChangeThreadPriority(thid, status.currentPriority - 1);
   161 	}
   162 }
   163 
   164 
   165 static int
   166 PSPAUD_Init(SDL_AudioDriverImpl * impl)
   167 {
   168 
   169     // Set the function pointers 
   170     impl->OpenDevice = PSPAUD_OpenDevice;
   171     impl->PlayDevice = PSPAUD_PlayDevice;
   172     impl->WaitDevice = PSPAUD_WaitDevice;
   173     impl->GetDeviceBuf = PSPAUD_GetDeviceBuf;   
   174     impl->WaitDone = PSPAUD_WaitDevice;
   175     impl->CloseDevice = PSPAUD_CloseDevice;
   176     impl->ThreadInit = PSPAUD_ThreadInit;
   177     
   178     //PSP audio device
   179     impl->OnlyHasDefaultOutputDevice = 1;    
   180 /*
   181     impl->HasCaptureSupport = 1;
   182 
   183     impl->OnlyHasDefaultInputDevice = 1;
   184 */
   185     /*
   186     impl->DetectDevices = DSOUND_DetectDevices;
   187     impl->Deinitialize = DSOUND_Deinitialize;
   188     */
   189     return 1;   /* this audio target is available. */
   190 }
   191 
   192 AudioBootStrap PSPAUD_bootstrap = {
   193     "psp", "PSP audio driver", PSPAUD_Init, 0
   194 };
   195 
   196  /* SDL_AUDI*/
   197 
   198 
   199