spAudio
Loading...
Searching...
No Matches
playsin.c
#include <stdio.h>
#include <sp/spBaseLib.h>
#include <sp/spAudioLib.h>
#include <sp/spMain.h>
#ifdef SP_SUPPORT_AUDIO
static double fund_freq;
static double dur_time;
static double amplitude;
static double samp_freq;
static int num_channel;
static int samp_bit = 16;
static long buffer_size;
static int num_buffer;
static char *driver_name = NULL;
static char *plugin_search_path = NULL;
static spBool help_flag;
static int debug_level = -1;
static spOptions options;
static spOption option[] = {
{"-f", "-freq", "sampling frequency [Hz]", NULL,
SP_TYPE_DOUBLE, &samp_freq, "8000.0"},
{"-c", "-channel", "number of channels", NULL,
SP_TYPE_INT, &num_channel, "1"},
{"-b", "-bit", "bits per sample", NULL,
SP_TYPE_INT, &samp_bit, "16"},
{"-f0", NULL, "fundamental frequency [Hz]", NULL,
SP_TYPE_DOUBLE, &fund_freq, "1000.0"},
{"-a", "-amp", "amplitude", NULL,
SP_TYPE_DOUBLE, &amplitude, "0.9"},
{"-d", "-dur", "duration [s]", NULL,
SP_TYPE_DOUBLE, &dur_time, "5.0"},
{"-buf", NULL, "buffer size", NULL,
SP_TYPE_LONG, &buffer_size, "16384"},
{"-nbuf", NULL, "number of buffers (valid only on Windows)", NULL,
SP_TYPE_INT, &num_buffer, "16"},
{"-driver", NULL, "audio driver name", NULL,
SP_TYPE_STRING, &driver_name, NULL},
{"-path", NULL, "plugin search path", NULL,
SP_TYPE_STRING, &plugin_search_path, NULL},
{"-debug", NULL, "debug level", NULL,
SP_TYPE_INT, &debug_level, NULL},
{"-help", "-h", "display this message", NULL,
SP_TYPE_BOOLEAN, &help_flag, SP_FALSE_STRING},
};
static const char *filelabel[] = {
"",
};
static spBool callbackFunc(spAudio audio, spAudioCallbackType call_type,
void *data1, void *data2, void *user_data)
{
double samp_rate;
spLong *ppos = (spLong *)data1;
spLong *data_length = (spLong *)user_data;
spGetAudioSampleRate(audio, &samp_rate);
spDebug(10, "callbackFunc", "samp_rate = %f, ppos = %ld / %ld\n", samp_rate, (long)*ppos, (long)*data_length);
printf("Time: %.2f / %.2f\r", (double)*ppos / samp_rate, (double)*data_length / samp_rate);
}
return SP_TRUE;
}
int spMain(int argc, char *argv[])
{
int c;
spLong k;
spLong offset;
spLong data_length;
double period;
double *data;
spAudio audio;
spSetHelpMessage(&help_flag, "Play sin wave");
options = spGetOptions(argc, argv, option, filelabel);
spGetOptionsValue(argc, argv, options);
spSetDebugLevel(debug_level);
spSetDebugStdout(SP_TRUE);
if (!spStrNone(plugin_search_path)) {
spDebug(10, "spMain", "plugin_search_path: %s\n", plugin_search_path);
spSetPluginSearchPath(plugin_search_path);
}
spDebug(10, NULL, "samp_freq = %f, f0 = %f, duration = %f, num_channel = %d\n",
samp_freq, fund_freq, dur_time, num_channel);
num_channel = MAX(num_channel, 1);
data_length = (spLong)spRound(dur_time * samp_freq);
period = samp_freq / fund_freq;
spDebug(1, NULL, "data_length = %ld, period = %f\n", (long)data_length, period);
if ((audio = spInitAudioDriver(driver_name)) == NULL) {
spError(1, "Can't initialize audio.\n");
}
spDebug(1, NULL, "init audio done\n");
spSetAudioSampleRate(audio, samp_freq);
spSetAudioChannel(audio, num_channel);
spSetAudioSampleBit(audio, samp_bit);
spSetAudioNumBuffer(audio, num_buffer);
spSetAudioCallbackFunc(audio, SP_AUDIO_OUTPUT_POSITION_CALLBACK, callbackFunc, &data_length);
spDebug(1, NULL, "set buffer size: %d\n", spSetAudioBufferSize(audio, buffer_size));
/* open output audio device */
if (!spOpenAudioDevice(audio, "w")) {
spError(1, "Can't open audio device.\n");
}
spDebug(1, NULL, "open output audio device done\n");
/* create data */
data = xspAlloc(data_length * num_channel, double);
for (k = 0; k < data_length; k++) {
data[k * num_channel] = amplitude * sin(2.0 * PI * (double)k / period);
for (c = 1; c < num_channel; c++) {
data[k * num_channel + c] = data[k * num_channel];
}
}
/*offset = data_length / 2;*//* for checking */
offset = 0;
/* write data to audio device */
spWriteAudioDouble(audio, data + offset * num_channel, (data_length - offset) * num_channel);
/* close audio device */
xspFree(data);
spDebug(10, "spMain", "done\n");
return 0;
}
#else
int spMain(int argc, char *argv[])
{
spError(1, "Audio is not supported.\n");
return 0;
}
#endif
A class to handle audio I/O.
#define spFreeAudioDriver(audio)
Definition spAudio.h:373
unsigned long spAudioCallbackType
Definition spAudio.h:63
#define SP_AUDIO_OUTPUT_POSITION_CALLBACK
Definition spAudio.h:66
void spError(int status, const char *format,...)
void spSetDebugLevel(int level)
void spSetDebugStdout(int flag)
double spRound(double x)
void spDebug(int level, const char *func_name, const char *format,...)
#define SP_FALSE_STRING
int spBool
#define SP_TRUE
#define spStrNone(string)
#define xspAlloc(n, type)
#define xspFree(p)
#define spGetOptions(argc, argv, option, file_label)
struct _spOptions * spOptions
void spGetOptionsValue(int argc, char **argv, spOptions options)
void spSetHelpMessage(spBool *flag, const char *format,...)
void spSetPluginSearchPath(const char *pathlist)
spAudioCallbackType spSetAudioCallbackFunc(spAudio audio, spAudioCallbackType call_type, spAudioCallbackFunc call_func, void *call_data)
spAudio spInitAudioDriver(const char *driver_name)
spBool spGetAudioSampleRate(spAudio audio, double *samp_rate)
spBool spSetAudioBufferSize(spAudio audio, int buffer_size)
spBool spSetAudioSampleBit(spAudio audio, int samp_bit)
long spWriteAudioDouble(spAudio audio, double *data, long length)
spBool spSetAudioSampleRate(spAudio audio, double samp_rate)
spBool spOpenAudioDevice(spAudio audio, const char *mode)
spBool spSetAudioNumBuffer(spAudio audio, int num_buffer)
spBool spSetAudioChannel(spAudio audio, int num_channel)
spBool spCloseAudioDevice(spAudio audio)