From 4f6b15ea26b5fa09f300f67e4ce1057c2b39a3aa Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 25 Sep 2014 17:08:44 +0200 Subject: New API, new name, new version. --- src/Makefile.am | 46 ++++-- src/aioloop.cc | 191 ++++++++++++++++++++++++ src/aiomixer.cc | 231 +++++++++++++++++++++++++++++ src/airecord.cc | 24 +-- src/audioin.cc | 445 ------------------------------------------------------- src/audioin.h | 130 ---------------- src/audioio.cc | 287 +++++++++++++++++++++++++++++++++++ src/audioio.h | 180 ++++++++++++++++++++++ src/compat.h | 20 --- src/device.cc | 275 ++++++++++++++++++++++++++++++++++ src/device.h | 61 ++++++++ src/mixer.cc | 321 +++++++++++++++++++++++++++++++++++++++ src/mixer.h | 125 ++++++++++++++++ src/sink.cc | 79 ++++++++++ src/sink.h | 56 +++++++ src/source.cc | 81 ++++++++++ src/source.h | 56 +++++++ src/test_audio.h | 112 -------------- 18 files changed, 1988 insertions(+), 732 deletions(-) create mode 100644 src/aioloop.cc create mode 100644 src/aiomixer.cc delete mode 100644 src/audioin.cc delete mode 100644 src/audioin.h create mode 100644 src/audioio.cc create mode 100644 src/audioio.h delete mode 100644 src/compat.h create mode 100644 src/device.cc create mode 100644 src/device.h create mode 100644 src/mixer.cc create mode 100644 src/mixer.h create mode 100644 src/sink.cc create mode 100644 src/sink.h create mode 100644 src/source.cc create mode 100644 src/source.h delete mode 100644 src/test_audio.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 88fd261..094b436 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,19 +1,39 @@ -bin_PROGRAMS = airecord -lib_LTLIBRARIES = libaudioin.la +bin_PROGRAMS = aiomixer aioloop +lib_LTLIBRARIES = libaudioio.la -libaudioin_la_LIBADD = $(ALSA_LIBS) -libaudioin_la_CXXFLAGS = $(ALSA_CFLAGS) +libaudioio_la_LIBADD = $(ALSA_LIBS) +libaudioio_la_CXXFLAGS = $(ALSA_CFLAGS) +libaudioio_la_SOURCES = \ + source.cc \ + sink.cc \ + device.cc \ + mixer.cc \ + audioio.cc -libaudioin_la_SOURCES = \ - audioin.cc +include_HEADERS = \ + audioio.h -airecord_LDADD = libaudioin.la -airecord_SOURCES = \ - airecord.cc +aiomixer_LDADD = $(ALSA_LIBS) +aiomixer_CXXFLAGS = $(ALSA_CFLAGS) +aiomixer_SOURCES = \ + aiomixer.cc \ + source.cc \ + sink.cc \ + device.cc \ + mixer.cc -include_HEADERS = \ - audioin.h +aioloop_LDADD = $(ALSA_LIBS) +aioloop_CXXFLAGS = $(ALSA_CFLAGS) +aioloop_SOURCES = \ + aioloop.cc \ + source.cc \ + sink.cc \ + device.cc \ + mixer.cc EXTRA_DIST = \ - compat.h \ - test_audio.h + device.h \ + mixer.h \ + sink.h \ + source.h + diff --git a/src/aioloop.cc b/src/aioloop.cc new file mode 100644 index 0000000..ad70c72 --- /dev/null +++ b/src/aioloop.cc @@ -0,0 +1,191 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * aioloop.cc + * + * Thu Sep 25 11:25:30 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "device.h" +#include "mixer.h" +#include "source.h" +#include "sink.h" + +#include +#include + +int pcm_size[2]; +char *pcm[2]; +volatile int buf_rcnt = 0; +volatile int buf_wcnt = 0; + +struct semaphore_private_t; + +class Semaphore { +public: + Semaphore(const char *name = ""); + ~Semaphore(); + + void post(); + void wait(); + +private: + struct semaphore_private_t *prv; + const char *name; +}; + +struct semaphore_private_t { + sem_t semaphore; +}; + +Semaphore::Semaphore(const char *name) +{ + prv = new struct semaphore_private_t(); + sem_init(&prv->semaphore, 0, 0); +} + +Semaphore::~Semaphore() +{ + sem_destroy(&prv->semaphore); + if(prv) delete prv; +} + +void Semaphore::post() +{ + sem_post(&prv->semaphore); +} + +void Semaphore::wait() +{ + sem_wait(&prv->semaphore); +} + + +static void* thread_run(void *data); + +class Thread { +public: + virtual ~Thread() {} + void run() + { + pthread_create(&tid, NULL, thread_run, this); + } + + void wait_stop() + { + pthread_join(tid, NULL); + } + + virtual void thread_main() = 0; + +private: + pthread_t tid; +}; + +static void* thread_run(void *data) +{ + Thread *t = (Thread*)data; + t->thread_main(); + return 0; +} + +char ringbuffer[4096 * 10]; + +class Player : public Thread { +public: + Player(Sink *sink) + { + this->sink = sink; + } + + void thread_main() + { + int pos = sizeof(ringbuffer) / 2; + char pcm[940 * sizeof(short)]; + while(1) { + for(unsigned int i = 0; i < sizeof(pcm); i++) { + pcm[i] = ringbuffer[pos % sizeof(ringbuffer)]; + pos++; + } + int sz = sink->writeSamples(pcm, sizeof(pcm)); + if(sz < 1) { + printf("write: %d\n", sz); + continue; + } + } + } + +private: + Sink *sink; +}; + +int main(int argc, char *argv[]) +{ + if(argc < 4) { + printf("Usage: %s card input output\n", argv[0]); + return 1; + } + + size_t buffer_size = 940 * sizeof(short); + pcm[0] = (char *)malloc(buffer_size); + pcm[1] = (char *)malloc(buffer_size); + pcm_size[0] = 0; + pcm_size[1] = 0; + + Device device(argv[1]); + + Source *src = device.getSource(argv[2], 44100, 1); + if(src == NULL) { + printf("Source '%s' failed!\n", argv[2]); + return 1; + } + + Sink *sink = device.getSink(argv[3], 44100, 1); + if(sink == NULL) { + printf("Sink '%s' failed!\n", argv[3]); + return 1; + } + + memset(ringbuffer, 0, sizeof(ringbuffer)); + + Player p(sink); + p.run(); + + int sz = 0; + int pos = sizeof(ringbuffer) / 2; + char pcm[940 * sizeof(short)]; + while(1) { + sz = src->readSamples(pcm, sizeof(pcm)); + for(int i = 0; i < sz; i++) { + ringbuffer[pos % sizeof(ringbuffer)] = pcm[i]; + pos++; + } + if(sz < 1) { + printf("read: %d\n", sz); + continue; + } + } + + delete src; + delete sink; +} + diff --git a/src/aiomixer.cc b/src/aiomixer.cc new file mode 100644 index 0000000..bfe37f9 --- /dev/null +++ b/src/aiomixer.cc @@ -0,0 +1,231 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * aiomixer.cc + * + * Wed Sep 24 11:06:36 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include + +#include "device.h" +#include "mixer.h" + +static const char version_str[] = +"aiomixer v" VERSION "\n" +; + +static const char copyright_str[] = +"Copyright (C) 2014 Bent Bisballe Nyeng - Aasimon.org.\n" +"This is free software. You may redistribute copies of it under the terms of\n" +"the GNU Lesser General Public License " +".\n" +"There is NO WARRANTY, to the extent permitted by law.\n" +"\n" +"Written by Bent Bisballe Nyeng (deva@aasimon.org)\n" +; + +static const char usage_str[] = +"Usage: %s card [-l] [mixer [options]]\n" +"Options:\n" +" -L, --list List controls on card.\n" +" -E, --list-enum List enumeration values\n" +" -e, --enum y Set value of enum to y\n" +" -e, --enum Get value of enum\n" +" -C, --channels Get number of channels in mixer\n" +" -l, --level n=m Set level of channel n to value m\n" +" -l, --level n Get level of channel n\n" +" -r, --range Get range of mixer\n" +" -c, --capture Get capture mode.\n" +" -c, --capture n Set capture mode to n (1 or 0).\n" +" -v, --version Print version information and exit.\n" +" -h, --help Print this message and exit.\n" +; + +int main(int argc, char *argv[]) +{ + int c; + char *p; + + bool list = false; + bool list_enum = false; + bool range = false; + bool num_channels = false; + + std::string enum_name; + std::string enum_value; + + std::string level_name; + std::string level_value; + + int option_index = 0; + while(1) { + static struct option long_options[] = { + {"list", no_argument, 0, 'L'}, + {"enum", required_argument, 0, 'e'}, + {"level", required_argument, 0, 'l'}, + {"version", no_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0} + }; + + c = getopt_long (argc, argv, "hvLe:l:rc", long_options, &option_index); + + if (c == -1) + break; + + switch(c) { + case 'L': + list = true; + break; + + case 'r': + range = true; + break; + + case 'c': + num_channels = true; + break; + + case 'e': + p = strchr(optarg, '='); + if(p) { + *p = '\0'; + enum_value = p+1; + } + enum_name = optarg; + if(enum_name == "") list_enum = true; + break; + + case 'l': + p = strchr(optarg, '='); + if(p) { + *p = '\0'; + level_value = p+1; + } + level_name = optarg; + break; + + case '?': + case 'h': + printf("%s", version_str); + printf(usage_str, argv[0]); + return 0; + + case 'v': + printf("%s", version_str); + printf("%s", copyright_str); + return 0; + + default: + break; + } + } + + std::string device; + std::string mixer; + + int arg = 0; + if(option_index < argc) { + while(optind < argc) { + switch(arg) { + case 0: device = argv[optind++]; break; + case 1: mixer = argv[optind++]; break; + default: + printf("Unknown third argument.\n"); + printf(usage_str, argv[0]); + return 1; + } + arg++; + } + } + + if(device == "") { + printf("Missing device argument.\n"); + printf(usage_str, argv[0]); + return 1; + } + + Device dev(device); + + if(list) { + std::vector mlist = dev.mixerNames(); + std::vector::iterator i = mlist.begin(); + while(i != mlist.end()) { + printf("%s\n", i->c_str()); + i++; + } + return 0; + } + + if(mixer != "") { + Mixer *mix = dev.getMixer(mixer); + if(!mix) return 1; + + if(range) { + Mixer::range_t range = mix->range(); + printf("Range: [%f ; %f]dB\n", range.min, range.max); + } + + if(num_channels) { + int num = mix->numberOfChannels(); + printf("Number of channels: %d\n", num); + } + + /* + for(int i = 0; i < num; i++) { + printf("Channel: %d\n", i); + float val = mix->level(i); + printf(" Level %fdB\n", val); + mix->setLevel(i, val - 1.1); + printf(" Level %fdB\n", mix->level(i)); + mix->setLevel(i, val); + printf(" Level %fdB\n", mix->level(i)); + } + */ + + if(list_enum) { + std::vector evs = mix->enumValues(); + std::vector::iterator i = evs.begin(); + printf("Enum values:\n"); + while(i != evs.end()) { + printf(" - '%s'\n", (*i).c_str()); + i++; + } + printf("\n"); + } + + if(enum_name != "" && enum_value == "") { + std::string ev = mix->enumValue(); + printf("Enum value: '%s'\n", ev.c_str()); + } + + // bool capt = mix->capture(); + // mix->setCapture(!capt); + + delete mix; + } + + return 0; +} diff --git a/src/airecord.cc b/src/airecord.cc index 8a40edc..e340832 100644 --- a/src/airecord.cc +++ b/src/airecord.cc @@ -10,19 +10,19 @@ /* * This file is part of libaudioin. * - * libaudioin is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * libaudioin is distributed in the hope that it will be useful, + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with libaudioin; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include diff --git a/src/audioin.cc b/src/audioin.cc deleted file mode 100644 index cc6fffc..0000000 --- a/src/audioin.cc +++ /dev/null @@ -1,445 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * audioin.cc - * - * Wed Sep 30 11:36:18 CEST 2009 - * Copyright 2011 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of libaudioin. - * - * libaudioin is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * libaudioin is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with libaudioin; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include "audioin.h" - -// Use the newer ALSA API -#define ALSA_PCM_NEW_HW_PARAMS_API - -#include -#include -#include -#include -#include - -class AudioIn { -public: - class CouldNotOpenPCMDevice : public std::exception {}; - class UnableToSetHWParams : public std::exception {}; - class PcmBufferTooSmall : public std::exception {}; - class OverRun : public std::exception {}; - class ReadError : public std::exception {}; - class ShortRead : public std::exception {}; - class CouldNotInitialiseParams : public std::exception {}; - class CouldNotSetAccessMode : public std::exception {}; - class CouldNotSetFormat : public std::exception {}; - class CouldNotSetChannelNumber : public std::exception {}; - class UnableToSetSampleRate : public std::exception {}; - class UnableToSetPeriodSize : public std::exception {}; - class MixerInitilisationFailed : public std::exception {}; - class MixerNotInitialised : public std::exception {}; - class InvalidMixerLevel : public std::exception {}; - class IllegalChannelNumber : public std::exception {}; - class CouldNotSetMixerLevel : public std::exception {}; - - AudioIn(std::string device, std::string mixer, unsigned int srate, int ch); - ~AudioIn(); - - /** - * Reads a number of samples from the soundcard. - * The returned signal is a 16bit mono pcm signal. - */ - size_t read(void *buf, size_t size); - - int set_level(unsigned int channel, float level); - - unsigned int get_samplerate(); - - void set_enable_noise_fix(bool fixit); - -private: - bool noisefix; - unsigned int samplerate; - int channels; - snd_pcm_t *handle; - snd_pcm_hw_params_t *params; - unsigned int val; - int dir; - snd_pcm_uframes_t frames; - - snd_mixer_t *mixhnd; - snd_mixer_elem_t *elem; - long lvl_min, lvl_max; -}; - -AudioIn::AudioIn(std::string device, std::string mixer_interface, - unsigned int srate, int ch) -{ - int open_mode = 0; - //open_mode |= SND_PCM_NONBLOCK; - //open_mode |= SND_PCM_NO_AUTO_RESAMPLE; - //open_mode |= SND_PCM_NO_AUTO_CHANNELS; - //open_mode |= SND_PCM_NO_AUTO_FORMAT; - //open_mode |= SND_PCM_NO_SOFTVOL; - elem = NULL; - mixhnd = NULL; - - noisefix = false; - - samplerate = 0; - - int rc; - - // Open PCM device for recording (capture). - rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_CAPTURE, open_mode); - if (rc < 0) throw CouldNotOpenPCMDevice(); - - // Allocate a hardware parameters object. - snd_pcm_hw_params_alloca(¶ms); - - // Fill it in with default values. - rc = snd_pcm_hw_params_any(handle, params); - if (rc < 0) throw CouldNotInitialiseParams(); - - // Set the desired hardware parameters. - - // Interleaved mode - rc = snd_pcm_hw_params_set_access(handle, params, - SND_PCM_ACCESS_RW_INTERLEAVED); - if (rc < 0) throw CouldNotSetAccessMode(); - - // Signed 16-bit little-endian format - rc = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); - if (rc < 0) throw CouldNotSetFormat(); - - // Set channels (stereo/mono) - rc = snd_pcm_hw_params_set_channels(handle, params, ch); - channels = ch; - if (rc < 0) throw CouldNotSetChannelNumber(); - - // Set sampling rate - samplerate = srate; - rc = snd_pcm_hw_params_set_rate_near(handle, params, &samplerate, &dir); - if(rc < 0) throw UnableToSetSampleRate(); - //if(samplerate != srate) throw UnableToSetSampleRate(); - - // NOTE: Setting period size to 32 frames will force use of lowest possible value. - frames = 512; - rc = snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); - if(rc < 0) throw UnableToSetPeriodSize(); - - // Write the parameters to the driver - rc = snd_pcm_hw_params(handle, params); - if (rc < 0) throw UnableToSetHWParams(); - - if(mixer_interface == "") return; - - // - // Set up mixer - // - snd_mixer_selem_id_t *mixer; - - // Open mixer device - if(snd_mixer_open(&mixhnd, 0) != 0) throw MixerInitilisationFailed(); - - if(snd_mixer_attach(mixhnd, device.c_str()) != 0) - throw MixerInitilisationFailed(); - - if(snd_mixer_selem_register(mixhnd, NULL, NULL) < 0) - throw MixerInitilisationFailed(); - - if(snd_mixer_load(mixhnd) < 0) - throw MixerInitilisationFailed(); - - // Obtain mixer element - snd_mixer_selem_id_alloca(&mixer); - if(mixer == NULL) throw MixerInitilisationFailed(); - snd_mixer_selem_id_set_name(mixer, mixer_interface.c_str()); - - elem = snd_mixer_find_selem(mixhnd, mixer); - if(elem == NULL) throw MixerInitilisationFailed(); - - if(snd_mixer_selem_get_capture_volume_range(elem, &lvl_min, &lvl_max) != 0) { - throw MixerInitilisationFailed(); - } - - if(snd_mixer_selem_set_capture_switch(elem, - SND_MIXER_SCHN_FRONT_LEFT, 1) != 0) { - throw MixerInitilisationFailed(); - } - - if(snd_mixer_selem_set_capture_switch(elem, - SND_MIXER_SCHN_FRONT_RIGHT, 1) != 0) { - throw MixerInitilisationFailed(); - } - - // Obtain mixer enumeration element "Input Source" and set it to 2 (line). - snd_mixer_selem_id_set_name(mixer, "Input Source"); - - snd_mixer_elem_t *iselem = snd_mixer_find_selem(mixhnd, mixer); - if(iselem == NULL) return; - - if(snd_mixer_selem_is_enumerated(iselem)) { - // Set to line-in - for(int i = 0; i < 3; i++) { - char name[16]; - snd_mixer_selem_get_enum_item_name(iselem, i, sizeof(name), name); - if(std::string(name) == "Line") { - snd_mixer_selem_set_enum_item(iselem, SND_MIXER_SCHN_MONO, i); - } - } - } -} - -AudioIn::~AudioIn() -{ - if(handle) { - snd_pcm_drain(handle); - snd_pcm_close(handle); - } - if(mixhnd) snd_mixer_close(mixhnd); -} - -size_t AudioIn::read(void *buf, size_t size) -{ - int rc; - - if(size < frames * sizeof(short) * channels) { - throw PcmBufferTooSmall(); - } - - rc = snd_pcm_readi(handle, buf, frames); - if (rc == -EPIPE) { - // EPIPE means overrun - snd_pcm_prepare(handle); - throw OverRun(); - return 0; - } else if (rc < 0) { - throw ReadError(); - } else if (rc != (int)frames) { - throw ShortRead(); - } - - return rc * sizeof(short) * channels; -} - -unsigned int AudioIn::get_samplerate() -{ - return samplerate; -} - -int AudioIn::set_level(unsigned int channel, float level) -{ - - if(!elem) throw MixerNotInitialised(); - - if(level > 1.0 || level < 0.0) throw InvalidMixerLevel(); - - long lvl = ((lvl_max - lvl_min) * level) + lvl_min; - - snd_mixer_selem_channel_id_t ch; - switch(channel) { - case 0: - ch = SND_MIXER_SCHN_FRONT_LEFT; - break; - case 1: - ch = SND_MIXER_SCHN_FRONT_RIGHT; - break; - default: - throw IllegalChannelNumber(); - break; - } - if(snd_mixer_selem_set_capture_volume(elem, ch, lvl) != 0) { - throw CouldNotSetMixerLevel(); - } - return 0; -} - - -#define MAGIC 0xdeadbeef - -struct ai_t { - unsigned int magic; - AudioIn *ai; -}; - -struct ai_t *ai_init(int *err, const char *device, const char *mixer, - unsigned int srate, unsigned int ch) -{ - *err = NO_ERROR; - - struct ai_t *handle = new ai_t; - handle->magic = MAGIC; - - if(handle == NULL) { - *err = OUT_OF_MEMORY; - return NULL; - } - - try { - handle->ai = new AudioIn(device, mixer, srate, ch); - } catch(AudioIn::CouldNotOpenPCMDevice &e) { *err = COULD_NOT_OPEN_DEVICE; - } catch(AudioIn::UnableToSetHWParams &e) { *err = COULD_NOT_SET_HW_PARAMS; - } catch(AudioIn::CouldNotInitialiseParams &e) { *err = COULD_NOT_INIT_PARAMS; - } catch(AudioIn::CouldNotSetAccessMode &e) { *err = COULD_NOT_SET_ACCESS_MODE; - } catch(AudioIn::CouldNotSetFormat &e) { *err = COULD_NOT_SET_FORMAT; - } catch(AudioIn::CouldNotSetChannelNumber &e) { *err = COULD_NOT_SET_CHANNELS; - } catch(AudioIn::UnableToSetSampleRate &e) { *err = COULD_NOT_SET_SAMPLE_RATE; - } catch(AudioIn::UnableToSetPeriodSize &e) { *err = COULD_NOT_SET_PERIOD_SIZE; - } catch(AudioIn::MixerInitilisationFailed &e) { *err = MIXER_INIT_FAILED; - } - - if(*err != NO_ERROR) { - handle->magic = 0; - delete handle; - return NULL; - } - - return handle; -} - -int ai_read(int *err, struct ai_t *handle, void *pcm, unsigned int maxsize) -{ - if(*err == 42) { // Magic debug function - short *p = (short*)pcm; - for(size_t i = 0; i < maxsize / sizeof(short); i++) { - p[i] = i; - } - *err = NO_ERROR; - return maxsize; - } - - *err = NO_ERROR; - - if(handle == NULL || handle->magic != MAGIC || handle->ai == NULL) { - *err = MISSING_HANDLE; - return -1; - } - - try { - return handle->ai->read(pcm, maxsize); - } catch(AudioIn::PcmBufferTooSmall &e) { *err = BUFFER_TOO_SMALL; - } catch(AudioIn::OverRun &e) { *err = BUFFER_OVERRUN; - } catch(AudioIn::ReadError &e) { *err = READ_ERROR; - } catch(AudioIn::ShortRead &e) { *err = SHORT_READ; - } - - return -1; -} - -int ai_get_samplerate(int *err, struct ai_t *handle) -{ - *err = NO_ERROR; - - if(handle == NULL || handle->magic != MAGIC || handle->ai == NULL) { - *err = MISSING_HANDLE; - return -1; - } - - return (int)handle->ai->get_samplerate(); -} - - -int ai_set_mixer_level(int *err, struct ai_t *handle, unsigned int channel, - float level) -{ - *err = NO_ERROR; - - if(handle == NULL || handle->magic != MAGIC || handle->ai == NULL) { - *err = MISSING_HANDLE; - return -1; - } - - try { - return handle->ai->set_level(channel, level); - } catch(AudioIn::MixerNotInitialised &e) { *err = MIXER_NOT_INITIALISED; - } catch(AudioIn::InvalidMixerLevel &e) { *err = INVALID_MIXER_LEVEL; - } catch(AudioIn::IllegalChannelNumber &e) { *err = INVALID_CHANNEL_NUMBER; - } catch(AudioIn::CouldNotSetMixerLevel &e) { *err = COULD_NOT_SET_MIXER_LEVEL; - } - - return -1; -} - -void ai_close(int *err, struct ai_t *handle) -{ - *err = NO_ERROR; - - if(handle == NULL || handle->magic != MAGIC || handle->ai == NULL) { - *err = MISSING_HANDLE; - return; - } - - delete handle->ai; - handle->ai = NULL; - handle->magic = 0; - delete handle; -} - -#ifdef TEST_AUDIOIN -//deps: -//cflags: $(ALSA_CFLAGS) -//libs: $(ALSA_LIBS) - -#include -#include "test_audio.h" -#include - -TEST_BEGIN; -int err; -struct ai_t *h; - -h = ai_init(&err, "default", "H/W Multi", 44100, 2); -TEST_EQUAL_INT(err, 0, "Check for errors."); -TEST_NOTEQUAL(h, NULL, "Check handle."); - -ai_close(&err, h); -TEST_EQUAL_INT(err, 0, "Check for errors."); - -h = ai_init(&err, "default", "H/W Multi", 22050, 1); -TEST_EQUAL_INT(err, 0, "Check for errors."); -TEST_NOTEQUAL(h, NULL, "Check handle."); - -char pcm[1880]; // Hold exactly one frame. -int r; - -r = ai_read(&err, h, pcm, 0); -TEST_EQUAL_INT(r, -1, "We should read something."); -TEST_EQUAL_INT(err, BUFFER_TOO_SMALL, "Check for errors."); - -for(int i = 0; i < 100; i++) { - r = ai_read(&err, h, pcm, sizeof(pcm)); -} -TEST_EQUAL_INT(err, 0, "Check for errors."); -TEST_NOTEQUAL_INT(r, 0, "We should read something."); - -char ref[sizeof(pcm)]; -memset(ref, 0, sizeof(ref)); -double diff = compareBuffers(r/sizeof(short), 1, pcm, ref); -TEST_LESS_THAN_FLOAT(diff, 1000.0, "Compare buffers (with no mic on the soundcard)"); - -short p[1024]; -err = 42; -ai_read(&err, h, p, sizeof(p)); -for(size_t i = 0; i < sizeof(p) / sizeof(short); i++) { - TEST_EQUAL_INT(p[i], i, "Compare slide."); -} - -ai_close(&err, h); - -TEST_END; - -#endif/*TEST_AUDIOIN*/ diff --git a/src/audioin.h b/src/audioin.h deleted file mode 100644 index fe12fa4..0000000 --- a/src/audioin.h +++ /dev/null @@ -1,130 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * audioin.h - * - * Wed Sep 30 11:36:18 CEST 2009 - * Copyright 2011 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of libaudioin. - * - * libaudioin is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * libaudioin is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with libaudioin; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#ifndef __LIBAUDIOIN_AUDIOIN_H__ -#define __LIBAUDIOIN_AUDIOIN_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#define NO_ERROR 0 - -#define OUT_OF_MEMORY 101 -#define MISSING_HANDLE 102 - -#define COULD_NOT_OPEN_DEVICE 203 -#define COULD_NOT_SET_HW_PARAMS 204 -#define COULD_NOT_INIT_PARAMS 205 -#define COULD_NOT_SET_ACCESS_MODE 206 -#define COULD_NOT_SET_FORMAT 207 -#define COULD_NOT_SET_CHANNELS 208 -#define COULD_NOT_SET_SAMPLE_RATE 209 -#define COULD_NOT_SET_PERIOD_SIZE 210 - -#define BUFFER_TOO_SMALL 305 -#define BUFFER_OVERRUN 306 -#define READ_ERROR 307 -#define SHORT_READ 308 - -#define MIXER_INIT_FAILED 400 - -#define MIXER_NOT_INITIALISED 401 -#define INVALID_MIXER_LEVEL 402 -#define INVALID_CHANNEL_NUMBER 403 -#define COULD_NOT_SET_MIXER_LEVEL 404 - -struct ai_t; - -/** - * Initalise the AudioIn library and connect to a soundcard and a mixer. - * @param err An int pointer containing error value if function is unsuccessful. - * @param device A string containing the device name to connect to. A list - * possible devices can be seen with the 'aplay -L' command. The device - * "default" are usually the one needed. - * @param mixer A string containing the mixer interface to connect to. A list - * of possible interfaces can be seen with the 'amixer scontrols' command. - * Usually the interface "Capture" is the one needed. - * @param samplerate An unsigned integer containing the desired samplerate in - * Hertz. - * @param channels An unsigned integer containing the desired number of - * channels. The channel samples will be interleaved in the data stream whe - * ai_read is called. - * @return A pointer to the newly created handle or NULL on failure. - */ -struct ai_t *ai_init(int *err, const char *device, const char *mixer, - unsigned int samplerate, unsigned int channels); - -/** - * Read samples from the soundcard. - * @param err An int pointer containing error value if function is unsuccessful. - * @param handle A pointer to the handle to be used. - * @param maxsize The maximum number of bytes (not samples) to read. - * @return Returns the number of bytes (not samples) read or -1 on error. - * NOTE: to get the number of samples read, devide the return value with the - * sample width (2 bytes / 16 bits) and the number of interleaved channels - * (usually 1 or 2). - */ -int ai_read(int *err, struct ai_t *handle, void *pcm, unsigned int maxsize); - -/** - * Adjust channel mixer levels. - * @param err An int pointer containing error value if function is unsuccessful. - * @param handle A pointer to the handle to be used. - * @param channel The channel number to set mixer level of. 0 or 1 should be - * used for the two channels of a stereo interface. - * @param level The mixer level to set, ranging from 0.0 to 1.0 where 0.0 is - * mute and 1.0 is maximum gain. - * @return Returns -1 on error 0 otherwise. - */ -int ai_set_mixer_level(int *err, struct ai_t *handle, unsigned int channel, - float level); - -/** - * Get actual samplerate. - * The samplerate set in ai_init may or may not match a possible samplerate for - * the audio hardware and therefore the ALSA library might decide to use another - * samplerate than the one specified. - * This function can be called to obtain the actual samplerate that the hardware - * has been configured to use. - * @param err An int pointer containing error value if function is unsuccessful. - * @param handle A pointer to the handle to be used. - * @return The samplerate as an integer or -1 on error. - */ -int ai_get_samplerate(int *err, struct ai_t *handle); - -/** - * Close and free the handle. - * @param err An int pointer containing error value if function is unsuccessful. - * @param handle A pointer to the handle to be closed. - */ -void ai_close(int *err, struct ai_t *handle); - -#ifdef __cplusplus -} -#endif - -#endif/*__LIBAUDIOIN_AUDIOIN_H__*/ diff --git a/src/audioio.cc b/src/audioio.cc new file mode 100644 index 0000000..bb04762 --- /dev/null +++ b/src/audioio.cc @@ -0,0 +1,287 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * audioio.cc + * + * Thu Sep 25 15:55:14 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "audioio.h" + +#include "device.h" +#include "mixer.h" +#include "source.h" +#include "sink.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* +#define NO_ERROR 0 + +#define OUT_OF_MEMORY -101 +#define MISSING_HANDLE -102 + +#define COULD_NOT_OPEN_DEVICE -203 +#define COULD_NOT_SET_HW_PARAMS -204 +#define COULD_NOT_INIT_PARAMS -205 +#define COULD_NOT_SET_ACCESS_MODE -206 +#define COULD_NOT_SET_FORMAT -207 +#define COULD_NOT_SET_CHANNELS -208 +#define COULD_NOT_SET_SAMPLE_RATE -209 +#define COULD_NOT_SET_PERIOD_SIZE -210 + +#define BUFFER_TOO_SMALL -305 +#define BUFFER_OVERRUN -306 +#define READ_ERROR -307 +#define SHORT_READ -308 + +#define MIXER_INIT_FAILED -400 + +#define MIXER_NOT_INITIALISED -401 +#define INVALID_MIXER_LEVEL -402 +#define INVALID_CHANNEL_NUMBER -403 +#define COULD_NOT_SET_MIXER_LEVEL -404 +*/ + +#define MAGIC 0xdeadbeef + +struct aio_t { + unsigned int magic; + + Device *device; + + Source *source; + Mixer *source_mixer; + + Sink *sink; + Mixer *sink_mixer; +}; + +// Check if 'h' is a valid aio_t handle and report/return error if not. +#define CHECK_HANDLE(h) \ + do { \ + if(!h || h->magic != MAGIC) { \ + return MISSING_HANDLE; \ + } \ + } while(0) + +struct aio_t *aio_init(int *err, + const char *playback_device, + const char *playback_mixer, + const char *capture_device, + const char *capture_mixer, + unsigned int samplerate, + unsigned int channels) +{ + *err = NO_ERROR; + + const char *device = playback_device; + if(device == NULL || strlen(device) == 0) device = capture_device; + if(device == NULL || strlen(device) == 0) { + *err = COULD_NOT_OPEN_DEVICE; + return NULL; + } + + struct aio_t *h = new aio_t; + memset(h, 0, sizeof(struct aio_t)); + h->magic = MAGIC; + + + if(h == NULL) { + *err = OUT_OF_MEMORY; + return NULL; + } + + h->device = new Device(device); + + if(playback_device != NULL && strlen(playback_device) > 0) { + h->sink = h->device->getSink(playback_device, samplerate, channels); + if(h->sink == NULL) { + *err = COULD_NOT_OPEN_DEVICE; + } + } + + if(playback_mixer != NULL && strlen(playback_mixer) > 0) { + h->sink_mixer = h->device->getMixer(playback_mixer); + if(h->sink_mixer == NULL) { + *err = MIXER_INIT_FAILED; + } + } + + if(capture_device != NULL && strlen(capture_device) > 0) { + h->source = h->device->getSource(capture_device, samplerate, channels); + if(h->sink == NULL) { + *err = COULD_NOT_OPEN_DEVICE; + } + } + + if(capture_mixer != NULL && strlen(capture_mixer) > 0) { + h->source_mixer = h->device->getMixer(capture_mixer); + if(h->source_mixer == NULL) { + *err = MIXER_INIT_FAILED; + } + } + + if(*err != NO_ERROR) { + h->magic = 0; + if(h->source) delete h->source; + if(h->source_mixer) delete h->source_mixer; + if(h->sink) delete h->sink; + if(h->sink_mixer) delete h->sink_mixer; + delete h; + return NULL; + } + + return h; +} + +int aio_read(struct aio_t *h, char *pcm, unsigned int maxsize) +{ + CHECK_HANDLE(h); + + if(h->source) { + return h->source->readSamples(pcm, maxsize); + } + + return MISSING_HANDLE; +} + +int aio_write(struct aio_t *h, const char *pcm, unsigned int size) +{ + CHECK_HANDLE(h); + + if(h->sink) { + return h->sink->writeSamples(pcm, size); + } + + return MISSING_HANDLE; +} + +int aio_set_playback_mixer_level(struct aio_t *h, unsigned int c, float level) +{ + CHECK_HANDLE(h); + + if(h->sink_mixer) { + h->sink_mixer->setLevel(c, level); + return 0; + } + + return MISSING_HANDLE; +} + +int aio_get_playback_mixer_level(struct aio_t *h, unsigned int c, float *level) +{ + CHECK_HANDLE(h); + + if(h->sink_mixer) { + float l = h->sink_mixer->level(c); + if(l < -1000) return INVALID_MIXER_LEVEL; + *level = l; + return 0; + } + + return MISSING_HANDLE; +} + +int aio_get_playback_mixer_level_range(struct aio_t *h, float *min, float *max) +{ + CHECK_HANDLE(h); + + if(h->sink_mixer) { + Mixer::range_t r = h->sink_mixer->range(); + *min = r.min; + *max = r.max; + return 0; + } + + return MISSING_HANDLE; +} + +int aio_set_capture_mixer_level(struct aio_t *h, unsigned int c, float level) +{ + CHECK_HANDLE(h); + + if(h->source_mixer) { + h->source_mixer->setLevel(c, level); + return 0; + } + + return MISSING_HANDLE; +} + +int aio_get_capture_mixer_level(struct aio_t *h, unsigned int c, float *level) +{ + CHECK_HANDLE(h); + + if(h->source_mixer) { + float l = h->source_mixer->level(c); + if(l < -1000) return INVALID_MIXER_LEVEL; + *level = l; + return 0; + } + + return MISSING_HANDLE; +} + +int aio_get_capture_mixer_level_range(struct aio_t *h, float *min, float *max) +{ + CHECK_HANDLE(h); + + if(h->source_mixer) { + Mixer::range_t r = h->source_mixer->range(); + *min = r.min; + *max = r.max; + return 0; + } + + return MISSING_HANDLE; +} + +int aio_get_samplerate(struct aio_t *h) +{ + CHECK_HANDLE(h); + + if(h->source) return h->source->samplerate(); + else if(h->sink) return h->sink->samplerate(); + + return MISSING_HANDLE; +} + +int aio_close(struct aio_t *h) +{ + CHECK_HANDLE(h); + + h->magic = 0; + if(h->source) delete h->source; + if(h->source_mixer) delete h->source_mixer; + if(h->sink) delete h->sink; + if(h->sink_mixer) delete h->sink_mixer; + delete h; + + return 0; +} + +#ifdef __cplusplus +} +#endif diff --git a/src/audioio.h b/src/audioio.h new file mode 100644 index 0000000..f003d02 --- /dev/null +++ b/src/audioio.h @@ -0,0 +1,180 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * audioio.h + * + * Thu Sep 25 15:55:14 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __LIBAUDIOIO_AUDIOIO_H__ +#define __LIBAUDIOIO_AUDIOIO_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define NO_ERROR 0 + +#define OUT_OF_MEMORY -101 +#define MISSING_HANDLE -102 + +#define COULD_NOT_OPEN_DEVICE -203 +#define COULD_NOT_SET_HW_PARAMS -204 +#define COULD_NOT_INIT_PARAMS -205 +#define COULD_NOT_SET_ACCESS_MODE -206 +#define COULD_NOT_SET_FORMAT -207 +#define COULD_NOT_SET_CHANNELS -208 +#define COULD_NOT_SET_SAMPLE_RATE -209 +#define COULD_NOT_SET_PERIOD_SIZE -210 + +#define BUFFER_TOO_SMALL -305 +#define BUFFER_OVERRUN -306 +#define READ_ERROR -307 +#define SHORT_READ -308 + +#define MIXER_INIT_FAILED -400 + +#define MIXER_NOT_INITIALISED -401 +#define INVALID_MIXER_LEVEL -402 +#define INVALID_CHANNEL_NUMBER -403 +#define COULD_NOT_SET_MIXER_LEVEL -404 + +struct aio_t; + +/** + * Initalise the AudioIO library and connect to a soundcard and a mixer. + * @param err An int pointer containing error value if function is unsuccessful. + * @param device A string containing the device name to connect to. A list + * possible devices can be seen with the 'aplay -L' command. The device + * "default" are usually the one needed. + * @param mixer A string containing the mixer interface to connect to. A list + * of possible interfaces can be seen with the 'amixer scontrols' command. + * Usually the interface "Capture" is the one needed. + * @param samplerate An unsigned integer containing the desired samplerate in + * Hertz. + * @param channels An unsigned integer containing the desired number of + * channels. The channel samples will be interleaved in the data stream whe + * ai_read is called. + * @return A pointer to the newly created handle or NULL on failure. + */ +struct aio_t *aio_init(int *err, + const char *playback_device, + const char *playback_mixer, + const char *capture_device, + const char *capture_mixer, + unsigned int samplerate, + unsigned int channels); + +/** + * Read samples from the soundcard. + * @param err An int pointer containing error value if function is unsuccessful. + * @param handle A pointer to the handle to be used. + * @param pcm Buffer cotaining which will be filled with samples. + * @param maxsize The maximum number of bytes (not samples) to read. + * @return Returns the number of bytes (not samples) read or -1 on error. + * NOTE: to get the number of samples read, devide the return value with the + * sample width (2 bytes / 16 bits) and the number of interleaved channels + * (usually 1 or 2). + */ +int aio_read(struct aio_t *handle, char *pcm, unsigned int maxsize); + +/** + * Write samples to the soundcard. + * @param err An int pointer containing error value if function is unsuccessful. + * @param handle A pointer to the handle to be used. + * @param size The maximum number of bytes (not samples) to be written. + * @return Returns the number of bytes (not samples) written or -1 on error. + * NOTE: to get the number of samples read, devide the return value with the + * sample width (2 bytes / 16 bits) and the number of interleaved channels + * (usually 1 or 2). + */ +int aio_write(struct aio_t *handle, const char *pcm, unsigned int size); + +#if 0 +/** + * Adjust channel mixer levels. + * @param err An int pointer containing error value if function is unsuccessful. + * @param handle A pointer to the handle to be used. + * @param channel The channel number to set mixer level of. 0 or 1 should be + * used for the two channels of a stereo interface. + * @param level The mixer level to set in dB + * @return Returns -1 on error 0 otherwise. + */ +int aio_set_mixer_level(struct ai_t *handle, unsigned int channel, float level); + +/** + * Get channel mixer levels. + * @param err An int pointer containing error value if function is unsuccessful. + * @param handle A pointer to the handle to be used. + * @param channel The channel number to get mixer level of. 0 or 1 should be + * used for the two channels of a stereo interface. + * @return Returns the level in dB. + */ +int aio_get_mixer_level(struct aio_t *handle, unsigned int channel, + float *level); + +int aio_get_mixer_level_range(struct aio_t *handle, float *min, float *max); +#endif + +int aio_set_playback_mixer_level(struct aio_t *handle, unsigned int channel, + float level); + +int aio_get_playback_mixer_level(struct aio_t *handle, unsigned int channel, + float *level); + +int aio_get_playback_mixer_level_range(struct aio_t *handle, + float *min, float *max); + +int aio_set_capture_mixer_level(struct aio_t *handle, unsigned int channel, + float level); + +int aio_get_capture_mixer_level(struct aio_t *handle, unsigned int channel, + float *level); + +int aio_get_capture_mixer_level_range(struct aio_t *handle, + float *min, float *max); + +/** + * Get actual samplerate. + * The samplerate set in ai_init may or may not match a possible samplerate for + * the audio hardware and therefore the ALSA library might decide to use another + * samplerate than the one specified. + * This function can be called to obtain the actual samplerate that the hardware + * has been configured to use. + * @param err An int pointer containing error value if function is unsuccessful. + * @param handle A pointer to the handle to be used. + * @return The samplerate as an integer or -1 on error. + */ +int aio_get_samplerate(struct aio_t *handle); + +/** + * Close and free the handle. + * @param err An int pointer containing error value if function is unsuccessful. + * @param handle A pointer to the handle to be closed. + */ +int aio_close(struct aio_t *handle); + +#ifdef __cplusplus +} +#endif + +#endif/*__LIBAUDIOIO_AUDIOIO_H__*/ diff --git a/src/compat.h b/src/compat.h deleted file mode 100644 index 868a729..0000000 --- a/src/compat.h +++ /dev/null @@ -1,20 +0,0 @@ -/* -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * File: compat.h - * This file belongs to the Bifrost project. - * Macros for throw compatability. - * Date: Tue Feb 16 14:14:30 CET 2010 - * Author: Bent Bisballe Nyeng - * Copyright: 2010 - * Email: deva@aasimon.org - ****************************************************************************/ -#ifndef __LIBAUDIOIN_COMPAT_H__ -#define __LIBAUDIOIN_COMPAT_H__ - -#ifdef WIN32 -#define _throw(...) -#else -#define _throw(fmt...) throw(fmt) -#endif - -#endif/*__LIBAUDIOIN_COMPAT_H__*/ diff --git a/src/device.cc b/src/device.cc new file mode 100644 index 0000000..f68a90a --- /dev/null +++ b/src/device.cc @@ -0,0 +1,275 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * device.cc + * + * Wed Sep 24 08:54:47 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "device.h" + +Device::Device(std::string device) +{ + card = device; +} + +Device::~Device() +{ + /* + if(handle) { + snd_pcm_drain(handle); + snd_pcm_close(handle); + } + */ +} + +std::vector Device::mixerNames() +{ + std::vector mlist; + + int err; + snd_mixer_t *handle; + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_alloca(&sid); + + if((err = snd_mixer_open(&handle, 0)) < 0) { + printf("Mixer %s open error: %s", card.c_str(), snd_strerror(err)); + return mlist; + } + + if((err = snd_mixer_attach(handle, card.c_str())) < 0) { + printf("Mixer attach %s error: %s", card.c_str(), snd_strerror(err)); + snd_mixer_close(handle); + return mlist; + } + + if((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { + printf("Mixer register error: %s", snd_strerror(err)); + snd_mixer_close(handle); + return mlist; + } + err = snd_mixer_load(handle); + if (err < 0) { + printf("Mixer %s load error: %s", card.c_str(), snd_strerror(err)); + snd_mixer_close(handle); + return mlist; + } + for(elem = snd_mixer_first_elem(handle); + elem; + elem = snd_mixer_elem_next(elem)) { + snd_mixer_selem_get_id(elem, sid); + if(snd_mixer_selem_is_active(elem) == 0) continue; + + char cname[256]; + snprintf(cname, sizeof(cname), "'%s',%i", + snd_mixer_selem_id_get_name(sid), + snd_mixer_selem_id_get_index(sid)); + + mlist.push_back(cname); + } + + snd_mixer_close(handle); + + return mlist; +} + +Mixer *Device::getMixer(std::string name) +{ + int err; + snd_mixer_t *handle; + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_alloca(&sid); + + if((err = snd_mixer_open(&handle, 0)) < 0) { + printf("Mixer %s open error: %s", card.c_str(), snd_strerror(err)); + return NULL; + } + + if((err = snd_mixer_attach(handle, card.c_str())) < 0) { + printf("Mixer attach %s error: %s", card.c_str(), snd_strerror(err)); + snd_mixer_close(handle); + return NULL; + } + + if((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { + printf("Mixer register error: %s", snd_strerror(err)); + snd_mixer_close(handle); + return NULL; + } + err = snd_mixer_load(handle); + if (err < 0) { + printf("Mixer %s load error: %s", card.c_str(), snd_strerror(err)); + snd_mixer_close(handle); + return NULL; + } + for(elem = snd_mixer_first_elem(handle); + elem; + elem = snd_mixer_elem_next(elem)) { + snd_mixer_selem_get_id(elem, sid); + if(snd_mixer_selem_is_active(elem) == 0) continue; + + char cname[256]; + snprintf(cname, sizeof(cname), "'%s',%i", + snd_mixer_selem_id_get_name(sid), + snd_mixer_selem_id_get_index(sid)); + + if(std::string(cname) == name) { + // NOTE: The Mixer object takes ownership of 'handle' which closed in + // its destructor. + return new Mixer(handle, elem); + } + } + + snd_mixer_close(handle); + + printf("Mixer element \"%s\" not found.\n", name.c_str()); + + return NULL; +} + +static int pcm_init(snd_pcm_t *handle, unsigned int *samplerate, + unsigned int channels, snd_pcm_uframes_t *frames) +{ + int err; + snd_pcm_hw_params_t *params; + + // Allocate a hardware parameters object. + snd_pcm_hw_params_alloca(¶ms); + + // Fill it in with default values. + err = snd_pcm_hw_params_any(handle, params); + if(err) { + printf("snd_pcm_hw_params_any: %s, %d\n", snd_strerror(err), err); + return 1; + } + + // Interleaved mode + err = snd_pcm_hw_params_set_access(handle, params, + SND_PCM_ACCESS_RW_INTERLEAVED); + if(err) { + printf("snd_pcm_hw_params_set_access: %s, %d\n", snd_strerror(err), err); + return 1; + } + + // Signed 16-bit little-endian format + err = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); + if(err) { + printf("snd_pcm_hw_params_set_format: %s, %d\n", snd_strerror(err), err); + return 1; + } + + // Set channels (stereo/mono) + err = snd_pcm_hw_params_set_channels(handle, params, channels); + if(err) { + printf("snd_pcm_hw_params_set_channels: %s, %d\n", snd_strerror(err), err); + return 1; + } + + // dir: -1 := exact or first below + // 0 := exact (error if not?) + // 1 := exact or first above + int dir = 1; + + // Set sampling rate + err = snd_pcm_hw_params_set_rate_near(handle, params, samplerate, &dir); + if(err) { + printf("snd_pcm_hw_params_set_rate_near: %s, %d\n", snd_strerror(err), err); + return 1; + } + + // printf("Actual samplerate: %d\n", samplerate); + + err = snd_pcm_hw_params_set_period_size_near(handle, params, frames, &dir); + if(err) { + printf("snd_pcm_hw_params_set_period_size_near: %s, %d\n", + snd_strerror(err), err); + return 1; + } + + // printf("Actual buffersize: %d\n", (int)frames); + + // Write the parameters to the driver + err = snd_pcm_hw_params(handle, params); + if(err) { + printf("snd_pcm_hw_params: %s, %d\n", + snd_strerror(err), err); + return 1; + } + + return 0; +} + +Source *Device::getSource(std::string name, unsigned int samplerate, + unsigned int channels) +{ + int open_mode = 0; + //open_mode |= SND_PCM_NONBLOCK; + //open_mode |= SND_PCM_NO_AUTO_RESAMPLE; + //open_mode |= SND_PCM_NO_AUTO_CHANNELS; + //open_mode |= SND_PCM_NO_AUTO_FORMAT; + //open_mode |= SND_PCM_NO_SOFTVOL; + + int err; + snd_pcm_t *handle; + + // Open PCM device for recording (capture). + err = snd_pcm_open(&handle, name.c_str(), SND_PCM_STREAM_CAPTURE, + open_mode); + if(err) { + printf("snd_pcm_open: %s, %d\n", snd_strerror(err), err); + return NULL; + } + + snd_pcm_uframes_t frames = 512; + if(pcm_init(handle, &samplerate, channels, &frames)) return NULL; + + return new Source(handle, samplerate, channels, frames); +} + +Sink *Device::getSink(std::string name, unsigned int samplerate, + unsigned int channels) +{ + int open_mode = 0; + //open_mode |= SND_PCM_NONBLOCK; + //open_mode |= SND_PCM_NO_AUTO_RESAMPLE; + //open_mode |= SND_PCM_NO_AUTO_CHANNELS; + //open_mode |= SND_PCM_NO_AUTO_FORMAT; + //open_mode |= SND_PCM_NO_SOFTVOL; + + int err; + snd_pcm_t *handle; + + // Open PCM device for recording (capture). + err = snd_pcm_open(&handle, name.c_str(), SND_PCM_STREAM_PLAYBACK, + open_mode); + if(err) { + printf("snd_pcm_open: %s, %d\n", snd_strerror(err), err); + return NULL; + } + + snd_pcm_uframes_t frames = 512; + if(pcm_init(handle, &samplerate, channels, &frames)) return NULL; + + return new Sink(handle, samplerate, channels, frames); +} diff --git a/src/device.h b/src/device.h new file mode 100644 index 0000000..5129d36 --- /dev/null +++ b/src/device.h @@ -0,0 +1,61 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * device.h + * + * Wed Sep 24 08:54:47 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __LIBAUDIOIO_DEVICE_H__ +#define __LIBAUDIOIO_DEVICE_H__ + +#include +#include + +// Make API behave like ALSA 1.x.x with ALSA 0.9.x +#define ALSA_PCM_NEW_HW_PARAMS_API + +#include + +#include "mixer.h" +#include "source.h" +#include "sink.h" + +class Device { +public: + Device(std::string device); + ~Device(); + + std::vector mixerNames(); + Mixer *getMixer(std::string name); + + Source *getSource(std::string name, unsigned int samplerate, + unsigned int channels); + + Sink *getSink(std::string name, unsigned int samplerate, + unsigned int channels); + +private: + std::string card; +}; + +#endif/*__LIBAUDIOIO_DEVICE_H__*/ diff --git a/src/mixer.cc b/src/mixer.cc new file mode 100644 index 0000000..5a6efb7 --- /dev/null +++ b/src/mixer.cc @@ -0,0 +1,321 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * mixer.cc + * + * Tue Sep 23 14:38:54 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "mixer.h" + +#include +#include + +#define fERROR (strtof("-INF", NULL)) + +Mixer::Mixer(snd_mixer_t *handle, snd_mixer_elem_t *elem) +{ + this->handle = handle; + this->elem = elem; + + /* + if (snd_mixer_selem_has_capture_volume(elem) || + snd_mixer_selem_has_capture_switch(elem)) { + printf("Capture channels:\n"); + if(snd_mixer_selem_is_capture_mono(elem)) { + printf(" Mono"); + } else { + for(int ichn = 0; ichn <= (int)SND_MIXER_SCHN_LAST; ichn++) { + snd_mixer_selem_channel_id_t chn = (snd_mixer_selem_channel_id_t)ichn; + if(!snd_mixer_selem_has_capture_channel(elem, chn)) continue; + printf(" - %s\n", snd_mixer_selem_channel_name(chn)); + } + } + } + */ + + + + + + + + /* + snd_mixer_selem_id_t *mixer; + + // Open mixer device + if(snd_mixer_open(&mixhnd, 0) != 0) + throw MixerInitilisationFailed(); + + if(snd_mixer_attach_hctl(mixhnd, hctl) != 0) + throw MixerInitilisationFailed(); + + if(snd_mixer_selem_register(mixhnd, NULL, NULL) < 0) + throw MixerInitilisationFailed(); + + if(snd_mixer_load(mixhnd) < 0) + throw MixerInitilisationFailed(); + + // Obtain mixer element + snd_mixer_selem_id_alloca(&mixer); + if(mixer == NULL) throw MixerInitilisationFailed(); + snd_mixer_selem_id_set_name(mixer, mixer_interface); + + elem = snd_mixer_find_selem(mixhnd, mixer); + if(elem == NULL) throw MixerInitilisationFailed(); + */ + + /* + // Obtain mixer enumeration element "Input Source" and set it to 2 (line). + snd_mixer_selem_id_set_name(mixer, "Input Source"); + + snd_mixer_elem_t *iselem = snd_mixer_find_selem(mixhnd, mixer); + if(iselem == NULL) return; + + if(snd_mixer_selem_is_enumerated(iselem)) { + // Set to line-in + for(int i = 0; i < 3; i++) { + char name[16]; + snd_mixer_selem_get_enum_item_name(iselem, i, sizeof(name), name); + if(std::string(name) == "Line") { + snd_mixer_selem_set_enum_item(iselem, SND_MIXER_SCHN_MONO, i); + } + } + } + */ +} + +Mixer::~Mixer() +{ + if(handle) snd_mixer_close(handle); +} + +snd_mixer_selem_channel_id_t Mixer::chanId(int idx) +{ + int num = 0; + if(snd_mixer_selem_is_capture_mono(elem)) { + if(idx == 0) return SND_MIXER_SCHN_MONO; + } else { + for(int ichn = 0; ichn <= (int)SND_MIXER_SCHN_LAST; ichn++) { + snd_mixer_selem_channel_id_t chn = (snd_mixer_selem_channel_id_t)ichn; + if(!snd_mixer_selem_has_capture_channel(elem, chn) && + !snd_mixer_selem_has_playback_channel(elem, chn)) continue; + if(idx == num) return chn; + num++; + } + } + return SND_MIXER_SCHN_UNKNOWN; +} + +int Mixer::numberOfChannels() +{ + int num = 0; + if(snd_mixer_selem_is_capture_mono(elem)) { + num = 1; + } else { + for(int ichn = 0; ichn <= (int)SND_MIXER_SCHN_LAST; ichn++) { + snd_mixer_selem_channel_id_t chn = (snd_mixer_selem_channel_id_t)ichn; + if(!snd_mixer_selem_has_capture_channel(elem, chn)) continue; + num++; + } + } + + return num; +} + +bool Mixer::isCapture() +{ + return snd_mixer_selem_has_capture_volume(elem) || + snd_mixer_selem_has_capture_switch(elem); +} + +bool Mixer::isPlayback() +{ + return snd_mixer_selem_has_playback_volume(elem); +} + +bool Mixer::isEnum() +{ + return snd_mixer_selem_is_enumerated(elem); +} + +void Mixer::setEnumValue(std::string value) +{ + if(!isEnum()) return; +} + +std::string Mixer::enumValue() +{ + if(!isEnum()) return ""; + + int i; + unsigned int idx; + char itemname[40]; + for(i = 0; !snd_mixer_selem_get_enum_item(elem, + (snd_mixer_selem_channel_id_t)i, + &idx); i++) { + snd_mixer_selem_get_enum_item_name(elem, idx, + sizeof(itemname) - 1, itemname); + return itemname; + } + + return ""; +} + +std::vector Mixer::enumValues() +{ + std::vector values; + if(!isEnum()) return values; + + int i, items; + char itemname[40]; + items = snd_mixer_selem_get_enum_items(elem); + for (i = 0; i < items; i++) { + snd_mixer_selem_get_enum_item_name(elem, (snd_mixer_selem_channel_id_t)i, + sizeof(itemname) - 1, itemname); + values.push_back(itemname); + } + + return values; +} + +int Mixer::setLevel(int channel, float level) +{ + int err; + long int value = level * 100; + // dir: -1 := exact or first below + // 0 := exact (error if not?) + // 1 := exact or first above + int dir = 1; + + if(isCapture()) { + err = snd_mixer_selem_set_capture_dB(elem, chanId(channel), value, dir); + if(err) { + printf("snd_mixer_selem_set_capture_dB: %s, %d\n", + snd_strerror(err), err); + return 1; + } + } + + if(isPlayback()) { + err = snd_mixer_selem_set_playback_dB(elem, chanId(channel), value, dir); + if(err) { + printf("snd_mixer_selem_set_playback_dB: %s, %d\n", + snd_strerror(err), err); + return 1; + } + } + + return 0; +} + +float Mixer::level(int channel) +{ + int err; + long int value; + + if(isCapture()) { + err = snd_mixer_selem_get_capture_dB(elem, chanId(channel), &value); + if(err) { + printf("snd_mixer_selem_get_capture_dB: %s, %d\n", + snd_strerror(err), err); + return fERROR; + } + } else if(isPlayback()) { + err = snd_mixer_selem_get_playback_dB(elem, chanId(channel), &value); + if(err) { + printf("snd_mixer_selem_get_playback_dB: %s, %d\n", + snd_strerror(err), err); + return fERROR; + } + } else { + return fERROR; + } + + return (float)value / 100.0; +} + +Mixer::range_t Mixer::range() +{ + Mixer::range_t range; + + int err; + long int lvl_min, lvl_max; + + if(isCapture()) { + err = snd_mixer_selem_get_capture_dB_range(elem, &lvl_min, &lvl_max); + + if(err) { + printf("snd_mixer_selem_get_capture_dB_range: %s, %d\n", + snd_strerror(err), err); + } + } else if(isPlayback()) { + err = snd_mixer_selem_get_playback_dB_range(elem, &lvl_min, &lvl_max); + + if(err) { + printf("snd_mixer_selem_get_playback_dB_range: %s, %d\n", + snd_strerror(err), err); + } + } else { + range.min = fERROR; + range.max = fERROR; + return range; + } + + range.min = (float)lvl_min / 100.0; + range.max = (float)lvl_max / 100.0; + + return range; +} + +void Mixer::setCapture(bool capture) +{ + if(!isCapture()) return; + int c = capture?1:0; + int err; + err = snd_mixer_selem_set_capture_switch_all(elem, c); + if(err) { + printf("snd_mixer_selem_set_capture_switch_all: %s, %d\n", + snd_strerror(err), err); + } +} + +bool Mixer::capture() +{ + if(!isCapture()) return false; + + int err; + + int num = numberOfChannels(); + for(int idx = 0; idx < num; idx++) { + int value; + err = snd_mixer_selem_get_capture_switch(elem, chanId(idx), &value); + if(err) { + printf(" snd_mixer_selem_get_capture_switch: %s, %d\n", + snd_strerror(err), err); + return false; + } + if(value == 0) return false; + } + + return true; +} diff --git a/src/mixer.h b/src/mixer.h new file mode 100644 index 0000000..0adcdda --- /dev/null +++ b/src/mixer.h @@ -0,0 +1,125 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * mixer.h + * + * Tue Sep 23 14:38:54 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __LIBAUDIOIO_MIXER_H__ +#define __LIBAUDIOIO_MIXER_H__ + +// Make API behave like ALSA 1.x.x with ALSA 0.9.x +#define ALSA_PCM_NEW_HW_PARAMS_API + +#include + +#include +#include +#include + +/** + * A Mixer object represents a single group of channels, for example the two + * channels of a stereo mixer. + */ +class Mixer { +public: + /** + * Mixer constructor is not to be called "manually" is is invoked by + * Device::getMixer() + */ + Mixer(snd_mixer_t *handle, snd_mixer_elem_t *elem); + ~Mixer(); + + /** + * Get number of channels in this mixer 'strip' + */ + int numberOfChannels(); + + /** + * Return true if this mixer is a capture device, false otherwise. + */ + bool isCapture(); + + /** + * Return true if this mixer is a playback device, false otherwise. + */ + bool isPlayback(); + + /** + * Return true if this mixer is a enum, false otherwise. + */ + bool isEnum(); + + /** + * For capture channels only! + * Set arm for recording flag on all mixer channels. + */ + void setCapture(bool capture); + + /** + * Return true if all channels are armed for recording. + * Return false if not a capture channels strip, or if at least one channel + * is not armed. + */ + bool capture(); + + /** + * Set enum value. + */ + void setEnumValue(std::string value); + + /** + * Get enum value. + */ + std::string enumValue(); + + /** + * Get list of possible enum values. + */ + std::vector enumValues(); + + /** + * Get/set mixer volume level in dB. + * On error (no such channel) -INF is returned. + */ + int setLevel(int channel, float level); + // 0 on success, 1 on error. + float level(int channel); + + typedef struct { + float min; + float max; + } range_t; + /** + * Poll the valid volume range of this mixer in dB. + */ + range_t range(); + +private: + snd_mixer_selem_channel_id_t chanId(int idx); + + snd_mixer_t *handle; + snd_mixer_elem_t *elem; +}; + +#endif/*__LIBAUDIOIO_MIXER_H__*/ diff --git a/src/sink.cc b/src/sink.cc new file mode 100644 index 0000000..4f7cd0f --- /dev/null +++ b/src/sink.cc @@ -0,0 +1,79 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * sink.cc + * + * Thu Sep 25 10:35:42 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "sink.h" +Sink::Sink(snd_pcm_t *handle, unsigned int _samplerate, + unsigned int _channels, snd_pcm_uframes_t _frames) +{ + this->handle = handle; + this->_samplerate = _samplerate; + this->_channels = _channels; + this->_frames = _frames; +} + +Sink::~Sink() +{ + snd_pcm_drain(handle); + snd_pcm_close(handle); +} + +int Sink::writeSamples(const char *pcm, size_t size) +{ + int rc; + + /* + if(size < frames * sizeof(short) * channels) { + throw PcmBufferTooSmall(); + } + */ + + rc = snd_pcm_writei(handle, pcm, size / sizeof(short)); + if(rc == -EPIPE) { + // EPIPE means overrun + //snd_pcm_prepare(handle); + return -2; + } else if (rc < 0) { + return -1; // Read Error + } + + return rc * sizeof(short) /* * channels */ ; +} + +unsigned int Sink::samplerate() +{ + return this->_samplerate; +} + +unsigned int Sink::channels() +{ + return this->_channels; +} + +snd_pcm_uframes_t Sink::frames() +{ + return this->_frames; +} diff --git a/src/sink.h b/src/sink.h new file mode 100644 index 0000000..3ea0bf0 --- /dev/null +++ b/src/sink.h @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * sink.h + * + * Thu Sep 25 10:35:42 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __LIBAUDIOIO_SINK_H__ +#define __LIBAUDIOIO_SINK_H__ + +// Make API behave like ALSA 1.x.x with ALSA 0.9.x +#define ALSA_PCM_NEW_HW_PARAMS_API + +#include + +class Sink { +public: + Sink(snd_pcm_t *handle, unsigned int samplerate, + unsigned int channels, snd_pcm_uframes_t frames); + ~Sink(); + + int writeSamples(const char *pcm, size_t size); + + unsigned int samplerate(); + unsigned int channels(); + snd_pcm_uframes_t frames(); + +private: + snd_pcm_t *handle; + + unsigned int _samplerate; + unsigned int _channels; + snd_pcm_uframes_t _frames; +}; + +#endif/*__LIBAUDIOIO_SINK_H__*/ diff --git a/src/source.cc b/src/source.cc new file mode 100644 index 0000000..187a28e --- /dev/null +++ b/src/source.cc @@ -0,0 +1,81 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * source.cc + * + * Thu Sep 25 10:35:39 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "source.h" + +Source::Source(snd_pcm_t *handle, unsigned int _samplerate, + unsigned int _channels, snd_pcm_uframes_t _frames) +{ + this->handle = handle; + this->_samplerate = _samplerate; + this->_channels = _channels; + this->_frames = _frames; +} + +Source::~Source() +{ + snd_pcm_drain(handle); + snd_pcm_close(handle); +} + +int Source::readSamples(char *pcm, size_t maxsize) +{ + int rc; + + /* + if(size < frames * sizeof(short) * channels) { + throw PcmBufferTooSmall(); + } + */ + + rc = snd_pcm_readi(handle, pcm, maxsize / sizeof(short)); + if(rc == -EPIPE) { + // EPIPE means overrun + snd_pcm_prepare(handle); + return -2; + } else if (rc < 0) { + return -1; // Read Error + } + + return rc * sizeof(short) /* * channels */; +} + +unsigned int Source::samplerate() +{ + return this->_samplerate; +} + +unsigned int Source::channels() +{ + return this->_channels; +} + +snd_pcm_uframes_t Source::frames() +{ + return this->_frames; +} + diff --git a/src/source.h b/src/source.h new file mode 100644 index 0000000..d7e2d76 --- /dev/null +++ b/src/source.h @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * source.h + * + * Thu Sep 25 10:35:39 CEST 2014 + * Copyright 2014 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of LibAudioIO. + * + * LibAudioIO is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * LibAudioIO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with LibAudioIO; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __LIBAUDIOIO_SOURCE_H__ +#define __LIBAUDIOIO_SOURCE_H__ + +// Make API behave like ALSA 1.x.x with ALSA 0.9.x +#define ALSA_PCM_NEW_HW_PARAMS_API + +#include + +class Source { +public: + Source(snd_pcm_t *handle, unsigned int samplerate, + unsigned int channels, snd_pcm_uframes_t frames); + ~Source(); + + int readSamples(char *pcm, size_t maxsize); + + unsigned int samplerate(); + unsigned int channels(); + snd_pcm_uframes_t frames(); + +private: + snd_pcm_t *handle; + + unsigned int _samplerate; + unsigned int _channels; + snd_pcm_uframes_t _frames; +}; + +#endif/*__LIBAUDIOIO_SOURCE_H__*/ diff --git a/src/test_audio.h b/src/test_audio.h deleted file mode 100644 index 68dac67..0000000 --- a/src/test_audio.h +++ /dev/null @@ -1,112 +0,0 @@ -/* -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * File: test_audio.h - * This file belongs to the Bifrost project. - * [FILL IN DESCRIPTION HERE] - * Date: Wed Feb 3 08:04:33 CET 2010 - * Author: Bent Bisballe Nyeng - * Copyright: 2010 - * Email: deva@aasimon.org - ****************************************************************************/ -#ifndef __BIFROST_TEST_AUDIO_H__ -#define __BIFROST_TEST_AUDIO_H__ - -#include -#include -#include - -#ifdef VERBOSE -#include -#endif - -static inline size_t getBufferSize(size_t samples, size_t channels) -{ - return samples * channels * sizeof(short); -} - -static inline char *getBuffer(size_t samples, size_t channels) -{ - return (char*)calloc(getBufferSize(samples, channels), 1); -} - -static inline char *getSineBuffer(size_t samples, size_t channels, - size_t srate, size_t freq) -{ - short *s = (short*)getBuffer(samples, channels); - for(size_t i = 0; i < samples; i++) { - double x = (double)i / (double)srate; - double val = sin(x * 2 * M_PI * freq); - - for(size_t c = 0; c < channels; c++) { - s[(i * channels) + c] = val * 32500; - } - - } - return (char*)s; -} - -static inline char *getNoiseBuffer(size_t samples, size_t channels) -{ - srand(time(NULL)); - short *s = (short*)getBuffer(samples, channels); - for(size_t i = 0; i < samples; i++) { - for(size_t c = 0; c < channels; c++) { - s[(i * channels) + c] = (rand() % 65000) - 32500; - } - } - return (char*)s; -} - -static inline void normaliseBuffer(size_t samples, size_t channels, char *buf) -{ - int max = 0; - - short *s = (short*)buf; - for(size_t i = 0; i < samples; i++) { - for(size_t c = 0; c < channels; c++) { - if(max < abs(s[(i * channels) + c])) max = abs(s[(i * channels) + c]); - } - } - - for(size_t i = 0; i < samples; i++) { - for(size_t c = 0; c < channels; c++) { - s[(i * channels) + c] *= 32500/max; - } - } - -} - -static inline double compareBuffers(size_t samples, size_t channels, char *buf1, char *buf2) -{ - double diff = 0.0; - -#ifdef NORM - normaliseBuffer(samples, channels, buf1); - normaliseBuffer(samples, channels, buf2); -#endif - - short *s1 = (short*)buf1; - short *s2 = (short*)buf2; - - for(size_t i = 0; i < samples; i++) { - for(size_t c = 0; c < channels; c++) { - diff += (double)abs(s1[(i * channels) + c] - s2[(i * channels) + c]) / (double)(samples*channels); -#ifdef VERBOSE - if(c == 0) fprintf(stderr, "%d\t%d\t~%d\t: %f\n", - s1[(i * channels) + c], s2[(i * channels) + c], - s1[(i * channels) + c] - s2[(i * channels) + c], - diff); -#endif - - } - } - -#ifdef VERBOSE - fprintf(stderr, "Diff: %f\n", diff); -#endif - - return diff; -} - - -#endif/*__BIFROST_TEST_AUDIO_H__*/ -- cgit v1.2.3