1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "SonivoxTest"
19 #include <utils/Log.h>
20
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <fstream>
24
25 #include <libsonivox/eas.h>
26 #include <libsonivox/eas_reverb.h>
27
28 #include "SonivoxTestEnvironment.h"
29
30 #define OUTPUT_FILE "/data/local/tmp/output_midi.pcm"
31
32 // number of Sonivox output buffers to aggregate into one MediaBuffer
33 static constexpr uint32_t kNumBuffersToCombine = 4;
34 static constexpr uint32_t kSeekBeyondPlayTimeOffsetMs = 10;
35
36 static SonivoxTestEnvironment *gEnv = nullptr;
37 static int readAt(void *, void *, int, int);
38 static int getSize(void *);
39
40 class SonivoxTest : public ::testing::TestWithParam<tuple</*fileName*/ string,
41 /*audioPlayTimeMs*/ uint32_t,
42 /*totalChannels*/ uint32_t,
43 /*sampleRateHz*/ uint32_t>> {
44 public:
SonivoxTest()45 SonivoxTest()
46 : mFd(-1),
47 mInputFp(nullptr),
48 mEASDataHandle(nullptr),
49 mEASStreamHandle(nullptr),
50 mPCMBuffer(nullptr),
51 mAudioBuffer(nullptr),
52 mEASConfig(nullptr) {}
53
~SonivoxTest()54 ~SonivoxTest() {
55 if (mInputFp) fclose(mInputFp);
56 if (mFd >= 0) close(mFd);
57 if (mPCMBuffer) {
58 delete[] mPCMBuffer;
59 mPCMBuffer = nullptr;
60 }
61 if (mAudioBuffer) {
62 delete[] mAudioBuffer;
63 mAudioBuffer = nullptr;
64 }
65 if (gEnv->cleanUp()) remove(OUTPUT_FILE);
66 }
67
SetUp()68 virtual void SetUp() override {
69 tuple<string, uint32_t, uint32_t, uint32_t> params = GetParam();
70 mInputMediaFile = gEnv->getRes() + get<0>(params);
71 mAudioplayTimeMs = get<1>(params);
72 mTotalAudioChannels = get<2>(params);
73 mAudioSampleRate = get<3>(params);
74
75 // b/384791354: we're having presubmit failures that appear to be
76 // flaky non-population of the data to the device.
77 // To help diagnose, let's see what's in that directory. to see if it is
78 // non-population, incorrect permissions, or something novel+interesting.
79 {
80 string cmd;
81 // this will also show the directory itself....
82 cmd = "ls -la " + gEnv->getRes() + "/";
83 printf("Output from running %s\n", cmd.c_str());
84 system(cmd.c_str());
85 }
86
87 mFd = open(mInputMediaFile.c_str(), O_RDONLY | O_LARGEFILE);
88 ASSERT_GE(mFd, 0) << "Failed to get the file descriptor for file: " << mInputMediaFile;
89
90 struct stat buf;
91 int8_t err = stat(mInputMediaFile.c_str(), &buf);
92 ASSERT_EQ(err, 0) << "Failed to get information for file: " << mInputMediaFile;
93
94 mBase = 0;
95 mLength = buf.st_size;
96 mEasFile.handle = this;
97 mEasFile.readAt = ::readAt;
98 mEasFile.size = ::getSize;
99
100 EAS_RESULT result = EAS_Init(&mEASDataHandle);
101 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to initialize synthesizer library";
102
103 ASSERT_NE(mEASDataHandle, nullptr) << "Failed to initialize EAS data handle";
104
105 result = EAS_OpenFile(mEASDataHandle, &mEasFile, &mEASStreamHandle);
106 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to open file";
107
108 ASSERT_NE(mEASStreamHandle, nullptr) << "Failed to initialize EAS stream handle";
109
110 result = EAS_Prepare(mEASDataHandle, mEASStreamHandle);
111 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to prepare EAS data and stream handles";
112
113 EAS_I32 playTimeMs;
114 result = EAS_ParseMetaData(mEASDataHandle, mEASStreamHandle, &playTimeMs);
115 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to parse meta data";
116
117 ASSERT_EQ(playTimeMs, mAudioplayTimeMs)
118 << "Invalid audio play time found for file: " << mInputMediaFile;
119
120 EAS_I32 locationMs = -1;
121 /* EAS_ParseMetaData resets the parser to the starting of file */
122 result = EAS_GetLocation(mEASDataHandle, mEASStreamHandle, &locationMs);
123 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to get the location after parsing meta data";
124
125 ASSERT_EQ(locationMs, 0) << "Expected position: 0, found: " << locationMs;
126
127 mEASConfig = EAS_Config();
128 ASSERT_NE(mEASConfig, nullptr) << "Failed to configure the library";
129
130 ASSERT_GT(mEASConfig->mixBufferSize, 0) << "Mix buffer size must be greater than 0";
131
132 ASSERT_GT(mEASConfig->numChannels, 0) << "Number of channels must be greater than 0";
133
134 mPCMBufferSize = sizeof(EAS_PCM) * mEASConfig->mixBufferSize * mEASConfig->numChannels *
135 kNumBuffersToCombine;
136
137 mPCMBuffer = new (std::nothrow) EAS_PCM[mPCMBufferSize];
138 ASSERT_NE(mPCMBuffer, nullptr) << "Failed to allocate a memory of size: " << mPCMBufferSize;
139
140 mAudioBuffer =
141 new (std::nothrow) EAS_PCM[mEASConfig->mixBufferSize * mEASConfig->numChannels];
142 ASSERT_NE(mAudioBuffer, nullptr) << "Failed to allocate a memory of size: "
143 << mEASConfig->mixBufferSize * mEASConfig->numChannels;
144 }
145
TearDown()146 virtual void TearDown() {
147 EAS_RESULT result;
148 if (mEASDataHandle) {
149 if (mEASStreamHandle) {
150 result = EAS_CloseFile(mEASDataHandle, mEASStreamHandle);
151 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to close audio file/stream";
152 }
153 result = EAS_Shutdown(mEASDataHandle);
154 ASSERT_EQ(result, EAS_SUCCESS)
155 << "Failed to deallocate the resources for synthesizer library";
156 }
157 }
158
159 bool seekToLocation(EAS_I32);
160 bool renderAudio();
161 int readAt(void *buf, int offset, int size);
162 int getSize();
163
164 string mInputMediaFile;
165 uint32_t mAudioplayTimeMs;
166 uint32_t mTotalAudioChannels;
167 uint32_t mAudioSampleRate;
168 off64_t mBase;
169 int64_t mLength;
170 int mFd;
171
172 FILE *mInputFp;
173 EAS_DATA_HANDLE mEASDataHandle;
174 EAS_HANDLE mEASStreamHandle;
175 EAS_FILE mEasFile;
176 EAS_PCM *mPCMBuffer;
177 EAS_PCM *mAudioBuffer;
178 EAS_I32 mPCMBufferSize;
179 const S_EAS_LIB_CONFIG *mEASConfig;
180 };
181
readAt(void * handle,void * buffer,int offset,int size)182 static int readAt(void *handle, void *buffer, int offset, int size) {
183 return ((SonivoxTest *)handle)->readAt(buffer, offset, size);
184 }
185
getSize(void * handle)186 static int getSize(void *handle) {
187 return ((SonivoxTest *)handle)->getSize();
188 }
189
readAt(void * buffer,int offset,int size)190 int SonivoxTest::readAt(void *buffer, int offset, int size) {
191 if (offset > mLength) offset = mLength;
192 lseek(mFd, mBase + offset, SEEK_SET);
193 if (offset + size > mLength) {
194 size = mLength - offset;
195 }
196
197 return read(mFd, buffer, size);
198 }
199
getSize()200 int SonivoxTest::getSize() {
201 return mLength;
202 }
203
seekToLocation(EAS_I32 locationExpectedMs)204 bool SonivoxTest::seekToLocation(EAS_I32 locationExpectedMs) {
205 EAS_RESULT result = EAS_Locate(mEASDataHandle, mEASStreamHandle, locationExpectedMs, false);
206 if (result != EAS_SUCCESS) return false;
207
208 // position in milliseconds
209 EAS_I32 locationReceivedMs;
210 result = EAS_GetLocation(mEASDataHandle, mEASStreamHandle, &locationReceivedMs);
211 if (result != EAS_SUCCESS) return false;
212
213 if (locationReceivedMs != locationExpectedMs) return false;
214
215 return true;
216 }
217
renderAudio()218 bool SonivoxTest::renderAudio() {
219 EAS_I32 count = -1;
220 EAS_PCM *pcm = mAudioBuffer;
221
222 EAS_RESULT result = EAS_Render(mEASDataHandle, pcm, mEASConfig->mixBufferSize, &count);
223 if (result != EAS_SUCCESS) {
224 ALOGE("Failed to render audio");
225 return false;
226 }
227 if (count != mEASConfig->mixBufferSize) {
228 ALOGE("%ld of %ld bytes rendered", count, mEASConfig->mixBufferSize);
229 return false;
230 }
231
232 return true;
233 }
234
TEST_P(SonivoxTest,DecodeTest)235 TEST_P(SonivoxTest, DecodeTest) {
236 EAS_I32 totalChannels = mEASConfig->numChannels;
237 ASSERT_EQ(totalChannels, mTotalAudioChannels)
238 << "Expected: " << mTotalAudioChannels << " channels, Found: " << totalChannels;
239
240 EAS_I32 sampleRate = mEASConfig->sampleRate;
241 ASSERT_EQ(sampleRate, mAudioSampleRate)
242 << "Expected: " << mAudioSampleRate << " sample rate, Found: " << sampleRate;
243
244 // TODO(b/158231824): Check and verify the output with other parameters present at eas_reverb.h
245 // select reverb preset and enable
246 EAS_RESULT result = EAS_SetParameter(mEASDataHandle, EAS_MODULE_REVERB, EAS_PARAM_REVERB_PRESET,
247 EAS_PARAM_REVERB_CHAMBER);
248 ASSERT_EQ(result, EAS_SUCCESS)
249 << "Failed to set reverberation preset parameter in reverb module";
250
251 result =
252 EAS_SetParameter(mEASDataHandle, EAS_MODULE_REVERB, EAS_PARAM_REVERB_BYPASS, EAS_FALSE);
253 ASSERT_EQ(result, EAS_SUCCESS)
254 << "Failed to set reverberation bypass parameter in reverb module";
255
256 EAS_I32 count;
257 EAS_STATE state;
258
259 FILE *filePtr = fopen(OUTPUT_FILE, "wb");
260 ASSERT_NE(filePtr, nullptr) << "Failed to open file: " << OUTPUT_FILE;
261
262 while (1) {
263 EAS_PCM *pcm = mPCMBuffer;
264 int32_t numBytesOutput = 0;
265 result = EAS_State(mEASDataHandle, mEASStreamHandle, &state);
266 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to get EAS State";
267
268 ASSERT_NE(state, EAS_STATE_ERROR) << "Error state found";
269
270 /* is playback complete */
271 if (state == EAS_STATE_STOPPED) {
272 break;
273 }
274
275 EAS_I32 locationMs;
276 result = EAS_GetLocation(mEASDataHandle, mEASStreamHandle, &locationMs);
277 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to get the current location in ms";
278
279 if (locationMs >= mAudioplayTimeMs) {
280 ASSERT_NE(state, EAS_STATE_STOPPED)
281 << "Invalid state reached when rendering is complete";
282
283 break;
284 }
285
286 for (uint32_t i = 0; i < kNumBuffersToCombine; i++) {
287 result = EAS_Render(mEASDataHandle, pcm, mEASConfig->mixBufferSize, &count);
288 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to render the audio data";
289
290 pcm += count * mEASConfig->numChannels;
291 numBytesOutput += count * mEASConfig->numChannels * sizeof(EAS_PCM);
292 }
293 int32_t numBytes = fwrite(mPCMBuffer, 1, numBytesOutput, filePtr);
294 ASSERT_EQ(numBytes, numBytesOutput)
295 << "Wrote " << numBytes << " of " << numBytesOutput << " to file: " << OUTPUT_FILE;
296 }
297 fclose(filePtr);
298 }
299
TEST_P(SonivoxTest,SeekTest)300 TEST_P(SonivoxTest, SeekTest) {
301 bool status = seekToLocation(0);
302 ASSERT_TRUE(status) << "Seek test failed for location(ms): 0";
303
304 status = seekToLocation(mAudioplayTimeMs / 2);
305 ASSERT_TRUE(status) << "Seek test failed for location(ms): " << mAudioplayTimeMs / 2;
306
307 status = seekToLocation(mAudioplayTimeMs);
308 ASSERT_TRUE(status) << "Seek test failed for location(ms): " << mAudioplayTimeMs;
309
310 status = seekToLocation(mAudioplayTimeMs + kSeekBeyondPlayTimeOffsetMs);
311 ASSERT_FALSE(status) << "Invalid seek position: "
312 << mAudioplayTimeMs + kSeekBeyondPlayTimeOffsetMs;
313 }
314
TEST_P(SonivoxTest,DecodePauseResumeTest)315 TEST_P(SonivoxTest, DecodePauseResumeTest) {
316 EAS_I32 seekPosition = mAudioplayTimeMs / 2;
317 // go to middle of the audio
318 EAS_RESULT result = EAS_Locate(mEASDataHandle, mEASStreamHandle, seekPosition, false);
319 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to locate to location(ms): " << seekPosition;
320
321 bool status = renderAudio();
322 ASSERT_TRUE(status) << "Failed to render audio";
323
324 result = EAS_Pause(mEASDataHandle, mEASStreamHandle);
325 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to pause";
326
327 // will render previous audio again, no change in audio position
328 status = renderAudio();
329 ASSERT_TRUE(status) << "should not move audio position, since we're paused";
330
331 // current position in milliseconds
332 EAS_I32 currentPosMs = -1;
333 result = EAS_GetLocation(mEASDataHandle, mEASStreamHandle, ¤tPosMs);
334 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to get current location";
335
336 ASSERT_EQ(currentPosMs, seekPosition) << "Must not move the audio position after pause";
337
338 EAS_STATE state;
339 result = EAS_State(mEASDataHandle, mEASStreamHandle, &state);
340 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to get EAS state";
341
342 ASSERT_EQ(state, EAS_STATE_PAUSED) << "Invalid state reached when paused";
343
344 result = EAS_Resume(mEASDataHandle, mEASStreamHandle);
345 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to resume";
346
347 status = renderAudio();
348 ASSERT_TRUE(status) << "Failed to render audio after resume";
349
350 currentPosMs = -1;
351 result = EAS_GetLocation(mEASDataHandle, mEASStreamHandle, ¤tPosMs);
352 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to get current location";
353
354 ASSERT_GT(currentPosMs, seekPosition) << "Invalid position after resuming";
355
356 result = EAS_State(mEASDataHandle, mEASStreamHandle, &state);
357 ASSERT_EQ(result, EAS_SUCCESS) << "Failed to get EAS state";
358
359 ASSERT_EQ(state, EAS_STATE_PLAY) << "Invalid state reached when resumed";
360 }
361
362 INSTANTIATE_TEST_SUITE_P(SonivoxTestAll, SonivoxTest,
363 ::testing::Values(make_tuple("midi_a.mid", 2000, 2, 22050),
364 make_tuple("midi8sec.mid", 8002, 2, 22050),
365 make_tuple("midi_cs.mid", 2000, 2, 22050),
366 make_tuple("midi_gs.mid", 2000, 2, 22050),
367 make_tuple("ants.mid", 17233, 2, 22050),
368 make_tuple("testmxmf.mxmf", 29095, 2, 22050)));
369
main(int argc,char ** argv)370 int main(int argc, char **argv) {
371 gEnv = new SonivoxTestEnvironment();
372 ::testing::AddGlobalTestEnvironment(gEnv);
373 ::testing::InitGoogleTest(&argc, argv);
374 int status = gEnv->initFromOptions(argc, argv);
375 if (status == 0) {
376 status = RUN_ALL_TESTS();
377 ALOGV("Test result = %d\n", status);
378 }
379 return status;
380 }
381