Debian Codegraph @

test/Loop.cpp

raw
1 /*
2 Copyright (C) 2011, Michael Pruett. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 3. The name of the author may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 Test that loops can be written to and read from an audio file.
32 */
33
34 #include <audiofile.h>
35 #include <fcntl.h>
36 #include <gtest/gtest.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <string>
41
42 #include "TestUtilities.h"
43
44 TEST(Loop, AIFF)
45 {
46 std::string testFileName;
47 ASSERT_TRUE(createTemporaryFile("Loop", &testFileName));
48
49 AFfilesetup setup = afNewFileSetup();
50 afInitFileFormat(setup, AF_FILE_AIFF);
51 afInitChannels(setup, AF_DEFAULT_TRACK, 1);
52 afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
53
54 int markerIDs[] = { 1, 2, 3, 4 };
55 const int numMarkers = sizeof (markerIDs) / sizeof (int);
56 const char * const markerNames[numMarkers] =
57 {
58 "sustain loop start",
59 "sustain loop end",
60 "release loop start",
61 "release loop end"
62 };
63 const int markerPositions[] = { 0, 2, 4, 5 };
64 afInitMarkIDs(setup, AF_DEFAULT_TRACK, markerIDs, 4);
65 for (int i=0; i<numMarkers; i++)
66 afInitMarkName(setup, AF_DEFAULT_TRACK, markerIDs[i], markerNames[i]);
67
68 int loopIDs[] = {1, 2};
69 afInitLoopIDs(setup, AF_DEFAULT_INST, loopIDs, 2);
70
71 AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup);
72 ASSERT_TRUE(file) << "Could not open test file for writing";
73 afFreeFileSetup(setup);
74
75 const int frameCount = 5;
76 int16_t frames[frameCount] = {0};
77
78 EXPECT_EQ(afWriteFrames(file, AF_DEFAULT_TRACK, frames, frameCount),
79 frameCount);
80
81 for (int i=0; i<numMarkers; i++)
82 afSetMarkPosition(file, AF_DEFAULT_TRACK, markerIDs[i], markerPositions[i]);
83
84 afSetLoopStart(file, AF_DEFAULT_INST, 1, 1);
85 afSetLoopEnd(file, AF_DEFAULT_INST, 1, 2);
86 afSetLoopStart(file, AF_DEFAULT_INST, 2, 3);
87 afSetLoopEnd(file, AF_DEFAULT_INST, 2, 4);
88
89 afCloseFile(file);
90
91 file = afOpenFile(testFileName.c_str(), "r", NULL);
92 ASSERT_TRUE(file) << "Could not open test file for reading";
93
94 ASSERT_EQ(afGetLoopIDs(file, AF_DEFAULT_INST, NULL), 2);
95 int readLoopIDs[2];
96 afGetLoopIDs(file, AF_DEFAULT_INST, readLoopIDs);
97 for (int i=0; i<2; i++)
98 ASSERT_EQ(loopIDs[i], readLoopIDs[i]);
99
100 ASSERT_EQ(afGetMarkIDs(file, AF_DEFAULT_TRACK, NULL), numMarkers);
101 int readMarkerIDs[numMarkers];
102 afGetMarkIDs(file, AF_DEFAULT_TRACK, readMarkerIDs);
103 for (int i=0; i<numMarkers; i++)
104 ASSERT_EQ(markerIDs[i], readMarkerIDs[i]);
105
106 for (int i=0; i<numMarkers; i++)
107 ASSERT_TRUE(!strcmp(afGetMarkName(file, AF_DEFAULT_TRACK, markerIDs[i]),
108 markerNames[i]));
109
110 for (int i=0; i<numMarkers; i++)
111 ASSERT_EQ(afGetMarkPosition(file, AF_DEFAULT_TRACK, markerIDs[i]),
112 markerPositions[i]);
113
114 ASSERT_EQ(afGetLoopStart(file, AF_DEFAULT_INST, 1), 1);
115 ASSERT_EQ(afGetLoopEnd(file, AF_DEFAULT_INST, 1), 2);
116 ASSERT_EQ(afGetLoopStart(file, AF_DEFAULT_INST, 2), 3);
117 ASSERT_EQ(afGetLoopEnd(file, AF_DEFAULT_INST, 2), 4);
118
119 ASSERT_EQ(afGetLoopTrack(file, AF_DEFAULT_INST, 1), AF_DEFAULT_TRACK);
120 ASSERT_EQ(afGetLoopTrack(file, AF_DEFAULT_INST, 2), AF_DEFAULT_TRACK);
121
122 ASSERT_EQ(afGetLoopStartFrame(file, AF_DEFAULT_INST, 1),
123 markerPositions[0]);
124 ASSERT_EQ(afGetLoopEndFrame(file, AF_DEFAULT_INST, 1),
125 markerPositions[1]);
126 ASSERT_EQ(afGetLoopStartFrame(file, AF_DEFAULT_INST, 2),
127 markerPositions[2]);
128 ASSERT_EQ(afGetLoopEndFrame(file, AF_DEFAULT_INST, 2),
129 markerPositions[3]);
130
131 afCloseFile(file);
132
133 ASSERT_EQ(::unlink(testFileName.c_str()), 0);
134 }
135
136 int main(int argc, char **argv)
137 {
138 ::testing::InitGoogleTest(&argc, argv);
139 return RUN_ALL_TESTS();
140 }