spAudio
Loading...
Searching...
No Matches
iotest.c
#include <stdio.h>
#include <sp/spBaseLib.h>
#include <sp/spAudio.h>
#include <sp/spMain.h>
#ifdef SP_SUPPORT_AUDIO
static double rec_time;
static long buffer_size;
static double input_gain;
static double samp_freq;
static int num_channel;
static int samp_bit;
static int num_buffer;
static spBool endless_flag;
static spBool duplex_flag;
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 channel", NULL,
SP_TYPE_INT, &num_channel, "1"},
{"-b", "-bit", "bits per sample", NULL,
SP_TYPE_INT, &samp_bit, "16"},
{"-igain", NULL, "input gain", NULL,
SP_TYPE_DOUBLE, &input_gain, "1.0"},
{"-time", NULL, "recording time [s]", NULL,
SP_TYPE_DOUBLE, &rec_time, "3.0"},
{"-buf", NULL, "buffer size", NULL,
SP_TYPE_LONG, &buffer_size, "256"},
{"-nbuf", NULL, "number of buffers (valid only on Windows/Mac)", NULL,
SP_TYPE_INT, &num_buffer, "64"},
{"-endless", NULL, "recording endlessly (full duplex mode only)", NULL,
SP_TYPE_BOOLEAN, &endless_flag, SP_FALSE_STRING},
{"-duplex", NULL, "full duplex mode", NULL,
SP_TYPE_BOOLEAN, &duplex_flag, SP_FALSE_STRING},
{"-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[] = {
"",
};
int spMain(int argc, char *argv[])
{
long k, l;
long nread;
long length;
long rec_length;
double *wave_data;
long wave_length;
spAudio audio;
spSetHelpMessage(&help_flag, "Audio I/O test");
options = spGetOptions(argc, argv, option, filelabel);
spGetOptionsValue(argc, argv, options);
spSetDebugLevel(debug_level);
if (!spStrNone(plugin_search_path)) {
spDebug(10, "spMain", "plugin_search_path: %s\n", plugin_search_path);
spSetPluginSearchPath(plugin_search_path);
}
rec_length = (long)spRound(rec_time * samp_freq);
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);
spDebug(1, NULL, "set buffer size: %d\n", spSetAudioBufferSize(audio, buffer_size));
if (duplex_flag == SP_TRUE) {
for (l = 0; l < 2; l++) {
/* open input audio device */
if (!spOpenAudioDevice(audio, "r")) {
spError(1, "Can't open audio device.\n");
}
spDebug(1, NULL, "open input audio device done\n");
/* open output audio device */
if (!spOpenAudioDevice(audio, "w")) {
spError(1, "Can't open audio device with full duplex mode.\n");
}
spGetAudioChannel(audio, &num_channel);
wave_length = buffer_size / 2 / num_channel; /* sometimes '/ num_channel' generates better result (which is not indispensable) */
wave_data = xspAlloc(wave_length, double);
length = 0;
for (;;) {
if ((nread = spReadAudioDouble(audio, wave_data, wave_length)) < 0) {
spWarning("Can't read audio data.\n");
break;
}
if (input_gain != 1.0) {
for (k = 0; k < nread; k++) {
wave_data[k] = input_gain * wave_data[k];
}
}
spWriteAudioDouble(audio, wave_data, nread);
if (endless_flag == SP_FALSE) {
length += (nread / num_channel);
if (length >= rec_length) {
break;
}
}
}
}
} else {
for (l = 0; l < 2; l++) {
/* open input audio device */
if (!spOpenAudioDevice(audio, "r")) {
spError(1, "Can't open audio device.\n");
}
spDebug(1, NULL, "open input audio device done\n");
spGetAudioChannel(audio, &num_channel);
wave_length = rec_length * num_channel;
wave_data = xspAlloc(wave_length, double);
nread = spReadAudioDouble(audio, wave_data, wave_length);
spMessage("nread = %ld\n", nread);
if (input_gain != 1.0) {
for (k = 0; k < nread; k++) {
wave_data[k] = input_gain * wave_data[k];
}
}
spMessage("read done\n");
spSleep(1);
/* 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");
spWriteAudioDouble(audio, wave_data, nread);
spMessage("write 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
void spSleep(int sec)
void spError(int status, const char *format,...)
void spSetDebugLevel(int level)
void spWarning(const char *format,...)
void spMessage(const char *format,...)
double spRound(double x)
void spDebug(int level, const char *func_name, const char *format,...)
#define SP_FALSE_STRING
int spBool
#define spStrNone(string)
#define xspAlloc(n, type)
#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)
spAudio spInitAudioDriver(const char *driver_name)
spBool spGetAudioChannel(spAudio audio, int *num_channel)
spBool spSetAudioBufferSize(spAudio audio, int buffer_size)
spBool spSetAudioSampleBit(spAudio audio, int samp_bit)
long spReadAudioDouble(spAudio audio, double *data, long length)
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)