//! //! @brief This function initializes the USB Stream driver. //! void usb_stream_init( uint32_t sample_rate_hz , uint8_t num_channels , uint8_t bits_per_sample , bool swap_channels) { uint32_t i; usb_stream_context->sample_rate = sample_rate_hz; usb_stream_context->rd_id= usb_stream_context->wr_id= 0; usb_stream_context->synchronized= false; usb_stream_context->bits_per_sample=bits_per_sample; usb_stream_context->channel_count =num_channels; usb_stream_resync_frequency=0; for( i=0 ; i<USB_STREAM_BUFFER_NUMBER ; i++ ) usb_stream_context->audio_buffer_size[i] = 0; audio_mixer_dacs_setup_direct(sample_rate_hz, num_channels, bits_per_sample, swap_channels); // Start the broken stream timer cpu_set_timeout(cpu_ms_2_cy(BROKEN_STREAM_TIMER, FCPU_HZ), &broken_stream_timer); }
void buff_player_dacs_setup(uint32_t sample_rate_hz, uint8_t num_channels, uint8_t bits_per_sample, bool swap_channels) { audio_mixer_dacs_setup_direct(sample_rate_hz, num_channels, bits_per_sample, swap_channels); }
//! //! @brief This function initializes the USB Stream driver. //! void usb_stream_init(uint32_t sample_rate_hz, uint8_t num_channels, uint8_t bits_per_sample, bool swap_channels) { const resampling_config_t default_param = AUDIO_STREAM_IN_OUT_FS_DEFAULT_VALUE; const resampling_config_t translation_tab[] = AUDIO_STREAM_IN_OUT_FS_TRANSLATION_TAB; int i; int max_buffer_size; int underrun_fs, overrun_fs, order; dsp16_resampling_options_t options; // Reset the structure memset(&options, 0, sizeof(dsp16_resampling_options_t)); // Add default values options.coefficients_generation = DSP16_RESAMPLING_OPTIONS_USE_DYNAMIC; options.dynamic.coefficients_normalization = true; if (num_channels == 1) { num_channels = 2; usb_stream_context->duplicate_channels = true; } else usb_stream_context->duplicate_channels = false; // Free the existing context if any usb_stream_close(); // Clear the error flag usb_stream_context->error = USB_STREAM_ERROR_NONE; // Choose which output sampling rate to choose usb_stream_context->fs_output = default_param.output_fs_hz; underrun_fs = default_param.output_underrun_fs_hz; overrun_fs = default_param.output_overrun_fs_hz; order = default_param.order; usb_stream_context->ctx_original.gain = default_param.gain; usb_stream_context->ctx_underrun.gain = default_param.gain_underrun; usb_stream_context->ctx_overrun.gain = default_param.gain_overrun; for (i=0; i<sizeof(translation_tab) / sizeof(translation_tab[0]); i++) { if (translation_tab[i].input_fs_hz == sample_rate_hz) { usb_stream_context->fs_output = translation_tab[i].output_fs_hz; underrun_fs = translation_tab[i].output_underrun_fs_hz; overrun_fs = translation_tab[i].output_overrun_fs_hz; order = translation_tab[i].order; // Set gain stage for each context usb_stream_context->ctx_original.gain = translation_tab[i].gain; usb_stream_context->ctx_underrun.gain = translation_tab[i].gain_underrun; usb_stream_context->ctx_overrun.gain = translation_tab[i].gain_overrun; break; } } if (num_channels > AUDIO_STREAM_MAX_NB_CHANNELS) { usb_stream_context->error = USB_STREAM_ERROR_UNSUPPORTED; return; } // Create a new resampling context usb_stream_context->ctx_original.resampling = dsp16_resampling_setup(sample_rate_hz, usb_stream_context->fs_output, usb_stream_context->input_buffer_size, order, num_channels, (malloc_fct_t) malloc, &options); usb_stream_context->ctx_overrun.resampling = dsp16_resampling_setup(sample_rate_hz, overrun_fs, usb_stream_context->input_buffer_size, order, num_channels, (malloc_fct_t) malloc, &options); usb_stream_context->ctx_underrun.resampling = dsp16_resampling_setup(sample_rate_hz, underrun_fs, usb_stream_context->input_buffer_size, order, num_channels, (malloc_fct_t) malloc, &options); if (!usb_stream_context->ctx_original.resampling || !usb_stream_context->ctx_overrun.resampling || !usb_stream_context->ctx_underrun.resampling) { usb_stream_context->error = USB_STREAM_ERROR_UNSUPPORTED; usb_stream_close(); return; } // Get the maximum size of the output buffer (the same will be used with the different frequencies) max_buffer_size = dsp16_resampling_get_output_max_buffer_size(usb_stream_context->ctx_original.resampling); max_buffer_size = max(max_buffer_size, dsp16_resampling_get_output_max_buffer_size(usb_stream_context->ctx_overrun.resampling)); max_buffer_size = max(max_buffer_size, dsp16_resampling_get_output_max_buffer_size(usb_stream_context->ctx_underrun.resampling)); // Allocate memory for the output buffers for(i=0; i<AUDIO_STREAM_NB_OUTPUT_BUFFERS; i++) { if (!(usb_stream_context->output_buffers[i] = malloc(sizeof(dsp16_t)*(max_buffer_size + 1)*num_channels))) { usb_stream_context->error = USB_STREAM_ERROR_UNSUPPORTED; usb_stream_close(); return; } } // Allocate memory for the temporary output buffer if (!(usb_stream_context->output_temp_buffer = malloc(sizeof(dsp16_t)*max_buffer_size))) { usb_stream_context->error = USB_STREAM_ERROR_UNSUPPORTED; usb_stream_close(); return; } // Fills the context structure usb_stream_context->error = USB_STREAM_ERROR_NONE; usb_stream_context->nb_channels = num_channels; if (bits_per_sample == 32) bits_per_sample = 16; usb_stream_context->bits_per_sample = bits_per_sample; usb_stream_context->swap_channels = swap_channels; // No full buffer available yet usb_stream_context->current_full_buffer = -1; // Set buffers to "initialization" state usb_stream_context->current_buffer = 0; for(i=0; i<AUDIO_STREAM_NB_INPUT_BUFFERS; i++) usb_stream_context->input_buffers[i]->buffer_state = BUFFER_STATE_EMPTY; usb_stream_context->synchronized = false; // Configure the DAC audio_mixer_dacs_setup_direct(usb_stream_context->fs_output, num_channels, bits_per_sample, swap_channels); // Use by default the original re-sampling setup usb_stream_context->ctx = &usb_stream_context->ctx_original; // Set status to initialized usb_stream_context->status = USB_STREAM_STATUS_IDLE; // Start the broken stream timer cpu_set_timeout(cpu_ms_2_cy(BROKEN_STREAM_TIMER, FCPU_HZ), &broken_stream_timer); }