summaryrefslogtreecommitdiff
path: root/src/audiocacheeventhandler.cc
blob: c6392423a9b6e4ebdd01c95e8e7cef938b32fc24 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            audiocacheeventhandler.cc
 *
 *  Sun Jan  3 19:57:55 CET 2016
 *  Copyright 2016 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 3 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 "audiocacheeventhandler.h"

#include <assert.h>

#include <hugin.hpp>

#include "audiocachefile.h"
#include "audiocache.h"
#include "audiocacheidmanager.h"

enum class EventType {
	LoadNext,
	Close,
};

class CacheEvent {
public:
	EventType event_type;

	// For close event:
	cacheid_t id;

	// For load next event:
	size_t pos;
	AudioCacheFile* afile;
	CacheChannels channels;
};

AudioCacheEventHandler::AudioCacheEventHandler(AudioCacheIDManager& id_manager)
	: id_manager(id_manager)
{
}

AudioCacheEventHandler::~AudioCacheEventHandler()
{
	// Close all ids already enqueued to be closed.
	clearEvents();

	auto active_ids = id_manager.getActiveIDs();
	for(auto id : active_ids)
	{
		handleCloseCache(id);
	}
}

void AudioCacheEventHandler::start()
{
	if(running)
	{
		return;
	}

	running = true;
	run();
	sem_run.wait();
}

void AudioCacheEventHandler::stop()
{
	if(!running)
	{
		return;
	}

	running = false;

	sem.post();
	wait_stop();
}

void AudioCacheEventHandler::setThreaded(bool threaded)
{
	if(this->threaded == threaded)
	{
		return;
	}

	if(threaded && !running)
	{
		start();
	}
	else if(!threaded && running)
	{
		stop();
	}

	this->threaded = threaded;
}

bool AudioCacheEventHandler::isThreaded() const
{
	return threaded;
}

void AudioCacheEventHandler::lock()
{
	mutex.lock();
}

void AudioCacheEventHandler::unlock()
{
	mutex.unlock();
}

void AudioCacheEventHandler::pushLoadNextEvent(AudioCacheFile* afile,
                                               size_t channel,
                                               size_t pos, sample_t* buffer,
                                               volatile bool* ready)
{
	CacheEvent cache_event;
	cache_event.event_type = EventType::LoadNext;
	cache_event.pos = pos;
	cache_event.afile = afile;

	CacheChannel c;
	c.channel = channel;
	c.samples = buffer;

	*ready = false;
	c.ready = ready;

	cache_event.channels.insert(cache_event.channels.end(), c);

	pushEvent(cache_event);
}

void AudioCacheEventHandler::pushCloseEvent(cacheid_t id)
{
	CacheEvent cache_event;
	cache_event.event_type = EventType::Close;
	cache_event.id = id;

	pushEvent(cache_event);
}

void AudioCacheEventHandler::setChunkSize(size_t chunksize)
{
	DEBUG(cache, "%s\n", __PRETTY_FUNCTION__);

	// We should already locked when this method is called.
	//assert(!mutex.try_lock());

	if(this->chunksize == chunksize)
	{
		return;
	}

	DEBUG(cache, "setChunkSize 1\n");

	// Remove all events from event queue.
	clearEvents();

	DEBUG(cache, "setChunkSize 2\n");

	// Skip all active cacheids and make their buffers point at nodata.
	id_manager.disableActive();

	DEBUG(cache, "setChunkSize 3\n");

	this->chunksize = chunksize;
}

size_t AudioCacheEventHandler::chunkSize()
{
	return chunksize;
}

AudioCacheFile& AudioCacheEventHandler::openFile(const std::string& filename)
{
	std::lock_guard<std::mutex> lock(mutex);
	return files.getFile(filename);
}

void AudioCacheEventHandler::clearEvents()
{
	// Iterate all events ignoring load events and handling close events.
	for(auto& event : eventqueue)
	{
		if(event.event_type == EventType::Close)
		{
			handleCloseCache(event.id); // This method does not lock.
		}
	}

	eventqueue.clear();
}

void AudioCacheEventHandler::handleLoadNextEvent(CacheEvent& cache_event)
{
	cache_event.afile->readChunk(cache_event.channels, cache_event.pos,
	                             chunksize);
}

void AudioCacheEventHandler::handleCloseEvent(CacheEvent& cache_event)
{
	std::lock_guard<std::mutex> lock(mutex);
	handleCloseCache(cache_event.id);
}

void AudioCacheEventHandler::handleCloseCache(cacheid_t id)
{
	auto& cache = id_manager.getCache(id);

	files.releaseFile(cache.afile->getFilename());

	delete[] cache.front;
	delete[] cache.back;

	id_manager.releaseID(id);
}

void AudioCacheEventHandler::handleEvent(CacheEvent& cache_event)
{
	switch(cache_event.event_type)
	{
	case EventType::LoadNext:
		handleLoadNextEvent(cache_event);
		break;
	case EventType::Close:
		handleCloseEvent(cache_event);
		break;
	}
}

void AudioCacheEventHandler::thread_main()
{
	sem_run.post(); // Signal that the thread has been started

	while(running)
	{
		sem.wait();

		mutex.lock();
		if(eventqueue.empty())
		{
			mutex.unlock();
			continue;
		}

		CacheEvent cache_event = eventqueue.front();
		eventqueue.pop_front();
		mutex.unlock();

		// TODO: Skip event if cache_event.pos < cache.pos
		//if(!cache_event.active)
		//{
		//	continue;
		//}

		handleEvent(cache_event);
	}
}

void AudioCacheEventHandler::pushEvent(CacheEvent& cache_event)
{
	if(!threaded)
	{
		handleEvent(cache_event);
		return;
	}

	{
		std::lock_guard<std::mutex> lock(mutex);

		bool found = false;

		if(cache_event.event_type == EventType::LoadNext)
		{
			for(auto& queued_event : eventqueue)
			{
				if((queued_event.event_type == EventType::LoadNext) &&
				   (cache_event.afile->getFilename() ==
				    queued_event.afile->getFilename()) &&
				   (cache_event.pos == queued_event.pos))
				{
					// Append channel and buffer to the existing event.
					queued_event.channels.insert(queued_event.channels.end(),
					                             cache_event.channels.begin(),
					                             cache_event.channels.end());
					found = true;
					break;
				}
			}
		}

		if(!found)
		{
			// The event was not already on the list, create a new one.
			eventqueue.push_back(cache_event);
		}
	}

	sem.post();
}