File: audioBuffer.cpp

package info (click to toggle)
giada 0.15.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,700 kB
  • sloc: cpp: 30,043; sh: 1,056; makefile: 422; ansic: 1
file content (100 lines) | stat: -rw-r--r-- 2,216 bytes parent folder | download
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
#include <memory>
#include "../src/core/audioBuffer.h"
#include <catch.hpp>


TEST_CASE("AudioBuffer")
{
	using namespace giada::m;

	static const int BUFFER_SIZE = 4096;

	/* Each SECTION the TEST_CASE is executed from the start. Any code between 
	this comment and the first SECTION macro is exectuted before each SECTION. */

	AudioBuffer buffer;
	buffer.alloc(BUFFER_SIZE, 2);
	
	SECTION("test allocation")
	{
		SECTION("test mono")
		{
			buffer.alloc(BUFFER_SIZE, 1);
			REQUIRE(buffer.countFrames() == BUFFER_SIZE);
			REQUIRE(buffer.countSamples() == BUFFER_SIZE);
			REQUIRE(buffer.countChannels() == 1);
		}		

		SECTION("test stereo")
		{
			REQUIRE(buffer.countFrames() == BUFFER_SIZE);
			REQUIRE(buffer.countSamples() == BUFFER_SIZE * 2);
			REQUIRE(buffer.countChannels() == 2);
		}		

		SECTION("test odd channels count")
		{
			buffer.alloc(BUFFER_SIZE, 7);
			REQUIRE(buffer.countFrames() == BUFFER_SIZE);
			REQUIRE(buffer.countSamples() == BUFFER_SIZE * 7);
			REQUIRE(buffer.countChannels() == 7);
		}

		buffer.free();

		REQUIRE(buffer[0] == nullptr);
		REQUIRE(buffer.countFrames() == 0);
		REQUIRE(buffer.countSamples() == 0);
		REQUIRE(buffer.countChannels() == 0);
	}

	SECTION("test clear all")
	{	
		buffer.clear();
		for (int i=0; i<buffer.countFrames(); i++)
			for (int k=0; k<buffer.countChannels(); k++)
				REQUIRE(buffer[i][k] == 0.0f);
		buffer.free();
	}

	SECTION("test clear range")
	{	
		for (int i=0; i<buffer.countFrames(); i++)
			for (int k=0; k<buffer.countChannels(); k++)
					buffer[i][k] = 1.0f;

		buffer.clear(5, 6);

		for (int k=0; k<buffer.countChannels(); k++)
			REQUIRE(buffer[5][k] == 0.0f);

		buffer.free();
	}

	SECTION("test copy")
	{	
		int size = BUFFER_SIZE * 2;
		float* data = new float[size];
		for (int i=0; i<size; i++)
			data[i] = (float) i;

		SECTION("test full copy")
		{	
			buffer.copyData(data, BUFFER_SIZE);

			REQUIRE(buffer[0][0]    == 0.0f);
			REQUIRE(buffer[16][0]   == 32.0f);
			REQUIRE(buffer[32][0]   == 64.0f);
			REQUIRE(buffer[1024][0] == 2048.0f);
		}

		SECTION("test copy frame")
		{	
			buffer.copyFrame(16, &data[32]);
			REQUIRE(buffer[16][0] == 32.0f);
			REQUIRE(buffer[16][1] == 33.0f);
		}

		delete[] data;
	}
}