1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
#include "gvDevice.h"
#include "gvCodec.h"
#include "gvSource.h"
#include "gvUtil.h"
#include <audioinput.h>
#if !defined(_PSP)
#error This file should only be used with the PSP
#endif
/************
** DEFINES **
************/
// the number of samples to capture at a time
// is a multiple of 64 and 160
// at 11025hz, corresponds to about 30ms
#define GVI_INPUT_LEN 320
#define GVI_CAPTURE_THREAD_STACK_SIZE (1024 * 4)
// ADC settings
#define GVI_ADC_ALC -6 // -6 dB
#define GVI_ADC_GAIN +30 // +30 dB
#define GVI_ADC_NOIZ -60 // -60 dB
#define GVI_ADC_HOLD 0 // 0 ms
#define GVI_ADC_DECAY 3 // 192 ms
#define GVI_ADC_ATTACK 2 // 24 ms
/**********
** TYPES **
**********/
typedef struct
{
GVBool m_capturing;
GVScalar m_captureVolume;
GVFrameStamp m_captureClock;
GVScalar m_captureThreshold;
GVFrameStamp m_captureLastCrossedThresholdTime;
GVSample * m_captureBuffer;
size_t m_captureBufferLen; // len in samples (not bytes)
size_t m_captureBufferWritePos;
size_t m_captureBufferReadPos;
SceUID m_captureBufferSemaphore;
SceUID m_captureThreadID;
GVBool m_captureThreadStop;
} GVIHardwareData;
/************
** GLOBALS **
************/
static GVIDevice * GVIPSPDevice = NULL;
/**************
** FUNCTIONS **
**************/
static void gviHardwareFreeDevice(GVIDevice * device);
static GVBool gviWaitSemaphore(GVIHardwareData * data)
{
int rcode = sceKernelWaitSema(data->m_captureBufferSemaphore, 1, NULL);
if(rcode == 0)
return GVTrue;
return GVFalse;
}
static GVBool gviSignalSemaphore(GVIHardwareData * data)
{
int rcode = sceKernelSignalSema(data->m_captureBufferSemaphore, 1);
if(rcode == 0)
return GVTrue;
return GVFalse;
}
GVBool gviHardwareStartup(void)
{
SceAudioInputParam param;
int rcode;
// init mic capture
param.alc = GVI_ADC_ALC;
param.gain = GVI_ADC_GAIN;
param.noiz = GVI_ADC_NOIZ;
param.hold = GVI_ADC_HOLD;
param.decay = GVI_ADC_DECAY;
param.attack = GVI_ADC_ATTACK;
rcode = sceAudioInputInitEx(¶m);
if(rcode < 0)
return GVFalse;
return GVTrue;
}
void gviHardwareCleanup(void)
{
if(GVIPSPDevice)
{
gviHardwareFreeDevice(GVIPSPDevice);
GVIPSPDevice = NULL;
}
}
void gviHardwareThink(void)
{
// no thinking needed
}
int gviHardwareListDevices(GVDeviceInfo devices[], int maxDevices, GVDeviceType types)
{
if(maxDevices < 1)
return 0;
if(!(types & GV_CAPTURE))
return 0;
memset(&devices[0], 0, sizeof(GVDeviceInfo));
devices[0].m_id = 0;
strcpy(devices[0].m_name, _T("Headset Mic"));
devices[0].m_deviceType = GV_CAPTURE;
devices[0].m_defaultDevice = GV_CAPTURE;
devices[0].m_hardwareType = GVHardwarePSPHeadset;
return 1;
}
static void gviHardwareFreeDevice(GVIDevice * device)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
// don't track this device anymore
GVIPSPDevice = NULL;
// tell the thread to stop
// the thread will free the device after it stops
data->m_captureThreadStop = GVTrue;
}
static GVBool gviHardwareStartDevice(GVIDevice * device, GVDeviceType type)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
if(type != GV_CAPTURE)
return GVFalse;
// already capturing?
if(data->m_capturing == GVTrue)
return GVTrue;
// set vars
data->m_captureBufferWritePos = 0;
data->m_captureBufferReadPos = 0;
// start capturing
data->m_capturing = GVTrue;
return GVTrue;
}
static void gviHardwareStopDevice(GVIDevice * device, GVDeviceType type)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
if(type != GV_CAPTURE)
return;
// stop capturing
data->m_capturing = GVFalse;
// increment the clock so new audio isn't contiguous
data->m_captureClock++;
}
static GVBool gviHardwareIsDeviceStarted(GVIDevice * device, GVDeviceType type)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
if(type != GV_CAPTURE)
return GVFalse;
return data->m_capturing;
}
static void gviHardwareSetDeviceVolume(GVIDevice * device, GVDeviceType type, GVScalar volume)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
if(type != GV_CAPTURE)
return;
data->m_captureVolume = volume;
}
static GVScalar gviHardwareGetDeviceVolume(GVIDevice * device, GVDeviceType type)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
if(type != GV_CAPTURE)
return (GVScalar)0;
return data->m_captureVolume;
}
static void gviHardwareSetCaptureThreshold(GVIDevice * device, GVScalar threshold)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
data->m_captureThreshold = threshold;
}
static GVScalar gviHardwareGetCaptureThreshold(GVIDevice * device)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
return data->m_captureThreshold;
}
static int gviHardwareGetAvailableCaptureBytes(GVDevice device)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
int numSamples;
if(!data->m_capturing)
return GVFalse;
// figure out how many samples are available
gviWaitSemaphore(data);
if(data->m_captureBufferWritePos < data->m_captureBufferReadPos)
numSamples = (data->m_captureBufferLen - data->m_captureBufferReadPos);
else
numSamples = (data->m_captureBufferWritePos - data->m_captureBufferReadPos);
gviSignalSemaphore(data);
return numSamples;
}
static GVBool gviHardwareCapturePacket(GVDevice device, GVByte * packet, int * len, GVFrameStamp * frameStamp, GVScalar * volume)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
int numBytes;
int numSamples;
int numFrames;
GVBool overThreshold = GVFalse;
int lenAvailable;
int framesAvailable;
int i, j;
GVSample * readPtr;
if(!data->m_capturing)
return GVFalse;
// figure out how many encoded bytes they can handle
lenAvailable = *len;
// clear the len and volume
*len = 0;
if(volume)
*volume = 0;
// figure out how many bytes can be captured
numSamples = gviHardwareGetAvailableCaptureBytes(device);
numBytes = (numSamples * GV_BYTES_PER_SAMPLE);
// figure out how many frames that is
numFrames = (numBytes / GVIBytesPerFrame);
if(numFrames == 0)
return GVFalse;
// figure out how many frames they can handle
framesAvailable = (lenAvailable / GVIEncodedFrameSize);
// don't give them more frames than they can handle
numFrames = min(numFrames, framesAvailable);
if(!numFrames)
return GVFalse;
// figure out how many bytes to capture
numBytes = (numFrames * GVIBytesPerFrame);
numSamples = (numBytes / GV_BYTES_PER_SAMPLE);
// get the read pointer
readPtr = (data->m_captureBuffer + data->m_captureBufferReadPos);
// get the volume if they're interested
if(volume)
*volume = gviGetSamplesVolume(readPtr, numSamples);
// check against the threshold
if(volume)
{
// we already got the volume, so use that to check
overThreshold = (*volume >= data->m_captureThreshold);
}
else
{
// we didn't get a volume, so check the samples directly
overThreshold = gviIsOverThreshold(readPtr, numSamples, data->m_captureThreshold);
}
// did the audio cross the threshold?
if(overThreshold)
{
// update the time at which we crossed
data->m_captureLastCrossedThresholdTime = data->m_captureClock;
}
else
{
// check if we are still within the hold time
overThreshold = ((GVFrameStamp)(data->m_captureClock - data->m_captureLastCrossedThresholdTime) < GVI_HOLD_THRESHOLD_FRAMES);
}
if(overThreshold)
{
// store the framestamp
*frameStamp = data->m_captureClock;
// handle the data one frame at a time
for(i = 0 ; i < numFrames ; i++)
{
// scale the data
if(data->m_captureVolume < 1.0)
{
for(j = 0 ; j < GVISamplesPerFrame ; j++)
readPtr[j] = (GVSample)(readPtr[j] * data->m_captureVolume);
}
// filter
if(device->m_captureFilterCallback)
device->m_captureFilterCallback(device, readPtr, (GVFrameStamp)(data->m_captureClock + i));
// encode the buffer into the packet
gviEncode(packet + (GVIEncodedFrameSize * i), readPtr);
// update the loop info as needed
readPtr += GVISamplesPerFrame;
}
}
// advance the read position and clock
data->m_captureBufferReadPos += numSamples;
data->m_captureBufferReadPos %= data->m_captureBufferLen;
data->m_captureClock += numFrames;
// set the len
*len = (numFrames * GVIEncodedFrameSize);
// return false if we didn't get a packet
if(!overThreshold)
return GVFalse;
return GVTrue;
}
static int gviPSPCaptureThread(SceSize args, void * argp)
{
GVIDevice * device = *(GVIDevice **)argp;
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
int rcode;
assert(device);
assert(args == sizeof(GVIDevice*));
// loop until we're told to stop
while(!data->m_captureThreadStop)
{
// are we capturing?
if(data->m_capturing)
{
// get some input
rcode = sceAudioInputBlocking(GVI_INPUT_LEN, GV_SAMPLES_PER_SECOND,
data->m_captureBuffer + data->m_captureBufferWritePos);
if(rcode < 0)
{
gviDeviceUnplugged(device);
}
else
{
gviWaitSemaphore(data);
data->m_captureBufferWritePos += GVI_INPUT_LEN;
data->m_captureBufferWritePos %= data->m_captureBufferLen;
gviSignalSemaphore(data);
}
}
else
{
// sleep
msleep(10);
}
}
// end sampling
// if another device has already been created, don't do this
if(GVIPSPDevice != NULL)
sceAudioInputBlocking(GVI_INPUT_LEN, GV_SAMPLES_PER_SECOND, NULL);
// free the device and its memory
gsifree(data->m_captureBuffer);
sceKernelDeleteSema(data->m_captureBufferSemaphore);
gviFreeDevice(device);
sceKernelExitThread(0);
return 0;
}
static GVBool gviPSPAudioInitDevice(GVIDevice *device)
{
GVIHardwareData * data = (GVIHardwareData *)device->m_data;
size_t size;
size_t oldSize;
int rcode;
// create a semaphore
data->m_captureBufferSemaphore = sceKernelCreateSema("capture buffer", SCE_KERNEL_SA_THFIFO, 1, 1, NULL);
if(data->m_captureBufferSemaphore <= 0)
return GVFalse;
// figure out the buffer size
size = gviMultiplyByBytesPerMillisecond(GVI_CAPTURE_BUFFER_MILLISECONDS);
size /= GV_BYTES_PER_SAMPLE; // convert from bytes to samples
do
{
// it needs to be a multiple of both the frame size and the input len
oldSize = size;
size = gviRoundUpToNearestMultiple(size, GVISamplesPerFrame);
size = gviRoundUpToNearestMultiple(size, GVI_INPUT_LEN);
}
while(size != oldSize);
data->m_captureBufferLen = size;
// allocate the buffer
data->m_captureBuffer = (GVSample *)gsimalloc(size * GV_BYTES_PER_SAMPLE);
if(!data->m_captureBuffer)
{
sceKernelDeleteSema(data->m_captureBufferSemaphore);
return GVFalse;
}
// create capture thread
data->m_captureThreadID = sceKernelCreateThread("capture",gviPSPCaptureThread,
SCE_KERNEL_USER_HIGHEST_PRIORITY, GVI_CAPTURE_THREAD_STACK_SIZE, 0, NULL);
if(data->m_captureThreadID < 0)
{
gsifree(data->m_captureBuffer);
sceKernelDeleteSema(data->m_captureBufferSemaphore);
return GVFalse;
}
// start capture thread
rcode = sceKernelStartThread(data->m_captureThreadID, sizeof(GVIDevice*), &device);
if(rcode < 0)
{
sceKernelDeleteThread(data->m_captureThreadID);
gsifree(data->m_captureBuffer);
sceKernelDeleteSema(data->m_captureBufferSemaphore);
return GVFalse;
}
return GVTrue;
}
GVDevice gviHardwareNewDevice(GVDeviceID deviceID, GVDeviceType type)
{
GVIDevice * device;
GVIHardwareData * data;
GVBool result;
// we only do capture
if(type != GV_CAPTURE)
return NULL;
// there can only be one device at a time
if(GVIPSPDevice != NULL)
return NULL;
// create the device
device = gviNewDevice(deviceID, GVHardwarePSPHeadset, type, sizeof(GVIHardwareData));
if(!device)
return NULL;
// init the device
result = gviPSPAudioInitDevice(device);
if(result == GVFalse)
{
gviFreeDevice(device);
return NULL;
}
// store the pointers
device->m_methods.m_freeDevice = gviHardwareFreeDevice;
device->m_methods.m_startDevice = gviHardwareStartDevice;
device->m_methods.m_stopDevice = gviHardwareStopDevice;
device->m_methods.m_isDeviceStarted = gviHardwareIsDeviceStarted;
device->m_methods.m_setDeviceVolume = gviHardwareSetDeviceVolume;
device->m_methods.m_getDeviceVolume = gviHardwareGetDeviceVolume;
device->m_methods.m_setCaptureThreshold = gviHardwareSetCaptureThreshold;
device->m_methods.m_getCaptureThreshold = gviHardwareGetCaptureThreshold;
device->m_methods.m_getAvailableCaptureBytes = gviHardwareGetAvailableCaptureBytes;
device->m_methods.m_capturePacket = gviHardwareCapturePacket;
// get a pointer to the data
data = (GVIHardwareData *)device->m_data;
// init vars
data->m_captureVolume = (GVScalar)1.0;
data->m_captureClock = 0;
data->m_captureLastCrossedThresholdTime = (data->m_captureClock - GVI_HOLD_THRESHOLD_FRAMES - 1);
GVIPSPDevice = device;
return device;
}
|