summaryrefslogtreecommitdiff
path: root/src/aioloop.cc
blob: ad70c72edf7ce839538c328755e8282f4a34ab82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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;
}