SDL  2.0
SDL_pspaudio.c
Go to the documentation of this file.
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_audio_c.h"
34 #include "../SDL_audiodev_c.h"
35 #include "../SDL_sysaudio.h"
36 #include "SDL_pspaudio.h"
37 
38 #include <pspaudio.h>
39 #include <pspthreadman.h>
40 
41 /* The tag name used by PSP audio */
42 #define PSPAUDIO_DRIVER_NAME "psp"
43 
44 static int
45 PSPAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
46 {
47  int format, mixlen, i;
48  this->hidden = (struct SDL_PrivateAudioData *)
49  SDL_malloc(sizeof(*this->hidden));
50  if (this->hidden == NULL) {
51  return SDL_OutOfMemory();
52  }
53  SDL_zerop(this->hidden);
54  switch (this->spec.format & 0xff) {
55  case 8:
56  case 16:
57  this->spec.format = AUDIO_S16LSB;
58  break;
59  default:
60  return SDL_SetError("Unsupported audio format");
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. */
69 
70  /* Allocate the mixing buffer. Its size and starting address must
71  be a multiple of 64 bytes. Our sample count is already a multiple of
72  64, so spec->size should be a multiple of 64 as well. */
73  mixlen = this->spec.size * NUM_BUFFERS;
74  this->hidden->rawbuf = (Uint8 *) memalign(64, mixlen);
75  if (this->hidden->rawbuf == NULL) {
76  return SDL_SetError("Couldn't allocate mixing buffer");
77  }
78 
79  /* Setup the hardware channel. */
80  if (this->spec.channels == 1) {
81  format = PSP_AUDIO_FORMAT_MONO;
82  } else {
83  format = PSP_AUDIO_FORMAT_STEREO;
84  }
85  this->hidden->channel = sceAudioChReserve(PSP_AUDIO_NEXT_CHANNEL, this->spec.samples, format);
86  if (this->hidden->channel < 0) {
87  free(this->hidden->rawbuf);
88  this->hidden->rawbuf = NULL;
89  return SDL_SetError("Couldn't reserve hardware channel");
90  }
91 
92  memset(this->hidden->rawbuf, 0, mixlen);
93  for (i = 0; i < NUM_BUFFERS; i++) {
94  this->hidden->mixbufs[i] = &this->hidden->rawbuf[i * this->spec.size];
95  }
96 
97  this->hidden->next_buffer = 0;
98  return 0;
99 }
100 
101 static void PSPAUDIO_PlayDevice(_THIS)
102 {
103  Uint8 *mixbuf = this->hidden->mixbufs[this->hidden->next_buffer];
104 
105  if (this->spec.channels == 1) {
106  sceAudioOutputBlocking(this->hidden->channel, PSP_AUDIO_VOLUME_MAX, mixbuf);
107  } else {
108  sceAudioOutputPannedBlocking(this->hidden->channel, PSP_AUDIO_VOLUME_MAX, PSP_AUDIO_VOLUME_MAX, mixbuf);
109  }
110 
111  this->hidden->next_buffer = (this->hidden->next_buffer + 1) % NUM_BUFFERS;
112 }
113 
114 /* This function waits until it is possible to write a full sound buffer */
115 static void PSPAUDIO_WaitDevice(_THIS)
116 {
117  /* Because we block when sending audio, there's no need for this function to do anything. */
118 }
119 static Uint8 *PSPAUDIO_GetDeviceBuf(_THIS)
120 {
121  return this->hidden->mixbufs[this->hidden->next_buffer];
122 }
123 
124 static void PSPAUDIO_CloseDevice(_THIS)
125 {
126  if (this->hidden->channel >= 0) {
127  sceAudioChRelease(this->hidden->channel);
128  }
129  free(this->hidden->rawbuf); /* this uses memalign(), not SDL_malloc(). */
130  SDL_free(this->hidden);
131 }
132 
133 static void PSPAUDIO_ThreadInit(_THIS)
134 {
135  /* Increase the priority of this audio thread by 1 to put it
136  ahead of other SDL threads. */
137  SceUID thid;
138  SceKernelThreadInfo status;
139  thid = sceKernelGetThreadId();
140  status.size = sizeof(SceKernelThreadInfo);
141  if (sceKernelReferThreadStatus(thid, &status) == 0) {
142  sceKernelChangeThreadPriority(thid, status.currentPriority - 1);
143  }
144 }
145 
146 
147 static int
148 PSPAUDIO_Init(SDL_AudioDriverImpl * impl)
149 {
150  /* Set the function pointers */
151  impl->OpenDevice = PSPAUDIO_OpenDevice;
152  impl->PlayDevice = PSPAUDIO_PlayDevice;
153  impl->WaitDevice = PSPAUDIO_WaitDevice;
154  impl->GetDeviceBuf = PSPAUDIO_GetDeviceBuf;
155  impl->CloseDevice = PSPAUDIO_CloseDevice;
156  impl->ThreadInit = PSPAUDIO_ThreadInit;
157 
158  /* PSP audio device */
159  impl->OnlyHasDefaultOutputDevice = 1;
160 /*
161  impl->HasCaptureSupport = 1;
162 
163  impl->OnlyHasDefaultCaptureDevice = 1;
164 */
165  /*
166  impl->DetectDevices = DSOUND_DetectDevices;
167  impl->Deinitialize = DSOUND_Deinitialize;
168  */
169  return 1; /* this audio target is available. */
170 }
171 
173  "psp", "PSP audio driver", PSPAUDIO_Init, 0
174 };
175 
176  /* SDL_AUDI */
177 
178 #endif /* SDL_AUDIO_DRIVER_PSP */
179 
180 /* vi: set ts=4 sw=4 expandtab: */
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
AudioBootStrap PSPAUDIO_bootstrap
SDL_EventEntry * free
Definition: SDL_events.c:81
#define memset
Definition: SDL_malloc.c:639
void(* PlayDevice)(_THIS)
Definition: SDL_sysaudio.h:79
Uint16 samples
Definition: SDL_audio.h:174
void(* WaitDevice)(_THIS)
Definition: SDL_sysaudio.h:78
#define SDL_zerop(x)
Definition: SDL_stdinc.h:360
SDL_AudioSpec spec
Definition: loopwave.c:35
void(* ThreadInit)(_THIS)
Definition: SDL_sysaudio.h:77
Uint8 channels
Definition: SDL_audio.h:172
#define _THIS
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
void SDL_free(void *mem)
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1632
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
Uint32 size
Definition: SDL_audio.h:176
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:76
#define NUM_BUFFERS
Definition: SDL_pspaudio.h:30
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_SetError
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:85
SDL_AudioFormat format
Definition: SDL_audio.h:171
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
Uint8 *(* GetDeviceBuf)(_THIS)
Definition: SDL_sysaudio.h:81
#define SDL_malloc