Ktam3874/pitx software guide – Kontron KTAM3874-pITX User Manual
Page 93

KTD-S0057-I
Page 89 Linux® Programming Examples (DebianTM)
KTAM3874/pITX Software Guide
#include <stdio.h>
#include <getopt.h>
#include <math.h>
#include <alsa/asoundlib.h>
static char *device = "plughw:0,0";
/* playback device */
static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */
static unsigned int rate = 44100;
/* stream rate */
static unsigned int channels = 2;
/* count of channels */
static unsigned int buffer_time = 500000;
/* ring buffer length in us */
static unsigned int period_time = 100000;
/* period time in us */
static double freq = 440;
/* sinusoidal wave frequency in Hz */
static int verbose = 0;
/* verbose flag */
static int resample = 1;
/* enable alsa-lib resampling */
static int period_event = 0;
/* produce poll event after each period */
static snd_pcm_sframes_t buffer_size;
static snd_pcm_sframes_t period_size;
static snd_output_t *output = NULL;
struct async_private_data
{
signed short *samples;
snd_pcm_channel_area_t
*areas;
double
phase;
};
struct transfer_method
{
const char *name;
snd_pcm_access_t
access;
int (*transfer_loop)(snd_pcm_t *handle, signed short *samples, snd_pcm_channel_area_t *areas);
};
static void generate_sine (const snd_pcm_channel_area_t *areas, snd_pcm_uframes_t offset,
int count, double *_phase)
{
static double max_phase = 2. * M_PI;
double phase = *_phase;
double step = max_phase * freq / (double) rate;
unsigned
char
*samples[channels];
int
steps[channels];
unsigned int chn;
int format_bits = snd_pcm_format_width (format);
unsigned int maxval = (1 << (format_bits - 1)) - 1;
int bps = format_bits / 8;
/* bytes per sample */
int phys_bps = snd_pcm_format_physical_width (format) / 8;
int big_endian = snd_pcm_format_big_endian (format) == 1;
int to_unsigned = snd_pcm_format_unsigned (format) == 1;
int is_float = (format == SND_PCM_FORMAT_FLOAT_LE || format == SND_PCM_FORMAT_FLOAT_BE);