Example #1
0
static int init_media_processing(avdtp_media_codec_configuration_sbc_t configuration){
    int num_channels = configuration.num_channels;
    int sample_rate = configuration.sampling_frequency;
    
#ifdef DECODE_SBC
    btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL);
#endif

#ifdef STORE_SBC_TO_WAV_FILE
    wav_writer_open(wav_filename, num_channels, sample_rate);  
#endif

#ifdef STORE_SBC_TO_SBC_FILE    
    sbc_file = fopen(sbc_filename, "wb"); 
#endif

#ifdef HAVE_PORTAUDIO
    // int frames_per_buffer = configuration.frames_per_buffer;
    PaError err;
    PaStreamParameters outputParameters;

    /* -- initialize PortAudio -- */
    err = Pa_Initialize();
    if (err != paNoError){
        printf("Error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
        return err;
    } 
    /* -- setup input and output -- */
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    outputParameters.channelCount = num_channels;
    outputParameters.sampleFormat = PA_SAMPLE_TYPE;
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;
    /* -- setup stream -- */
    err = Pa_OpenStream(
           &stream,
           NULL,                /* &inputParameters */
           &outputParameters,
           sample_rate,
           0,
           paClipOff,           /* we won't output out of range samples so don't bother clipping them */
           patestCallback,      /* use callback */
           NULL );   
    
    if (err != paNoError){
        printf("Error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
        return err;
    }
    memset(ring_buffer_storage, 0, sizeof(ring_buffer_storage));
    btstack_ring_buffer_init(&ring_buffer, ring_buffer_storage, sizeof(ring_buffer_storage));
    pa_stream_started = 0;
#endif 
    media_initialized = 1;
    return 0;
}
Example #2
0
static int transport_open(void){
    esp_err_t ret;

    log_info("transport_open");

    btstack_ring_buffer_init(&hci_ringbuffer, hci_ringbuffer_storage, sizeof(hci_ringbuffer_storage));


    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        log_error("transport: esp_bt_controller_init failed");
        return -1;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
    if (ret) {
        log_error("transport: esp_bt_controller_enable failed");
        return -1;
    }

    esp_vhci_host_register_callback(&vhci_host_cb);
    return 0;
}
Example #3
0
static int media_processing_init(avdtp_media_codec_configuration_sbc_t configuration){
    if (is_media_initialized) return 0;
#ifdef DECODE_SBC
    btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL);
#endif

#ifdef STORE_SBC_TO_WAV_FILE
    wav_writer_open(wav_filename, configuration.num_channels, configuration.sampling_frequency);
#endif

#ifdef STORE_SBC_TO_SBC_FILE    
   sbc_file = fopen(sbc_filename, "wb"); 
#endif

#ifdef HAVE_PORTAUDIO
    // int frames_per_buffer = configuration.frames_per_buffer;
    PaError err;
    PaStreamParameters outputParameters;
    const PaDeviceInfo *deviceInfo;

    /* -- initialize PortAudio -- */
    err = Pa_Initialize();
    if (err != paNoError){
        printf("Error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
        return err;
    } 
    /* -- setup input and output -- */
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    outputParameters.channelCount = configuration.num_channels;
    outputParameters.sampleFormat = PA_SAMPLE_TYPE;
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;
    deviceInfo = Pa_GetDeviceInfo( outputParameters.device );
    printf("PortAudio: Output device: %s\n", deviceInfo->name);
    log_info("PortAudio: Output device: %s", deviceInfo->name);
    /* -- setup stream -- */
    err = Pa_OpenStream(
           &stream,
           NULL,                /* &inputParameters */
           &outputParameters,
           configuration.sampling_frequency,
           0,
           paClipOff,           /* we won't output out of range samples so don't bother clipping them */
           portaudio_callback,      /* use callback */
           NULL );   
    
    if (err != paNoError){
        printf("Error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
        return err;
    }
    log_info("PortAudio: stream opened");
    printf("PortAudio: stream opened\n");
#endif
#ifdef HAVE_AUDIO_DMA
    audio_stream_paused  = 1;
    hal_audio_dma_init(configuration.sampling_frequency);
    hal_audio_dma_set_audio_played(&hal_audio_dma_done);
    // start playing silence
    hal_audio_dma_done();
#endif

 #if defined(HAVE_PORTAUDIO) || defined (HAVE_AUDIO_DMA)
    memset(ring_buffer_storage, 0, sizeof(ring_buffer_storage));
    btstack_ring_buffer_init(&ring_buffer, ring_buffer_storage, sizeof(ring_buffer_storage));
    audio_stream_started = 0;
    audio_stream_paused = 0;
#endif 
 is_media_initialized = 1;
    return 0;
}
Example #4
0
int main(int argc, const char * argv[]){
    (void) argc;
    (void) argv;
    
    PaError err;
    static paTestData data;
    static PaStream * stream;

    /* initialise sinusoidal wavetable */
    int i;
    for (i=0; i<TABLE_SIZE; i++){
        data.sine[i] = sin(((double)i/(double)TABLE_SIZE) * M_PI * 2.)*32767;
    }
    data.left_phase = data.right_phase = 0;

    err = Pa_Initialize();
    if (err != paNoError){
        printf("Error initializing portaudio: \"%s\"\n",  Pa_GetErrorText(err));
        return paNoError;
    } 

    PaStreamParameters outputParameters;
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    outputParameters.channelCount = NUM_CHANNELS;
    outputParameters.sampleFormat = PA_SAMPLE_TYPE;
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;
    
    /* -- setup stream -- */
    err = Pa_OpenStream(
           &stream,
           NULL,                /* &inputParameters */
           &outputParameters,
           SAMPLE_RATE,
           FRAMES_PER_BUFFER,
           paClipOff,           /* we won't output out of range samples so don't bother clipping them */
           patestCallback,      /* use callback */
           &data );             /* callback userData */

    memset(ring_buffer_storage, 0, sizeof(ring_buffer_storage));
    btstack_ring_buffer_init(&ring_buffer, ring_buffer_storage, sizeof(ring_buffer_storage));

    wav_writer_open(wav_filename, NUM_CHANNELS, SAMPLE_RATE);

    if (err != paNoError){
        printf("Error opening default stream: \"%s\"\n",  Pa_GetErrorText(err));
        return paNoError;
    } 

    err = Pa_StartStream(stream);
    if (err != paNoError){
        printf("Error starting the stream: \"%s\"\n",  Pa_GetErrorText(err));
        return paNoError;
    } 

    /* Sleep for several seconds. */
    while (1){
        fill_ring_buffer(&data);
        Pa_Sleep(1);
    }
    
    err = Pa_StopStream(stream);
    if (err != paNoError){
        printf("Error stopping the stream: \"%s\"\n",  Pa_GetErrorText(err));
        return paNoError;
    } 

    err = Pa_CloseStream(stream);
    if (err != paNoError){
        printf("Error closing the stream: \"%s\"\n",  Pa_GetErrorText(err));
        return paNoError;
    } 

    err = Pa_Terminate();
    if (err != paNoError){
        printf("Error terminating portaudio: \"%s\"\n",  Pa_GetErrorText(err));
        return paNoError;
    } 
    return 0;
}
Example #5
0
// return 1 if ok
static int portaudio_initialize(int sample_rate){
    PaError err;

    /* -- initialize PortAudio -- */
    printf("PortAudio: Initialize\n");
    err = Pa_Initialize();
    if( err != paNoError ) return 0;

    /* -- setup input and output -- */
    const PaDeviceInfo *deviceInfo;
    PaStreamParameters * inputParameters = NULL;
    PaStreamParameters outputParameters;
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    outputParameters.channelCount = NUM_CHANNELS;
    outputParameters.sampleFormat = paInt16;
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;
    deviceInfo = Pa_GetDeviceInfo( outputParameters.device );
    log_info("PortAudio: Output device: %s", deviceInfo->name);
#ifdef USE_PORTAUDIO_INPUT
    PaStreamParameters theInputParameters;
    theInputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
    theInputParameters.channelCount = NUM_CHANNELS;
    theInputParameters.sampleFormat = paInt16;
    theInputParameters.suggestedLatency = Pa_GetDeviceInfo( theInputParameters.device )->defaultHighOutputLatency;
    theInputParameters.hostApiSpecificStreamInfo = NULL;
    inputParameters = &theInputParameters;
    deviceInfo = Pa_GetDeviceInfo( inputParameters->device );
    log_info("PortAudio: Input device: %s", deviceInfo->name);
#endif

    /* -- setup output stream -- */
    printf("PortAudio: Open stream\n");
    err = Pa_OpenStream(
           &pa_stream,
           inputParameters,
           &outputParameters,
           sample_rate,
           0,
           paClipOff, /* we won't output out of range samples so don't bother clipping them */
           portaudio_callback,  
           NULL );  
    if (err != paNoError){
        printf("Error opening portaudio stream: \"%s\"\n",  Pa_GetErrorText(err));
        return 0;
    }
    memset(pa_output_ring_buffer_storage, 0, sizeof(pa_output_ring_buffer_storage));
    btstack_ring_buffer_init(&pa_output_ring_buffer, pa_output_ring_buffer_storage, sizeof(pa_output_ring_buffer_storage));
#ifdef USE_PORTAUDIO_INPUT
    memset(pa_input_ring_buffer_storage, 0, sizeof(pa_input_ring_buffer_storage));
    btstack_ring_buffer_init(&pa_input_ring_buffer, pa_input_ring_buffer_storage, sizeof(pa_input_ring_buffer_storage));
    printf("PortAudio: Input buffer size %u\n", btstack_ring_buffer_bytes_free(&pa_input_ring_buffer));
#endif

    /* -- start stream -- */
    err = Pa_StartStream(pa_stream);
    if (err != paNoError){
        printf("Error starting the stream: \"%s\"\n",  Pa_GetErrorText(err));
        return 0;
    }
    pa_output_started = 1; 
    pa_output_paused  = 1;
#ifdef USE_PORTAUDIO_INPUT
    pa_input_started = 1; 
    pa_input_paused  = 1;
#endif

    return 1;
}