Microchip® Advanced Software Framework

usb_stream_player.h File Reference
#include "conf_audio_mixer.h"
#include "conf_audio_player.h"
#include "conf_usb.h"
#include "audio_mixer.h"
#include "dsp.h"

Data Structures

struct  buffer_t
 
struct  resampling_config_t
 
struct  resampling_context_t
 
struct  usb_stream_context_t
 

Macros

#define AUDIO_STREAM_IN_OUT_FS_DEFAULT_VALUE
 
#define AUDIO_STREAM_IN_OUT_FS_TRANSLATION_TAB
 
#define AUDIO_STREAM_MAX_NB_CHANNELS   2
 
#define AUDIO_STREAM_MAX_USB_PACKET_SIZE   (USB_STREAM_REAL_BUFFER_SIZE)
 
#define AUDIO_STREAM_NB_INPUT_BUFFERS   8
 
#define AUDIO_STREAM_NB_OUTPUT_BUFFERS   2
 
#define AUDIO_STREAM_RESAMPLING_FILTER_ORDER   6
 
#define PPM(value, ppm)   ( ((int)( (long long)value*(ppm)/1000000) ) )
 Compute the PPM of a number. More...
 
#define PPM_ADD(value, ppm)   ( ((int)(value + (long long)value*(ppm)/1000000)) )
 Add x PPM to a value. More...
 
#define PPM_SUB(value, ppm)   ( ((int)(value - (long long)value*(ppm)/1000000)) )
 Remove x PPM to a value. More...
 
#define USB_STREAM_REAL_BUFFER_SIZE   (USB_STREAM_BUFFER_SIZE)
 Real size of the buffer, i.e. taking into account the possible data 'expansion'. More...
 
#define USB_STREAM_STATUS_DATA_ACQUIRING   4
 
#define USB_STREAM_STATUS_DATA_PROCESSING   2
 
#define USB_STREAM_STATUS_IDLE   1
 
#define USB_STREAM_STATUS_NOT_INITIALIZED   0
 

Enumerations

enum  buffer_state_t {
  BUFFER_STATE_EMPTY,
  BUFFER_STATE_FULL,
  BUFFER_STATE_FILLING,
  BUFFER_STATE_RESAMPLING
}
 
enum  usb_stream_error_defs_t {
  USB_STREAM_ERROR_NONE = 0,
  USB_STREAM_ERROR_OVERFLOW,
  USB_STREAM_ERROR_UNDERFLOW,
  USB_STREAM_ERROR_DEL_SAMPLES,
  USB_STREAM_ERROR_ADD_SAMPLES,
  USB_STREAM_ERROR_NOT_SYNCHRONIZED,
  USB_STREAM_ERROR_RECOVERING_FROM_UNDERFLOW,
  USB_STREAM_ERROR_UNSUPPORTED,
  USB_STREAM_ERROR_BROKEN_STREAM,
  USB_STREAM_ERROR_NO_DATA
}
 
enum  usb_stream_side_t {
  USB_STREAM_DEVICE =0,
  USB_STREAM_HOST
}
 

Functions

void adc_sample_sent_cb (void)
 
void adc_underflow_cb (void)
 
void dac_sample_sent_cb (void)
 This callback function is called when the DAC interrupt has sent the buffer 'n-1' and switches to buffer 'n'. More...
 
void dac_underflow_cb (void)
 This callback function is called when the PWM DAC interrupt does not have any more audio samples (i.e. More...
 
void player_init (void)
 
void player_shutdown (void)
 
void usb_stream_close (void)
 
uint32_t usb_stream_get_buffer_level (void)
 This function returns the number of full buffers. More...
 
uint16_t usb_stream_get_counter (void)
 This function returns the internal audio counter. More...
 
uint8_t usb_stream_get_status (void)
 
uint16_t usb_stream_get_stream_status (void)
 This function returns the audio status. More...
 
void usb_stream_init (uint32_t sample_rate_hz, uint8_t num_channels, uint8_t bits_per_sample, bool swap_channels)
 This function initializes the USB Stream driver. More...
 
int usb_stream_input (usb_stream_side_t side, uint8_t pipe_in, uint32_t *pFifoCount)
 This function takes the stream coming from the selected USB pipe and sends it to the DAC driver. More...
 
bool usb_stream_new_sample_rate (uint32_t sample_rate)
 Sync the sample rate of the audio output stream with the USB stream. More...
 
void usb_stream_out_of_samples (void)
 
int32_t usb_stream_ppm_get (void)
 This function returns the PPM differences between the input audio stream frequency and the DAC output frequency. More...
 
void usb_stream_resync (void)
 This function ensures that no underflow/underflow will never occur by adjusting the SSC/ABDAC frequencies. More...
 
bool usb_stream_task (void)
 

Variables

usb_stream_context_tusb_stream_context
 

#define AUDIO_STREAM_IN_OUT_FS_DEFAULT_VALUE
Value:
{ \
.output_fs_hz = 44100, \
.output_underrun_fs_hz = 44000, \
.output_overrun_fs_hz = 44200, \
.order = 6, \
.gain = 0, \
.gain_overrun = DSP16_Q(0.95), \
.gain_underrun = DSP16_Q(0.95) \
}
#define DSP16_Q(fnum)
Convert a float into a 16-bit signed fixed-point type.
Definition: dsp.h:388

Referenced by usb_stream_init().

#define AUDIO_STREAM_IN_OUT_FS_TRANSLATION_TAB
Value:
{ \
{ \
.input_fs_hz = 48000, \
.output_fs_hz = 44100, \
.output_underrun_fs_hz = 44000, \
.output_overrun_fs_hz = 44200, \
.order = 6, \
.gain = DSP16_Q(1.0), \
.gain_overrun = DSP16_Q(1.0), \
.gain_underrun = DSP16_Q(1.0) \
}, \
{ \
.input_fs_hz = 32000, \
.output_fs_hz = 44100, \
.output_underrun_fs_hz = 44000, \
.output_overrun_fs_hz = 44200, \
.order = 6, \
.gain = DSP16_Q(1.0), \
.gain_overrun = DSP16_Q(1.0), \
.gain_underrun = DSP16_Q(1.0) \
} \
}
#define DSP16_Q(fnum)
Convert a float into a 16-bit signed fixed-point type.
Definition: dsp.h:388

Referenced by usb_stream_init().

#define AUDIO_STREAM_MAX_NB_CHANNELS   2
#define AUDIO_STREAM_MAX_USB_PACKET_SIZE   (USB_STREAM_REAL_BUFFER_SIZE)

Referenced by player_init().

#define AUDIO_STREAM_NB_OUTPUT_BUFFERS   2
#define AUDIO_STREAM_RESAMPLING_FILTER_ORDER   6
#define PPM (   value,
  ppm 
)    ( ((int)( (long long)value*(ppm)/1000000) ) )

Compute the PPM of a number.

#define PPM_ADD (   value,
  ppm 
)    ( ((int)(value + (long long)value*(ppm)/1000000)) )

Add x PPM to a value.

#define PPM_SUB (   value,
  ppm 
)    ( ((int)(value - (long long)value*(ppm)/1000000)) )

Remove x PPM to a value.

#define USB_STREAM_REAL_BUFFER_SIZE   (USB_STREAM_BUFFER_SIZE)

Real size of the buffer, i.e. taking into account the possible data 'expansion'.

#define USB_STREAM_STATUS_DATA_ACQUIRING   4

Referenced by usb_stream_input().

#define USB_STREAM_STATUS_DATA_PROCESSING   2

Referenced by usb_stream_task().

#define USB_STREAM_STATUS_IDLE   1

Referenced by usb_stream_init().

#define USB_STREAM_STATUS_NOT_INITIALIZED   0

Enumerator
BUFFER_STATE_EMPTY 
BUFFER_STATE_FULL 
BUFFER_STATE_FILLING 
BUFFER_STATE_RESAMPLING 
Enumerator
USB_STREAM_DEVICE 
USB_STREAM_HOST 

void adc_sample_sent_cb ( void  )
void adc_underflow_cb ( void  )
void dac_sample_sent_cb ( void  )

This callback function is called when the DAC interrupt has sent the buffer 'n-1' and switches to buffer 'n'.

The aim of this function is thus to prepare the buffer 'n+1'; so that there is always a pending buffer for the interrupt.

References usb_stream_context_t::current_full_buffer, usb_stream_context_t::error, and USB_STREAM_ERROR_UNDERFLOW.

void dac_underflow_cb ( void  )

This callback function is called when the PWM DAC interrupt does not have any more audio samples (i.e.

"famine").

References usb_stream_context_t::error, usb_stream_context_t::synchronized, and USB_STREAM_ERROR_UNDERFLOW.

uint32_t usb_stream_get_buffer_level ( void  )

This function returns the number of full buffers.

References BUFFER_STATE_FULL, and usb_stream_count_nb_buffer().

uint16_t usb_stream_get_counter ( void  )

This function returns the internal audio counter.

References usb_stream_cnt.

uint8_t usb_stream_get_status ( void  )
uint16_t usb_stream_get_stream_status ( void  )

This function returns the audio status.

References usb_stream_context_t::error.

void usb_stream_init ( uint32_t  sample_rate_hz,
uint8_t  num_channels,
uint8_t  bits_per_sample,
bool  swap_channels 
)

This function initializes the USB Stream driver.

References audio_mixer_dacs_setup_direct(), AUDIO_STREAM_IN_OUT_FS_DEFAULT_VALUE, AUDIO_STREAM_IN_OUT_FS_TRANSLATION_TAB, AUDIO_STREAM_MAX_NB_CHANNELS, AUDIO_STREAM_NB_INPUT_BUFFERS, AUDIO_STREAM_NB_OUTPUT_BUFFERS, bits_per_sample, usb_stream_context_t::bits_per_sample, BROKEN_STREAM_TIMER, buffer_t::buffer_state, BUFFER_STATE_EMPTY, dsp16_resampling_options_t::coefficients_generation, cpu_ms_2_cy(), cpu_set_timeout(), usb_stream_context_t::ctx, usb_stream_context_t::ctx_original, usb_stream_context_t::ctx_overrun, usb_stream_context_t::ctx_underrun, usb_stream_context_t::current_buffer, usb_stream_context_t::current_full_buffer, dsp16_resampling_get_output_max_buffer_size(), dsp16_resampling_setup(), usb_stream_context_t::duplicate_channels, dsp16_resampling_options_t::dynamic, usb_stream_context_t::error, FCPU_HZ, usb_stream_context_t::fs_output, resampling_config_t::gain, resampling_context_t::gain, resampling_config_t::gain_overrun, resampling_config_t::gain_underrun, usb_stream_context_t::input_buffer_size, usb_stream_context_t::input_buffers, max, usb_stream_context_t::nb_channels, num_channels, resampling_config_t::order, usb_stream_context_t::output_buffers, resampling_config_t::output_fs_hz, resampling_config_t::output_overrun_fs_hz, usb_stream_context_t::output_temp_buffer, resampling_config_t::output_underrun_fs_hz, resampling_context_t::resampling, usb_stream_context_t::status, swap_channels, usb_stream_context_t::swap_channels, usb_stream_context_t::synchronized, usb_stream_close(), USB_STREAM_ERROR_NONE, USB_STREAM_ERROR_UNSUPPORTED, and USB_STREAM_STATUS_IDLE.

Referenced by host_audio_task(), and usb_stream_new_sample_rate().

int usb_stream_input ( usb_stream_side_t  side,
uint8_t  pipe_in,
uint32_t *  pFifoCount 
)

This function takes the stream coming from the selected USB pipe and sends it to the DAC driver.

Moreover, it ensures that both input and output stream keep synchronized by adding or deleting samples.

Parameters
sideUSB_STREAM_HOST for USB host, USB_STREAM_DEVICE for device.
pipe_inNumber of the addressed pipe
pFifoCount(return parameter) NULL or pointer to the number of used buffers at this time
Returns
status: (USB_STREAM_STATUS_OK, USB_STREAM_STATUS_NOT_SYNCHRONIZED, USB_STREAM_STATUS_SPEED_UP, USB_STREAM_STATUS_SLOW_DOWN, USB_STREAM_STATUS_BUFFER_OVERFLOW)

References usb_stream_context_t::bits_per_sample, BROKEN_STREAM_TIMER, BUFFER_STATE_FULL, cpu_is_timeout(), cpu_ms_2_cy(), cpu_set_timeout(), usb_stream_context_t::duplicate_channels, usb_stream_context_t::error, FCPU_HZ, Host_byte_count, host_read_p_rxpacket(), Host_reset_pipe_fifo_access, usb_stream_context_t::input_temp_buffer, Min, usb_stream_context_t::nb_channels, usb_stream_context_t::status, Usb_byte_count, Usb_reset_endpoint_fifo_access, usb_stream_cnt, usb_stream_count_nb_buffer(), USB_STREAM_DEVICE, USB_STREAM_ERROR_BROKEN_STREAM, USB_STREAM_ERROR_NO_DATA, USB_STREAM_ERROR_NONE, USB_STREAM_ERROR_NOT_SYNCHRONIZED, usb_stream_fill_input_buffers(), USB_STREAM_HOST, USB_STREAM_STATUS_DATA_ACQUIRING, and USB_STREAM_STATUS_NOT_INITIALIZED.

Referenced by host_audio_task().

bool usb_stream_new_sample_rate ( uint32_t  sample_rate)

Sync the sample rate of the audio output stream with the USB stream.

Return values
falseThe new sample rate already matches the current settings.
trueThe output sample rate was changed.

References audio_mixer_dacs_flush_direct(), and usb_stream_init().

void usb_stream_out_of_samples ( void  )
int32_t usb_stream_ppm_get ( void  )

This function returns the PPM differences between the input audio stream frequency and the DAC output frequency.

Note that this information should only be considered as an indication, for debug or visual effect for example.

Referenced by mmi_activity_display().

void usb_stream_resync ( void  )

This function ensures that no underflow/underflow will never occur by adjusting the SSC/ABDAC frequencies.

References usb_stream_task().

Referenced by host_audio_task().

usb_stream_context_t* usb_stream_context