/* -*- 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; }