Atmel 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"

Data Structures

struct  usb_stream_context_t
 

Macros

#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...
 

Enumerations

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)
 
static __always_inline void usb_stream_fifo_get (void **pp_buffer, uint16_t *p_size)
 This function gets from the audio stream FIFO the next buffer to read. More...
 
static __always_inline int8_t * usb_stream_fifo_get_buffer (uint8_t index)
 This function returns the pointer on a buffer according to its index. More...
 
static __always_inline int usb_stream_fifo_get_free_room (void)
 This function returns the number of free buffers in the USB stream FIFO. More...
 
static __always_inline int usb_stream_fifo_get_used_room (void)
 This function returns the number of used buffers in the USB stream FIFO. More...
 
static __always_inline void usb_stream_fifo_pull (void)
 This function increases the read index of the FIFO. More...
 
static __always_inline void usb_stream_fifo_push (uint16_t size)
 This function put into the audio stream FIFO a new buffer and increases the write index of the FIFO. More...
 
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...
 
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...
 
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 monitors the buffering level and set a timer for the resynchronization. More...
 

Variables

usb_stream_context_tusb_stream_context
 

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

Compute the PPM of a number.

Referenced by usb_stream_input().

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

Add x PPM to a value.

Referenced by usb_stream_resync().

#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'.

Referenced by usb_stream_input().

Enumerator
USB_STREAM_DEVICE 
USB_STREAM_HOST 

void adc_underflow_cb ( void  )

References b_microphone_started.

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 audio_mixer_dacs_output_direct(), usb_stream_context_t::bits_per_sample, buffer, usb_stream_context_t::channel_count, usb_stream_context_t::status, usb_stream_context_t::synchronized, USB_STREAM_ERROR_NOT_SYNCHRONIZED, USB_STREAM_ERROR_UNDERFLOW, usb_stream_fifo_get(), usb_stream_fifo_get_used_room(), and usb_stream_fifo_pull().

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::status, usb_stream_context_t::synchronized, USB_STREAM_ERROR_NOT_SYNCHRONIZED, and USB_STREAM_ERROR_UNDERFLOW.

void player_init ( void  )
void player_shutdown ( void  )
static __always_inline void usb_stream_fifo_get ( void **  pp_buffer,
uint16_t *  p_size 
)
static

This function gets from the audio stream FIFO the next buffer to read.

Note that it does not increase the read index of the FIFO.

Parameters
pp_buffer(return parameter) Pointer to the buffer address to update.
p_size(return parameter) Pointer to the size of the buffer.

References usb_stream_context_t::audio_buffer, usb_stream_context_t::audio_buffer_size, usb_stream_context_t::rd_id, and USB_STREAM_BUFFER_NUMBER.

Referenced by dac_sample_sent_cb(), and usb_stream_input().

static __always_inline int8_t* usb_stream_fifo_get_buffer ( uint8_t  index)
static

This function returns the pointer on a buffer according to its index.

Parameters
indexIndex of the buffer
Returns
pointer on buffer

References usb_stream_context_t::audio_buffer, and USB_STREAM_BUFFER_NUMBER.

Referenced by usb_stream_input().

static __always_inline int usb_stream_fifo_get_free_room ( void  )
static

This function returns the number of free buffers in the USB stream FIFO.

Returns
number of free buffers.

References USB_STREAM_BUFFER_NUMBER, and usb_stream_fifo_get_used_room().

static __always_inline int usb_stream_fifo_get_used_room ( void  )
static

This function returns the number of used buffers in the USB stream FIFO.

Returns
number of used buffers.

References audio_mixer_dacs_output_direct(), NULL, usb_stream_context_t::rd_id, USB_STREAM_BUFFER_NUMBER, and usb_stream_context_t::wr_id.

Referenced by dac_sample_sent_cb(), usb_stream_fifo_get_free_room(), usb_stream_get_buffer_level(), usb_stream_input(), and usb_stream_resync().

static __always_inline void usb_stream_fifo_pull ( void  )
static

This function increases the read index of the FIFO.

References usb_stream_context_t::rd_id, and USB_STREAM_BUFFER_NUMBER.

Referenced by dac_sample_sent_cb(), and usb_stream_input().

static __always_inline void usb_stream_fifo_push ( uint16_t  size)
static

This function put into the audio stream FIFO a new buffer and increases the write index of the FIFO.

Parameters
sizeSize of the buffer.

References usb_stream_context_t::audio_buffer_size, USB_STREAM_BUFFER_NUMBER, and usb_stream_context_t::wr_id.

Referenced by usb_stream_input().

uint32_t usb_stream_get_buffer_level ( void  )

This function returns the number of full buffers.

References usb_stream_count_nb_buffer(), and usb_stream_fifo_get_used_room().

uint16_t usb_stream_get_counter ( void  )

This function returns the internal audio counter.

References usb_stream_cnt.

uint16_t usb_stream_get_stream_status ( void  )

This function returns the audio status.

References usb_stream_context_t::status.

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/endpoint
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)

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 _32_BITS_RATIO, abs, audio_mixer_dacs_output_direct(), usb_stream_context_t::bits_per_sample, BROKEN_STREAM_TIMER, buffer, usb_stream_context_t::channel_count, cpu_is_timeout(), cpu_is_timer_stopped(), cpu_ms_2_cy(), cpu_set_timeout(), cpu_stop_timeout(), cpu_us_2_cy(), CS2200_FREF, cs2200_freq_clk_out(), FCPU_HZ, for(), Host_byte_count, host_read_p_rxpacket(), Host_reset_pipe_fifo_access, i, Min, NULL, PPM, UnionPtr::s16ptr, UnionPtr::s32ptr, UnionPtr::s8ptr, usb_stream_context_t::sample_rate, usb_stream_context_t::status, swap16, swap32, usb_stream_context_t::synchronized, TIMER_USB_RESYNC_CORRECTION, TIMER_USB_STREAM_MONITOR, Usb_byte_count, usb_read_ep_rxpacket(), Usb_reset_endpoint_fifo_access, USB_STREAM_BUFFER_NUMBER, 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_ERROR_OVERFLOW, usb_stream_fifo_get(), usb_stream_fifo_get_buffer(), usb_stream_fifo_get_used_room(), usb_stream_fifo_pull(), usb_stream_fifo_push(), usb_stream_fill_input_buffers(), USB_STREAM_HOST, USB_STREAM_REAL_BUFFER_SIZE, usb_stream_real_sample_freq, usb_stream_resync(), usb_stream_resync_b_acting, usb_stream_resync_freq_ofst, usb_stream_resync_frequency, usb_stream_resync_last_room, usb_stream_resync_ppm, usb_stream_resync_ppm_ofst, USB_STREAM_RESYNC_PPM_STEPS, usb_stream_resync_step, and usb_stream_context_t::wr_id.

Referenced by device_audio_task(), and 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().

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.

References usb_stream_resync_ppm, and usb_stream_resync_ppm_ofst.

Referenced by mmi_activity_display().

void usb_stream_resync ( void  )

usb_stream_context_t* usb_stream_context