/* * _al_PCMRatioify( ALuint ffreq, ALuint tfreq, * ALenum ffmt, ALenum tfmt, * ALuint samples ) * * Returns the number of byte necessary to contain samples worth of data, if * the data undergoes a conversion from ffreq to tfreq in the sampling-rate * and from ffmt to tfmt in terms of format. */ ALuint _al_PCMRatioify( ALuint ffreq, ALuint tfreq, ALenum ffmt, ALenum tfmt, ALuint samples ) { samples *= ((float) tfreq / ffreq ); samples *= (_al_formatbits( ffmt ) / 8 ); samples /= (_al_formatbits( tfmt ) / 8 ); return samples; }
ALboolean alutLoadWAV( const char *fname, void **wave, ALsizei *format, ALsizei *size, ALsizei *bits, ALsizei *freq ) { ALushort alFmt = 0; ALushort acChan = 0; ALushort acFreq = 0; ALuint acSize = 0; if(ReadWAVFile(fname, wave, &alFmt, &acChan, &acFreq, &acSize) == AL_FALSE) { _alDebug(ALD_CONVERT, __FILE__, __LINE__, "ReadWAVFile failed for %s", fname); return AL_FALSE; } /* set output params */ *format = (ALsizei) alFmt; *freq = (ALsizei) acFreq; *size = (ALsizei) acSize; *bits = (ALsizei) _al_formatbits(alFmt); _alDebug(ALD_CONVERT, __FILE__, __LINE__, "alutLoadWAV %s with [alformat/size/bits/freq] = [0x%x/%d/%d]", fname, *format, *size, *freq); return AL_TRUE; }
/* * _al_formatscale( ALenum format, ALuint new_channel_num ) * * Returns the openal format that is identical to format, but with sufficient * channel width to accomedate new_channel_num channels. */ ALenum _al_formatscale(ALenum format, ALuint new_channel_num) { int fmt_bits = _al_formatbits(format); switch(new_channel_num) { case 1: switch(fmt_bits) { case 8: return AL_FORMAT_MONO8; break; case 16: return AL_FORMAT_MONO16; break; default: return -1; } break; case 2: switch(fmt_bits) { case 8: return AL_FORMAT_STEREO8; break; case 16: return AL_FORMAT_STEREO16; break; default: return -1; } break; default: #ifdef DEBUG_CONVERT fprintf(stderr, "No support for %d channel AL format, sorry\n", new_channel_num); #endif /* DEBUG_CONVERT */ break; } return -1; }
void *grab_write_esd(void) { esd_format_t fmt; int socket; const char *esdkey = genesdkey(); const char *espeaker = getenv("ESPEAKER"); int esd; esd = esd_open_sound(espeaker); if(esd < 0) { fprintf(stderr, "esd open sound failed.\n"); return NULL; } fmt = ESD_STREAM | ESD_PLAY; switch(DEF_CHANNELS) { case 1: fmt |= ESD_MONO; break; case 2: fmt |= ESD_STEREO; break; default: break; } switch(_al_formatbits(DEF_FORMAT)) { case 8: fmt |= ESD_BITS8; break; case 16: fmt |= ESD_BITS16; break; default: break; } socket = esd_play_stream(fmt, DEF_SPEED, espeaker, esdkey); if(socket < 0) { fprintf(stderr, "esd play stream failed.\n"); return NULL; } _alBlitBuffer = esd_blitbuffer; fprintf(stderr, "esd grab audio ok\n"); _alDebug(ALD_CONTEXT, __FILE__, __LINE__, "esd grab audio ok"); esd_info.speed = DEF_SPEED; esd_info.fmt = fmt; esd_info.socket = socket; esd_info.espeaker = espeaker; esd_info.paused = AL_FALSE; esd_info.esdhandle = esd; strncpy(esd_info.name, esdkey, ESD_NAMELEN); return &esd_info; }
ALboolean set_write_esd(UNUSED(void *handle), UNUSED(ALuint *bufsiz), ALenum *fmt, ALuint *speed) { esd_openal_info_t *eh; int socket; ALuint chans = _al_ALCHANNELS(*fmt); if(handle == NULL) { return AL_FALSE; } eh = (esd_openal_info_t *) handle; close(eh->socket); eh->fmt = ESD_STREAM | ESD_PLAY; switch(chans) { case 1: eh->fmt |= ESD_MONO; break; case 2: eh->fmt |= ESD_STEREO; break; default: break; } switch(_al_formatbits(*fmt)) { case 8: eh->fmt |= ESD_BITS8; break; case 16: eh->fmt |= ESD_BITS16; break; default: break; } eh->speed = *speed; socket = esd_play_stream(eh->fmt, eh->speed, eh->espeaker, eh->name); if(socket < 0) { return AL_FALSE; } eh->socket = socket; eh->paused = AL_FALSE; return AL_TRUE; }
ALboolean alutLoadRAW_ADPCMData_LOKI(ALuint bid, ALvoid *data, ALuint size, ALuint freq, ALenum format) { alWaveFMT_LOKI wfx; int i; ALvoid *persistent_data; if(RAW_first_time == AL_TRUE) { /* so kludgey */ for(i = 0; i < MAX_ADPCM; i++) { bidmap[i].bid = -1; bidmap[i].data = NULL; admap[i].sid = -1; } RAW_first_time = AL_FALSE; } persistent_data = malloc(size); if(persistent_data == NULL) { return AL_FALSE; } memcpy(persistent_data, data, size); wfx.frequency = freq; wfx.channels = _al_ALCHANNELS(format); wfx.bitspersample = _al_formatbits(format); /* insert new bid, ignore blockalign */ bidmap_insert(bid, persistent_data, size, &wfx); _alBufferDataWithCallback_LOKI(bid, RAW_ADPCM_Callback, RAW_ADPCM_DestroyCallback_Sid, RAW_ADPCM_DestroyCallback_Bid); return AL_TRUE; }
ALsizei alCaptureGetData_EXT( UNUSED(ALvoid* data), UNUSED(ALsizei n), UNUSED(ALenum format), UNUSED(ALuint rate) ) { AL_device *dev; ALuint size; ALuint cid; AL_context *cc; /* get the read device */ cid = _alcCCId; cc = _alcGetContext(cid); if ( cc == NULL ) { return 0; } dev = cc->read_device; if ( (dev->format == format) && (dev->speed == rate) ) { size = _alcDeviceRead(cid, data, n); } else { ALuint samples; void *temp; samples = n / (_al_formatbits(format) / 8); /* Set size to the bytes of raw audio data we need */ size = _al_PCMRatioify(rate, dev->speed, format, dev->format, samples); size *= (_al_formatbits(dev->format) / 8); if ( n > (ALsizei)size ) temp = malloc( n ); else temp = malloc( size ); if ( size > 0 ) { size = _alcDeviceRead(cid, temp, size); temp = _alBufferCanonizeData(dev->format, temp, size, dev->speed, format, rate, &size, AL_TRUE); } else { /* Hmm, zero size in record.. */ memset(temp, 0, n); size = n; } if(temp == NULL) { fprintf(stderr, "could not canonize data\n"); return 0; } memcpy(data, temp, size); free( temp ); } return size; }