summaryrefslogtreecommitdiff
path: root/test/audiocachetest.cc
blob: 823e9c46aded847622820a3cf61431df41215289 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            cachemanagertest.cc
 *
 *  Sun Apr 19 10:15:59 CEST 2015
 *  Copyright 2015 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 Lesser 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser 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 <uunit.h>

#include <thread>
#include <chrono>

#include <audiofile.h>
#include <audiocache.h>
#include <settings.h>

#include "drumkit_creator.h"

#define FRAMESIZE 64

class AudioCacheTest
	: public uUnit
{
public:
	AudioCacheTest()
	{
		uUNIT_TEST(AudioCacheTest::singleChannelNonThreaded);
		uUNIT_TEST(AudioCacheTest::singleChannelThreaded);
		uUNIT_TEST(AudioCacheTest::multiChannelNonThreaded);
		uUNIT_TEST(AudioCacheTest::multiChannelThreaded);
	}

	DrumkitCreator drumkit_creator;

	//! Test runner.
	//! \param filename The name of the file to read.
	//! \param channel The channel number to do comparison on.
	//! \param thread Control if this test is running in threaded mode or not.
	//! \param framesize The initial framesize to use.
	void testHelper(const char* filename, int channel, bool threaded,
	                int framesize, int num_channels)
	{
		// Reference file:
		AudioFile audio_file_ref(filename, channel);
		printf("audio_file_ref.load\n");
		audio_file_ref.load(nullptr);

		// Input file:
		AudioFile audio_file(filename, channel);
		printf("audio_file.load\n");
		audio_file.load(nullptr, 4096);

		Settings settings;
		AudioCache audio_cache(settings);
		printf("audio_cache.init\n");
		audio_cache.init(100);
		audio_cache.setAsyncMode(threaded);

		// Set initial (upper limit) framesize
		audio_cache.setFrameSize(framesize);
		audio_cache.updateChunkSize(num_channels);

		cacheid_t id;

		for(size_t initial_samples_needed = 0;
		    initial_samples_needed < (size_t)(framesize + 1);
		    ++initial_samples_needed)
		{

			printf("open: initial_samples_needed: %d\n", (int)initial_samples_needed);
			sample_t *samples =
				audio_cache.open(audio_file, initial_samples_needed, channel, id);
			size_t size = initial_samples_needed;
			size_t offset = 0;

			// Test pre cache:
			for(size_t i = 0; i < size; ++i)
			{
				uUNIT_ASSERT_EQUAL(audio_file_ref.data[offset], samples[i]);
				++offset;
			}

			// Test the rest
			while(offset < audio_file_ref.size)
			{
				if(threaded)
				{
					// Wait until we are finished reading
					int timeout = 1000;
					while(!audio_cache.isReady(id))
					{
						std::this_thread::sleep_for(std::chrono::milliseconds(1));
						if(--timeout == 0)
						{
							uUNIT_ASSERT(false); // timeout
						}
					}
				}

				size = framesize;
				samples = audio_cache.next(id, size);

				uUNIT_ASSERT_EQUAL(std::size_t(0), settings.number_of_underruns.load());

				for(size_t i = 0; (i < size) && (offset < audio_file_ref.size); ++i)
				{
					if(audio_file_ref.data[offset] != samples[i])
					{
						printf("-----> offset: %d, size: %d, diff: %d,"
						       " i: %d, size: %d, block-diff: %d\n",
						       (int)offset, (int)audio_file_ref.size,
						       (int)(audio_file_ref.size - offset),
						       (int)i, (int)size, (int)(size - i));
					}
					uUNIT_ASSERT_EQUAL(audio_file_ref.data[offset], samples[i]);
					++offset;
				}
			}

			audio_cache.close(id);
		}

		printf("done\n");
	}

	void singleChannelNonThreaded()
	{
		printf("\nsinglechannel_nonthreaded()\n");

		auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav");

		// Conduct test
		int channel = 0;
		bool threaded = false;
		int num_channels = 1;
		testHelper(filename.c_str(), channel, threaded, FRAMESIZE, num_channels);
	}

	void singleChannelThreaded()
	{
		printf("\nsinglechannel_threaded()\n");

		auto filename = drumkit_creator.createSingleChannelWav("single_channel.wav");

		// Conduct test
		int channel = 0;
		bool threaded = true;
		int num_channels = 1;
		testHelper(filename.c_str(), channel, threaded, FRAMESIZE, num_channels);
	}

	void multiChannelNonThreaded()
	{
		printf("\nmultichannel_nonthreaded()\n");

		auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav");

		// Conduct test
		int channel = 0;
		bool threaded = false;
		int num_channels = 13;
		testHelper(filename.c_str(), channel, threaded, FRAMESIZE, num_channels);
		++channel;
		testHelper(filename.c_str(), channel, threaded, FRAMESIZE, num_channels);
	}

	void multiChannelThreaded()
	{
		printf("\nmultichannel_threaded()\n");

		auto filename = drumkit_creator.createMultiChannelWav("multi_channel.wav");

		// Conduct test
		int channel = 0;
		bool threaded = true;
		int num_channels = 13;
		testHelper(filename.c_str(), channel, threaded, FRAMESIZE, num_channels);
		++channel;
		testHelper(filename.c_str(), channel, threaded, FRAMESIZE, num_channels);
	}

};

// Registers the fixture into the 'registry'
static AudioCacheTest test;