• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 #include <string.h>
18 
19 #include "wav/WavStreamReader.h"
20 
21 #include "OneShotSampleSource.h"
22 
23 namespace iolib {
24 
mixAudio(float * outBuff,int numChannels,int32_t numFrames)25 void OneShotSampleSource::mixAudio(float* outBuff, int numChannels, int32_t numFrames) {
26     int32_t numSamples = mSampleBuffer->getNumSamples();
27     int32_t sampleChannels = mSampleBuffer->getProperties().channelCount;
28     int32_t samplesLeft = numSamples - mCurSampleIndex;
29     int32_t numWriteFrames = mIsPlaying
30                          ? std::min(numFrames, samplesLeft / sampleChannels)
31                          : 0;
32 
33     if (numWriteFrames != 0) {
34         const float* data  = mSampleBuffer->getSampleData();
35         if ((sampleChannels == 1) && (numChannels == 1)) {
36             // MONO output from MONO samples
37             for (int32_t frameIndex = 0; frameIndex < numWriteFrames; frameIndex++) {
38                 outBuff[frameIndex] += data[mCurSampleIndex++] * mGain;
39             }
40         } else if ((sampleChannels == 1) && (numChannels == 2)) {
41             // STEREO output from MONO samples
42             int dstSampleIndex = 0;
43             for (int32_t frameIndex = 0; frameIndex < numWriteFrames; frameIndex++) {
44                 outBuff[dstSampleIndex++] += data[mCurSampleIndex] * mLeftGain;
45                 outBuff[dstSampleIndex++] += data[mCurSampleIndex++] * mRightGain;
46             }
47         } else if ((sampleChannels == 2) && (numChannels == 1)) {
48             // MONO output from STEREO samples
49             int dstSampleIndex = 0;
50             for (int32_t frameIndex = 0; frameIndex < numWriteFrames; frameIndex++) {
51                 outBuff[dstSampleIndex++] += data[mCurSampleIndex++] * mLeftGain +
52                                              data[mCurSampleIndex++] * mRightGain;
53             }
54         } else if ((sampleChannels == 2) && (numChannels == 2)) {
55             // STEREO output from STEREO samples
56             int dstSampleIndex = 0;
57             for (int32_t frameIndex = 0; frameIndex < numWriteFrames; frameIndex++) {
58                 outBuff[dstSampleIndex++] += data[mCurSampleIndex++] * mLeftGain;
59                 outBuff[dstSampleIndex++] += data[mCurSampleIndex++] * mRightGain;
60             }
61         }
62 
63         if (mCurSampleIndex >= numSamples) {
64             mIsPlaying = false;
65         }
66     }
67 
68     // silence
69     // no need as the output buffer would need to have been filled with silence
70     // to be mixed into
71 }
72 
73 } // namespace wavlib
74