slesTestBassBoostPath.cpp revision 00667fcca51d62236b538e6857b7e6b923453569
1/*
2 * Copyright (C) 2010 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#ifdef ANDROID
18#define LOG_NDEBUG 0
19#define LOG_TAG "slesTest_bassboost"
20
21#include <utils/Log.h>
22#else
23#define LOGV printf
24#endif
25
26#include <getopt.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <unistd.h>
31#include <sys/time.h>
32#include <fcntl.h>
33
34#include "SLES/OpenSLES.h"
35#ifdef ANDROID
36#include "SLES/OpenSLES_Android.h"
37#endif
38
39
40#define MAX_NUMBER_INTERFACES 3
41
42#define TIME_S_BETWEEN_BB_ON_OFF 3
43
44//-----------------------------------------------------------------
45/* Exits the application if an error is encountered */
46#define ExitOnError(x) ExitOnErrorFunc(x,__LINE__)
47
48void ExitOnErrorFunc( SLresult result , int line)
49{
50    if (SL_RESULT_SUCCESS != result) {
51        fprintf(stderr, "%lu error code encountered at line %d, exiting\n", result, line);
52        exit(1);
53    }
54}
55
56
57//-----------------------------------------------------------------
58
59/* Play an audio path by opening a file descriptor on that path  */
60void TestBassBoostPathFromFD( SLObjectItf sl, const char* path, int16_t boostStrength)
61{
62    SLresult  result;
63    SLEngineItf EngineItf;
64
65    /* Objects this application uses: one player and an ouput mix */
66    SLObjectItf  player, outputMix;
67
68    /* Source of audio data to play */
69    SLDataSource            audioSource;
70#ifdef ANDROID
71    SLDataLocator_AndroidFD locatorFd;
72#else
73    SLDataLocator_URI       locatorUri;
74#endif
75    SLDataFormat_MIME       mime;
76
77    /* Data sinks for the audio player */
78    SLDataSink               audioSink;
79    SLDataLocator_OutputMix  locator_outputmix;
80
81    /* Interfaces for the audio player */
82    SLPlayItf              playItf;
83    SLPrefetchStatusItf    prefetchItf;
84    SLBassBoostItf         bbItf;
85
86    SLboolean required[MAX_NUMBER_INTERFACES];
87    SLInterfaceID iidArray[MAX_NUMBER_INTERFACES];
88
89    /* Get the SL Engine Interface which is implicit */
90    result = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void*)&EngineItf);
91    ExitOnError(result);
92
93    /* Initialize arrays required[] and iidArray[] */
94    for (int i=0 ; i < MAX_NUMBER_INTERFACES ; i++) {
95        required[i] = SL_BOOLEAN_FALSE;
96        iidArray[i] = SL_IID_NULL;
97    }
98
99    /* ------------------------------------------------------ */
100    /* Configuration of the output mix  */
101
102    /* Create Output Mix object to be used by the player */
103     result = (*EngineItf)->CreateOutputMix(EngineItf, &outputMix, 1, iidArray, required);
104     ExitOnError(result);
105
106    /* Realize the Output Mix object in synchronous mode */
107    result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
108    ExitOnError(result);
109
110    /* Setup the data sink structure */
111    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
112    locator_outputmix.outputMix   = outputMix;
113    audioSink.pLocator            = (void*)&locator_outputmix;
114    audioSink.pFormat             = NULL;
115
116    /* ------------------------------------------------------ */
117    /* Configuration of the player  */
118
119    /* Set arrays required[] and iidArray[] for required interfaces */
120    /*  (SLPlayItf is implicit) */
121    required[0] = SL_BOOLEAN_TRUE;
122    iidArray[0] = SL_IID_PREFETCHSTATUS;
123    required[1] = SL_BOOLEAN_TRUE;
124    iidArray[1] = SL_IID_BASSBOOST;
125
126#ifdef ANDROID
127    /* Setup the data source structure for the URI */
128    locatorFd.locatorType = SL_DATALOCATOR_ANDROIDFD;
129    int fd = open(path, O_RDONLY);
130    if (fd == -1) {
131        ExitOnError(SL_RESULT_RESOURCE_ERROR);
132    }
133    locatorFd.fd = (SLint32) fd;
134    locatorFd.length = SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE;
135    locatorFd.offset = 0;
136#else
137    locatorUri.locatorType = SL_DATALOCATOR_URI;
138    locatorUri.URI = (SLchar *) path;
139#endif
140
141    mime.formatType = SL_DATAFORMAT_MIME;
142    /*     this is how ignored mime information is specified, according to OpenSL ES spec
143     *     in 9.1.6 SLDataFormat_MIME and 8.23 SLMetadataTraversalItf GetChildInfo */
144    mime.mimeType      = (SLchar*)NULL;
145    mime.containerType = SL_CONTAINERTYPE_UNSPECIFIED;
146
147    audioSource.pFormat  = (void*)&mime;
148#ifdef ANDROID
149    audioSource.pLocator = (void*)&locatorFd;
150#else
151    audioSource.pLocator = (void*)&locatorUri;
152#endif
153
154    /* Create the audio player */
155    result = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 2,
156            iidArray, required);
157    ExitOnError(result);
158
159    /* Realize the player in synchronous mode. */
160    result = (*player)->Realize(player, SL_BOOLEAN_FALSE); ExitOnError(result);
161    fprintf(stdout, "URI example: after Realize\n");
162
163    /* Get the SLPlayItf, SLPrefetchStatusItf and SLBassBoostItf interfaces for the player */
164    result = (*player)->GetInterface(player, SL_IID_PLAY, (void*)&playItf);
165    ExitOnError(result);
166
167    result = (*player)->GetInterface(player, SL_IID_PREFETCHSTATUS, (void*)&prefetchItf);
168    ExitOnError(result);
169
170    result = (*player)->GetInterface(player, SL_IID_BASSBOOST, (void*)&bbItf);
171    ExitOnError(result);
172
173    fprintf(stdout, "Player configured\n");
174
175    /* ------------------------------------------------------ */
176    /* Playback and test */
177
178    /* Start the data prefetching by setting the player to the paused state */
179    result = (*playItf)->SetPlayState( playItf, SL_PLAYSTATE_PAUSED );
180    ExitOnError(result);
181
182    /* Wait until there's data to play */
183    SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
184    while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
185        usleep(100 * 1000);
186        (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
187        ExitOnError(result);
188    }
189
190    /* Get duration */
191    SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
192    result = (*playItf)->GetDuration(playItf, &durationInMsec);
193    ExitOnError(result);
194    if (durationInMsec == SL_TIME_UNKNOWN) {
195        durationInMsec = 5000;
196    }
197
198    /* Start playback */
199    fprintf(stdout, "Starting to play\n");
200    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING );
201    ExitOnError(result);
202
203    /* Configure BassBoost */
204    SLboolean strengthSupported = SL_BOOLEAN_FALSE;
205    result = (*bbItf)->IsStrengthSupported(bbItf, &strengthSupported);
206    ExitOnError(result);
207    if (SL_BOOLEAN_FALSE == strengthSupported) {
208        fprintf(stdout, "BassBoost strength is not supported on this platform. Too bad!\n");
209    } else {
210        fprintf(stdout, "BassBoost strength is supported, setting strength to %d\n", boostStrength);
211        result = (*bbItf)->SetStrength(bbItf, boostStrength);
212        ExitOnError(result);
213    }
214
215    SLpermille strength = 0;
216    result = (*bbItf)->GetRoundedStrength(bbItf, &strength);
217    ExitOnError(result);
218    fprintf(stdout, "Rounded strength of boost = %d\n", strength);
219
220
221    /* Switch BassBoost on/off every TIME_S_BETWEEN_BB_ON_OFF seconds */
222    SLboolean enabled = SL_BOOLEAN_TRUE;
223    result = (*bbItf)->SetEnabled(bbItf, enabled);
224    ExitOnError(result);
225    for(unsigned int j=0 ; j<(durationInMsec/(1000*TIME_S_BETWEEN_BB_ON_OFF)) ; j++) {
226        usleep(TIME_S_BETWEEN_BB_ON_OFF * 1000 * 1000);
227        result = (*bbItf)->IsEnabled(bbItf, &enabled);
228        ExitOnError(result);
229        enabled = enabled == SL_BOOLEAN_TRUE ? SL_BOOLEAN_FALSE : SL_BOOLEAN_TRUE;
230        result = (*bbItf)->SetEnabled(bbItf, enabled);
231        if (SL_BOOLEAN_TRUE == enabled) {
232            fprintf(stdout, "BassBoost on\n");
233        } else {
234            fprintf(stdout, "BassBoost off\n");
235        }
236        ExitOnError(result);
237    }
238
239    /* Make sure player is stopped */
240    fprintf(stdout, "Stopping playback\n");
241    result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
242    ExitOnError(result);
243
244    /* Destroy the player */
245    (*player)->Destroy(player);
246
247    /* Destroy Output Mix object */
248    (*outputMix)->Destroy(outputMix);
249
250#ifdef ANDROID
251    close(fd);
252#endif
253}
254
255//-----------------------------------------------------------------
256int main(int argc, char* const argv[])
257{
258    LOGV("Starting %s\n", argv[0]);
259
260    SLresult    result;
261    SLObjectItf sl;
262
263    fprintf(stdout, "OpenSL ES test %s: exercises SLBassBoostItf ", argv[0]);
264    fprintf(stdout, "and AudioPlayer with SLDataLocator_AndroidFD source / OutputMix sink\n");
265    fprintf(stdout, "Plays the sound file designated by the given path, ");
266    fprintf(stdout, "and applies a bass boost effect of the specified strength,\n");
267    fprintf(stdout, "where strength is a integer value between 0 and 1000.\n");
268    fprintf(stdout, "Every %d seconds, the BassBoost will be turned on and off.\n",
269            TIME_S_BETWEEN_BB_ON_OFF);
270
271    if (argc < 3) {
272        fprintf(stdout, "Usage: \t%s path bass_boost_strength\n", argv[0]);
273        fprintf(stdout, "Example: \"%s /sdcard/my.mp3 1000\" \n", argv[0]);
274        exit(1);
275    }
276
277    SLEngineOption EngineOption[] = {
278            {(SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE}
279    };
280
281    result = slCreateEngine( &sl, 1, EngineOption, 0, NULL, NULL);
282    ExitOnError(result);
283
284    /* Realizing the SL Engine in synchronous mode. */
285    result = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
286    ExitOnError(result);
287
288    // intentionally not checking that argv[2], the bassboost strength, is between 0 and 1000
289    TestBassBoostPathFromFD(sl, argv[1], (int16_t)atoi(argv[2]));
290
291    /* Shutdown OpenSL ES */
292    (*sl)->Destroy(sl);
293    exit(0);
294
295    return 0;
296}
297