SndFile.c revision b7154f2324c8ae44b820c07c69aaa80a4bb9e418
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/* libsndfile integration */
18
19#include "sles_allinclusive.h"
20
21#ifdef USE_SNDFILE
22
23#include "SndFile.h"
24
25// FIXME should run this asynchronously esp. for socket fd, not on mix thread
26void SLAPIENTRY SndFile_Callback(SLBufferQueueItf caller, void *pContext)
27{
28    struct SndFile *this = (struct SndFile *) pContext;
29    SLresult result;
30    if (NULL != this->mRetryBuffer && 0 < this->mRetrySize) {
31        result = (*caller)->Enqueue(caller, this->mRetryBuffer,
32            this->mRetrySize);
33        if (SL_RESULT_BUFFER_INSUFFICIENT == result)
34            return;     // what, again?
35        assert(SL_RESULT_SUCCESS == result);
36        this->mRetryBuffer = NULL;
37        this->mRetrySize = 0;
38        return;
39    }
40    short *pBuffer = this->mIs0 ? this->mBuffer0 : this->mBuffer1;
41    this->mIs0 ^= SL_BOOLEAN_TRUE;
42    sf_count_t count;
43    // FIXME magic number
44    count = sf_read_short(this->mSNDFILE, pBuffer, (sf_count_t) 512);
45    if (0 < count) {
46        SLuint32 size = count * sizeof(short);
47        // FIXME if we had an internal API, could call this directly
48        result = (*caller)->Enqueue(caller, pBuffer, size);
49        if (SL_RESULT_BUFFER_INSUFFICIENT == result) {
50            this->mRetryBuffer = pBuffer;
51            this->mRetrySize = size;
52            return;
53        }
54        assert(SL_RESULT_SUCCESS == result);
55    }
56}
57
58SLboolean SndFile_IsSupported(const SF_INFO *sfinfo)
59{
60    switch (sfinfo->format & SF_FORMAT_TYPEMASK) {
61    case SF_FORMAT_WAV:
62        break;
63    default:
64        return SL_BOOLEAN_FALSE;
65    }
66    switch (sfinfo->format & SF_FORMAT_SUBMASK) {
67    case SF_FORMAT_PCM_16:
68        break;
69    default:
70        return SL_BOOLEAN_FALSE;
71    }
72    switch (sfinfo->samplerate) {
73    case 44100:
74        break;
75    default:
76        return SL_BOOLEAN_FALSE;
77    }
78    switch (sfinfo->channels) {
79    case 2:
80        break;
81    default:
82        return SL_BOOLEAN_FALSE;
83    }
84    return SL_BOOLEAN_TRUE;
85}
86
87#endif // USE_SNDFILE
88