summaryrefslogtreecommitdiffstats
path: root/flow/audioionull.cc
blob: 1570844e43e0b596f39a9fac695c87ed06a4f360 (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
    /*

    Copyright (C) 2001 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <algorithm>
#include <cstring>

#include "debug.h"
#include "audioio.h"
#include "audiosubsys.h"
#include "iomanager.h"
#include "dispatcher.h"

namespace Arts {

class AudioIONull : public AudioIO, public TimeNotify {
protected:
	timeval start;
	double samplesRead, samplesWritten, bytesPerSec;

public:
	AudioIONull();

	void notifyTime();

	void setParam(AudioParam param, int& value);
	int getParam(AudioParam param);

	bool open();
	void close();
	int read(void *buffer, int size);
	int write(void *buffer, int size);
};

REGISTER_AUDIO_IO(AudioIONull,"null","No Audio Input/Output");
}

using namespace std;
using namespace Arts;

AudioIONull::AudioIONull()
{
	/*
	 * default parameters
	 */
	param(samplingRate) = 44100;
	paramStr(deviceName) = "null";
	param(fragmentSize) = 1024;
	param(fragmentCount) = 7;
	param(channels) = 2;
	param(direction) = 2;
}

bool AudioIONull::open()
{
	int& _channels = param(channels);
	int& _fragmentSize = param(fragmentSize);
	int& _fragmentCount = param(fragmentCount);
	int& _samplingRate = param(samplingRate);
	int& _format = param(format);
	_format = 16;

	/*
	 * don't allow unreasonable large fragmentSize/Count combinations,
	 * because "real" hardware also doesn't
	 */

	if(_fragmentSize > 1024*128) _fragmentSize = 1024*128;

	while(_fragmentSize * _fragmentCount > 1024*128)
		_fragmentCount--;

	Dispatcher::the()->ioManager()->addTimer(10, this);

	samplesRead = samplesWritten = 0.0;
	bytesPerSec = _channels * 2 * _samplingRate;
	gettimeofday(&start,0);

	return true;
}

void AudioIONull::close()
{
	Dispatcher::the()->ioManager()->removeTimer(this);
}

void AudioIONull::notifyTime()
{
	int& _direction = param(direction);
	int& _fragmentSize = param(fragmentSize);

	for(;;)
	{
		int todo = 0;

		if((_direction & directionRead) && getParam(canRead) >= _fragmentSize)
			todo |= AudioSubSystem::ioRead;

		if((_direction & directionWrite) && getParam(canWrite) >= _fragmentSize)
			todo |= AudioSubSystem::ioWrite;

		if(!todo)
			return;

		AudioSubSystem::the()->handleIO(todo);
	}
}

void AudioIONull::setParam(AudioParam p, int& value)
{
	param(p) = value;
}

int AudioIONull::getParam(AudioParam p)
{
	timeval now;
	double delta;
	int bytes;

	switch(p)
	{
		case canRead:
		case canWrite:
				gettimeofday(&now,0);
				delta = (double)now.tv_sec + (double)now.tv_usec/1000000.0;
				delta -= (double)start.tv_sec + (double)start.tv_usec/1000000.0;
				bytes = (int)( (delta * bytesPerSec)
								 -  ((p == canRead)?samplesRead:samplesWritten)
								  );
				return bytes;
			break;

		default:
				return param(p);
			break;
	}
}

int AudioIONull::read(void *buffer, int size)
{
	samplesRead += size;
	memset(buffer, 0, size);
	return size;
}

int AudioIONull::write(void *, int size)
{
	samplesWritten += size;
	return size;
}