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