//! The main function int main(int argc, char *argv[]) { A_ALIGNED static dsp16_t s_input[N/NB_CUTS]; int f; dsp_resampling_t *resampling; dsp16_t *output; U32 temp; dsp16_resampling_options_t options; // Initialize options memset(&options, 0, sizeof(options)); options.coefficients_generation = DSP16_RESAMPLING_OPTIONS_USE_DYNAMIC; options.dynamic.coefficients_normalization = true; // Switch to external Oscillator 0. pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize the DSP debug module dsp_debug_initialization(FOSC0); dsp16_debug_printf("16-bit fixed point signal resampling\n"); dsp16_debug_printf("Input Fs: %iHz\n", F_INPUT); dsp16_debug_printf("Output Fs: %iHz\n", F_OUTPUT); dsp16_debug_printf("%i samples to process cut into %i buffers\n", N, NB_CUTS); dsp16_debug_printf("Filter order used: %i\n", FILTER_ORDER); if (!(resampling = dsp16_resampling_setup(F_INPUT, F_OUTPUT, N/NB_CUTS, FILTER_ORDER, 1, (malloc_fct_t) malloc, &options))) { dsp16_debug_printf("Unable to allocate enough memory\n"); return -1; } if (!(output = (dsp16_t *) malloc(dsp16_resampling_get_output_max_buffer_size(resampling)*sizeof(dsp16_t) + 4))) { dsp16_debug_printf("Unable to allocate enough memory for the output buffer\n"); return -1; } for(f=0; f<NB_CUTS; f++) { memcpy(s_input, &s[N/NB_CUTS*f], (N/NB_CUTS)*sizeof(dsp16_t)); temp = Get_sys_count(); dsp16_resampling_compute(resampling, output, s_input, 0); temp = Get_sys_count() - temp; dsp16_debug_print_vect(output, dsp16_resampling_get_output_current_buffer_size(resampling)); } dsp16_debug_printf( "Cycle count per iteration: %i\n in total (to compute %i samples): %i\n", temp, dsp16_resampling_get_output_current_buffer_size(resampling)*NB_CUTS, temp*NB_CUTS); dsp16_resampling_free(resampling, (free_fct_t) free); while(1); }
//! //! @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); }