From 95ab4d90ad6ee7644fd9a0b0d70a59e6c6b2dbda Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 30 Jul 2012 22:05:11 +0200 Subject: AudioIn/Out classes based on Alsa. --- src/Makefile.am | 12 ++-- src/audioin.cc | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/audioin.h | 81 ++++++++++++++++++++++ src/audioout.cc | 87 ++++++++++++++++++++++++ src/audioout.h | 52 ++++++++++++++ src/crosscomposer.cc | 18 ++++- 6 files changed, 434 insertions(+), 5 deletions(-) create mode 100644 src/audioin.cc create mode 100644 src/audioin.h create mode 100644 src/audioout.cc create mode 100644 src/audioout.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index d8c059e..c544e46 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,13 +2,17 @@ SUBDIRS = bin_PROGRAMS = crosscomposer -crosscomposer_LDADD = -crosscomposer_CXXFLAGS = +crosscomposer_LDADD = $(ALSA_LIBS) $(AO_LIBS) +crosscomposer_CXXFLAGS = $(ALSA_CFLAGS) $(AO_CFLAGS) crosscomposer_SOURCES = \ - crosscomposer.cc + crosscomposer.cc \ + audioin.cc \ + audioout.cc -EXTRA_DIST = +EXTRA_DIST = \ + audioin.h \ + audioout.h ################ # Test Section # diff --git a/src/audioin.cc b/src/audioin.cc new file mode 100644 index 0000000..3792ae2 --- /dev/null +++ b/src/audioin.cc @@ -0,0 +1,189 @@ +/* -*- 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" + +AudioIn::AudioIn(std::string device, std::string mixer_interface, + unsigned int srate, int ch) +{ + frames = 940; + + elem = NULL; + mixhnd = NULL; + + int rc; + + // Open PCM device for recording (capture). + rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_CAPTURE, 0); + 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_FLOAT); + 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 + unsigned int val = srate; + snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); + printf("srate: %d\n", val); + // if(val != srate) throw UnableToSetSampleRate(); + + // NOTE: Setting period size to 32 frames will force use of lowest possible value. + 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(); + + mixhnd = NULL; + /* + // + // 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 + snd_mixer_selem_set_enum_item(iselem, SND_MIXER_SCHN_MONO, 2); + } + */ +} + +AudioIn::~AudioIn() +{ + if(handle) { + snd_pcm_drain(handle); + snd_pcm_close(handle); + } + if(mixhnd) snd_mixer_close(mixhnd); +} + +size_t AudioIn::read(sample_t *buf, size_t size) +{ + int rc; + + if(size < frames * 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 * channels; +} + +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; +} diff --git a/src/audioin.h b/src/audioin.h new file mode 100644 index 0000000..99d2651 --- /dev/null +++ b/src/audioin.h @@ -0,0 +1,81 @@ +/* -*- 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__ + +// Use the newer ALSA API +#define ALSA_PCM_NEW_HW_PARAMS_API + +#include +#include +#include +#include +#include + +typedef float sample_t; + +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(); + + size_t read(sample_t *buf, size_t size); + + int set_level(unsigned int channel, float level); + +private: + 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; +}; + +#endif/*__LIBAUDIOIN_AUDIOIN_H__*/ diff --git a/src/audioout.cc b/src/audioout.cc new file mode 100644 index 0000000..0495a2c --- /dev/null +++ b/src/audioout.cc @@ -0,0 +1,87 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * audiooutputenginedummy.cc + * + * Sat Apr 30 21:12:02 CEST 2011 + * Copyright 2011 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo 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. + * + * DrumGizmo 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 DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "audioout.h" + + +#define T(x, msg) if(x < 0) { printf("%s failed: %s\n", msg, snd_strerror(x)); fflush(stdout); } + +#define BUFSZ 40960 + +AudioOut::AudioOut(std::string device, unsigned int srate, int channels) +{ + handle = NULL; + + snd_pcm_uframes_t frames = 940; + + int rc; + + rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0); + T(rc, "snd_pcm_open"); + + if(!handle) { + printf("No handle!\n"); + // return false; + } + + // Allocate a hardware parameters object. + snd_pcm_hw_params_alloca(¶ms); + // if(rc < 0) return false; + + // Fill it in with default values. + rc = snd_pcm_hw_params_any(handle, params); + T(rc, "snd_pcm_hw_params_any"); + + rc = snd_pcm_hw_params_set_access(handle, params, + SND_PCM_ACCESS_RW_INTERLEAVED); + T(rc, "snd_pcm_hw_params_set_access"); + + rc = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_FLOAT); + T(rc, "snd_pcm_hw_params_set_format"); + + rc = snd_pcm_hw_params_set_channels(handle, params, channels); + T(rc, "snd_pcm_hw_params_set_channels"); + + rc = snd_pcm_hw_params_set_rate_near(handle, params, &srate, 0); + T(rc, "snd_pcm_hw_params_set_rate_near"); + + rc = snd_pcm_hw_params_set_period_size_near(handle, params, &frames, 0); + T(rc, "snd_pcm_hw_params_set_period_size_near"); + + rc = snd_pcm_hw_params(handle, params); + T(rc, "snd_pcm_hw_params"); +} + +AudioOut::~AudioOut() +{ + if(handle) snd_pcm_close(handle); +} + +void AudioOut::write(sample_t *samples, size_t size) +{ + // Write the interleaved buffer to the soundcard + snd_pcm_writei(handle, samples, size); +} diff --git a/src/audioout.h b/src/audioout.h new file mode 100644 index 0000000..e020b0b --- /dev/null +++ b/src/audioout.h @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * player.h + * + * Sun May 17 08:19:51 CEST 2009 + * Copyright 2009 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of AudioChain. + * + * AudioChain 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. + * + * AudioChain 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 AudioChain; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __AUDIOCHAIN_PLAYER_H__ +#define __AUDIOCHAIN_PLAYER_H__ + +#include + +#include + +// Use the newer ALSA API +#define ALSA_PCM_NEW_HW_PARAMS_API + +#include + +typedef float sample_t; + +class AudioOut { +public: + AudioOut(std::string device, unsigned int srate, int channels); + ~AudioOut(); + void write(sample_t *samples, size_t size); + +private: + snd_pcm_t *handle; + snd_pcm_hw_params_t *params; +}; + +#endif/*__AUDIOCHAIN_PLAYER_H__*/ diff --git a/src/crosscomposer.cc b/src/crosscomposer.cc index 049d57f..c8992ff 100644 --- a/src/crosscomposer.cc +++ b/src/crosscomposer.cc @@ -25,8 +25,24 @@ * along with Cross Composer; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ +#include +#include "audioin.h" +#include "audioout.h" -int main() +int main(int argc, char *argv[]) { + AudioIn in("default", "Capture", 44101, 1); + AudioOut out("default", 44101, 1); + + sample_t samples[940]; + int i = 50; + while(i--) { + int sz = in.read(samples, sizeof(samples)); + out.write(samples, sz); + printf("."); fflush(stdout); + } + printf("!\n"); + + return 0; } -- cgit v1.2.3