int main(void) { float buffer[NUM_FRAMES]; PABLIO_Stream *inStream, *outStream; int x; double time = 0.0; double lastTime = 0.0; double delta = 1.0/SAMPLE_RATE; OpenAudioStream(&inStream, SAMPLE_RATE, paFloat32, PABLIO_READ|PABLIO_MONO); while (1) { ReadAudioStream(inStream, buffer, NUM_FRAMES); for (x = 0; x < NUM_FRAMES; x++) { if ((buffer[x] > 0.20) && (buffer[x] > buffer[x-1]) && (time - lastTime > DEBOUNCE_THRESHOLD)) { printf("%f (%f)\n", time, time-lastTime); lastTime = time; } time += delta; } } }
int main(void) { int i; PaError err; PABLIO_Stream *aInStream; PABLIO_Stream *aOutStream; int index; printf("Full duplex sound test using PABLIO\n"); fflush(stdout); /* Open simplified blocking I/O layer on top of PortAudio. */ /* Open input first so it can start to fill buffers. */ err = OpenAudioStream( &aInStream, SAMPLE_RATE, SAMPLE_TYPE, (PABLIO_READ | PABLIO_STEREO) ); if( err != paNoError ) goto error; /* printf("opened input\n"); fflush(stdout); /**/ err = OpenAudioStream( &aOutStream, SAMPLE_RATE, SAMPLE_TYPE, (PABLIO_WRITE | PABLIO_STEREO) ); if( err != paNoError ) goto error; /* printf("opened output\n"); fflush(stdout); /**/ /* Process samples in the foreground. */ index = 0; for( i=0; i<(NUM_SECONDS * SAMPLE_RATE); i++ ) { /* Write old frame of data to output. */ /* samples[index][1] = (i&256) * (1.0f/256.0f); /* sawtooth */ WriteAudioStream( aOutStream, &samples[index][0], 1 ); /* Read one frame of data into sample array for later output. */ ReadAudioStream( aInStream, &samples[index][0], 1 ); index += 1; if( index >= NUM_ECHO_FRAMES ) index = 0; if( (i & 0xFFFF) == 0 ) printf("i = %d\n", i ); fflush(stdout); /**/ } CloseAudioStream( aOutStream ); CloseAudioStream( aInStream ); printf("R/W echo sound test complete.\n" ); fflush(stdout); return 0; error: fprintf( stderr, "An error occured while using PortAudio\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return -1; }
int main(void) { PABLIO_Stream *inStream, *outStream; OpenAudioStream(&inStream, 44100, paInt16, PABLIO_MONO|PABLIO_READ); OpenAudioStream(&outStream, 44100, paInt16, PABLIO_MONO|PABLIO_WRITE); while(1) { // pass audio in to audio out one buffer at a time forever // using blocking read and write calls which provide flow control ReadAudioStream( inStream, buffer, NUM_FRAMES); // process samples in buffer here as desired WriteAudioStream(outStream, buffer, NUM_FRAMES); } }
int SoundDevice::read(void *buf, int len) { // len is in samples: 4*2 32bit stereo int res = -1; if(jack) { res = jack_in_pipe->read(len<<1,buf); } else if(aInStream) { // pablio // takes number of left and right frames (stereo / 2) res = ReadAudioStream( aInStream, buf, len); } return res; }
void filterAudio( double sampleRate, double duration, Filter& filter, Sequencer& sequencer ){ static const int length = 64; float input[length], output[length]; double secondsPerVector = (double)length / sampleRate; sequencer.beginPlayback(); PABLIO_Stream *stream; OpenAudioStream( &stream, sampleRate, paFloat32, PABLIO_READ_WRITE | PABLIO_MONO ); unsigned long count = (sampleRate / length) * duration; for( unsigned long i=0; i<count; ++i ){ ReadAudioStream( stream, input, length ); std::fill_n( output, length, 0.f ); // zero output filter.synthesize( output, input, length ); sequencer.update( secondsPerVector ); WriteAudioStream( stream, output, length ); } CloseAudioStream( stream ); }
int32 ad_read (ad_rec_t *handle, int16 *buf, int32 max) { int32 length; /* 2004/08/29 L Galescu <*****@*****.**> * ReadAudioStream() returns number of samples (frames in pa parlance) read! */ length = ReadAudioStream(handle->stream, buf, max); #ifdef AD_PA_DEBUG fprintf(stderr, "recording: %d; read: %d (requested: %d)\n", handle->recording, length, max); fprintf(stderr, ">>>ad_read>>>ring buffer: %d[r:%d;w:%d]\n", handle->stream->inFIFO.bufferSize/handle->bps, handle->stream->inFIFO.readIndex/handle->bps, handle->stream->inFIFO.writeIndex/handle->bps); #endif if ((length == 0) && (! handle->recording)) return AD_EOF; #ifdef UNDEF_LG { int32 offset; /* hack! get rid of those pesky nulls at the beginning! */ for (offset = 0; (offset < length) && (buf[offset] == 0); offset++); if (offset > 0) { fprintf(stderr, "ad_read(): %d nulls deleted (out of %d samples)\n", offset, length); bcopy(buf+offset, buf, length-offset); length -= offset; } } fprintf(stderr, "A/D Read: %d\n", length); #endif return length; }
int main(void) { int i; SAMPLE samples[SAMPLES_PER_FRAME * FRAMES_PER_BLOCK]; PaError err; PABLIO_Stream *aStream; printf("Full duplex sound test using PortAudio and RingBuffers\n"); fflush(stdout); /* Open simplified blocking I/O layer on top of PortAudio. */ err = OpenAudioStream( &aStream, SAMPLE_RATE, SAMPLE_TYPE, (PABLIO_READ_WRITE | PABLIO_STEREO) ); if( err != paNoError ) goto error; /* Process samples in the foreground. */ for( i=0; i<(NUM_SECONDS * SAMPLE_RATE); i += FRAMES_PER_BLOCK ) { /* Read one block of data into sample array from audio input. */ ReadAudioStream( aStream, samples, FRAMES_PER_BLOCK ); /* Write that same block of data to output. */ WriteAudioStream( aStream, samples, FRAMES_PER_BLOCK ); } CloseAudioStream( aStream ); printf("Full duplex sound test complete.\n" ); fflush(stdout); return 0; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return -1; }
static void *SWITCH_THREAD_FUNC read_stream_thread(switch_thread_t *thread, void *obj) { portaudio_stream_source_t *source = obj; portaudio_stream_context_t *cp; int samples = 0; int bused, bytesToWrite; switch_mutex_lock(globals.mutex); globals.threads++; switch_mutex_unlock(globals.mutex); if (!source->prebuf) { source->prebuf = DEFAULT_PREBUFFER_SIZE; } switch_mutex_lock(globals.mutex); switch_core_hash_insert(globals.source_hash, source->sourcename, source); switch_mutex_unlock(globals.mutex); switch_thread_rwlock_create(&source->rwlock, source->pool); if (engage_device(source, 0) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Dev %d cant be engaged !\n", (int) source->sourcedev); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " Dev %d engaged at %d rate!\n", (int) source->sourcedev, (int) source->rate); if (globals.running && !source->stopped) { source->ready = 1; if (!source->audio_stream) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Audio Stream wops!\n"); source->stopped = 0; source->ready = 0; } else { while (globals.running && !source->stopped) { samples = 0; switch_mutex_lock(source->device_lock); samples = ReadAudioStream(source->audio_stream, source->databuf, source->read_codec.implementation->samples_per_packet, 0, &source->timer); switch_mutex_unlock(source->device_lock); if (samples) { bytesToWrite = source->samples; if (samples < bytesToWrite) { bytesToWrite = samples; } bytesToWrite *= source->audio_stream->bytesPerFrame; if (source->total) { switch_mutex_lock(source->mutex); for (cp = source->context_list; cp; cp = cp->next) { switch_mutex_lock(cp->audio_mutex); bused = switch_buffer_inuse(cp->audio_buffer); if (bused > source->samples * 768) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Leaking stream handle! [%s() %s:%d] %d used %d max\n", cp->func, cp->file, cp->line, (int) bused, (int) (source->samples * 768)); switch_buffer_zero(cp->audio_buffer); } else { switch_buffer_write(cp->audio_buffer, source->databuf, bytesToWrite); } switch_mutex_unlock(cp->audio_mutex); } switch_mutex_unlock(source->mutex); } } } } } } source->ready = 0; switch_mutex_lock(globals.mutex); switch_core_hash_delete(globals.source_hash, source->sourcename); switch_mutex_unlock(globals.mutex); switch_thread_rwlock_wrlock(source->rwlock); switch_thread_rwlock_unlock(source->rwlock); switch_mutex_lock(source->device_lock); CloseAudioStream(source->audio_stream); if (switch_core_codec_ready(&source->read_codec)) { switch_core_codec_destroy(&source->read_codec); switch_core_codec_destroy(&source->write_codec); } if (switch_core_codec_ready(&source->write_codec)) { switch_core_codec_destroy(&source->write_codec); } switch_mutex_unlock(source->device_lock); switch_core_destroy_memory_pool(&source->pool); switch_mutex_lock(globals.mutex); globals.threads--; switch_mutex_unlock(globals.mutex); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " thread ending succesfully !\n"); switch_thread_exit(thread, SWITCH_STATUS_SUCCESS); return NULL; }