summaryrefslogtreecommitdiff
path: root/src/aioloop.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2014-09-25 17:08:44 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2014-09-25 17:08:44 +0200
commit4f6b15ea26b5fa09f300f67e4ce1057c2b39a3aa (patch)
tree45d7bb4a9a90ef603b5a7a65a4d4d4309a7a1538 /src/aioloop.cc
parent0602763044f0f0f9163f2a888627213347d3dbb7 (diff)
New API, new name, new version.
Diffstat (limited to 'src/aioloop.cc')
-rw-r--r--src/aioloop.cc191
1 files changed, 191 insertions, 0 deletions
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 <pthread.h>
+#include <semaphore.h>
+
+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;
+}
+