1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 #include <math.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #include "Log.h"
21 #include "audio/AudioSignalFactory.h"
22 
generateSineWave(AudioHardware::BytesPerSample BPS,int maxPositive,AudioHardware::SamplingRate samplingRate,int signalFreq,int samples,bool stereo)23 android::sp<Buffer> AudioSignalFactory::generateSineWave(AudioHardware::BytesPerSample BPS,
24         int maxPositive, AudioHardware::SamplingRate samplingRate, int signalFreq,
25         int samples,  bool stereo)
26 {
27     int bufferSize = samples * (stereo? 2 : 1) * BPS;
28     android::sp<Buffer> buffer(new Buffer(bufferSize));
29     // only 16bit signed
30     ASSERT(BPS == AudioHardware::E2BPS);
31     int16_t* data = reinterpret_cast<int16_t*>(buffer->getData());
32     double multiplier = 2.0 * M_PI * (double)signalFreq / samplingRate;
33     for (int i = 0; i < samples; i++) {
34         double val = sin(multiplier * i) * maxPositive;
35         *data = (int16_t)val;
36         data++;
37         if(stereo) {
38             *data = (int16_t)val;
39             data++;
40         }
41     }
42     buffer->setSize(buffer->getCapacity());
43     return buffer;
44 }
generateWhiteNoise(AudioHardware::BytesPerSample BPS,int maxPositive,int samples,bool stereo)45 android::sp<Buffer> AudioSignalFactory::generateWhiteNoise(AudioHardware::BytesPerSample BPS,
46         int maxPositive, int samples, bool stereo)
47 {
48     int bufferSize = samples * (stereo? 2 : 1) * BPS;
49     android::sp<Buffer> buffer(new Buffer(bufferSize, bufferSize));
50     // only 16bit signed
51     ASSERT(BPS == AudioHardware::E2BPS);
52     srand(123456);
53     int16_t* data = reinterpret_cast<int16_t*>(buffer->getData());
54     int middle = RAND_MAX / 2;
55     double multiplier = (double)maxPositive / middle;
56     for (int i = 0; i < samples; i++) {
57         int val =  rand();
58         val = (int16_t)((val - middle) * maxPositive / middle);
59         *data = val;
60         data++;
61         if (stereo) {
62             *data = val;
63             data++;
64         }
65     }
66     buffer->setSize(buffer->getCapacity());
67     return buffer;
68 }
69 
generateZeroSound(AudioHardware::BytesPerSample BPS,int samples,bool stereo)70 android::sp<Buffer> AudioSignalFactory::generateZeroSound(AudioHardware::BytesPerSample BPS,
71         int samples, bool stereo)
72 {
73     int bufferSize = samples * (stereo? 2 : 1) * BPS;
74     android::sp<Buffer> buffer(new Buffer(bufferSize, bufferSize));
75     // only 16bit signed
76     ASSERT(BPS == AudioHardware::E2BPS);
77     int16_t* data = reinterpret_cast<int16_t*>(buffer->getData());
78     for (int i = 0; i < samples; i++) {
79         *data = 0;
80         data++;
81         if (stereo) {
82             *data = 0;
83             data++;
84         }
85     }
86     buffer->setSize(buffer->getCapacity());
87     return buffer;
88 }
89 
90 
91