Home | History | Annotate | Download | only in audiotests
      1 // Copyright 2008, The Android Open Source Project
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 //
     16 #define LOG_NDEBUG 0
     17 #define LOG_TAG "shared_mem_test"
     18 
     19 #include <stdlib.h>
     20 #include <stdio.h>
     21 #include <cutils/properties.h>
     22 #include <media/AudioSystem.h>
     23 #include <media/AudioTrack.h>
     24 #include <math.h>
     25 
     26 #include "shared_mem_test.h"
     27 #include <binder/MemoryDealer.h>
     28 #include <binder/MemoryHeapBase.h>
     29 #include <binder/MemoryBase.h>
     30 #include <binder/ProcessState.h>
     31 
     32 
     33 #include <utils/Log.h>
     34 
     35 #include <fcntl.h>
     36 
     37 namespace android {
     38 
     39 /************************************************************
     40 *
     41 *    Constructor
     42 *
     43 ************************************************************/
     44 AudioTrackTest::AudioTrackTest(void) {
     45 
     46     InitSine();         // init sine table
     47 
     48 }
     49 
     50 
     51 /************************************************************
     52 *
     53 *
     54 ************************************************************/
     55 void AudioTrackTest::Execute(void) {
     56     if (Test01() == 0) {
     57         ALOGD("01 passed\n");
     58     } else {
     59         ALOGD("01 failed\n");
     60     }
     61 }
     62 
     63 /************************************************************
     64 *
     65 *    Shared memory test
     66 *
     67 ************************************************************/
     68 #define BUF_SZ 44100
     69 
     70 int AudioTrackTest::Test01() {
     71 
     72     sp<MemoryDealer> heap;
     73     sp<IMemory> iMem;
     74     uint8_t* p;
     75 
     76     short smpBuf[BUF_SZ];
     77     long rate = 44100;
     78     unsigned long phi;
     79     unsigned long dPhi;
     80     long amplitude;
     81     long freq = 1237;
     82     float f0;
     83 
     84     f0 = pow(2., 32.) * freq / (float)rate;
     85     dPhi = (unsigned long)f0;
     86     amplitude = 1000;
     87     phi = 0;
     88     Generate(smpBuf, BUF_SZ, amplitude, phi, dPhi);  // fill buffer
     89 
     90     for (int i = 0; i < 1024; i++) {
     91         heap = new MemoryDealer(1024*1024, "AudioTrack Heap Base");
     92 
     93         iMem = heap->allocate(BUF_SZ*sizeof(short));
     94 
     95         p = static_cast<uint8_t*>(iMem->pointer());
     96         memcpy(p, smpBuf, BUF_SZ*sizeof(short));
     97 
     98         sp<AudioTrack> track = new AudioTrack(AUDIO_STREAM_MUSIC,// stream type
     99                rate,
    100                AUDIO_FORMAT_PCM_16_BIT,// word length, PCM
    101                AUDIO_CHANNEL_OUT_MONO,
    102                iMem);
    103 
    104         status_t status = track->initCheck();
    105         if(status != NO_ERROR) {
    106             track.clear();
    107             ALOGD("Failed for initCheck()");
    108             return -1;
    109         }
    110 
    111         // start play
    112         ALOGD("start");
    113         track->start();
    114 
    115         usleep(20000);
    116 
    117         ALOGD("stop");
    118         track->stop();
    119         iMem.clear();
    120         heap.clear();
    121         usleep(20000);
    122     }
    123 
    124     return 0;
    125 
    126 }
    127 
    128 /************************************************************
    129 *
    130 *    Generate a mono buffer
    131 *    Error is less than 3lsb
    132 *
    133 ************************************************************/
    134 void AudioTrackTest::Generate(short *buffer, long bufferSz, long amplitude, unsigned long &phi, long dPhi)
    135 {
    136     // fill buffer
    137     for(int i0=0; i0<bufferSz; i0++) {
    138         buffer[i0] = ComputeSine( amplitude, phi);
    139         phi += dPhi;
    140     }
    141 }
    142 
    143 /************************************************************
    144 *
    145 *    Generate a sine
    146 *    Error is less than 3lsb
    147 *
    148 ************************************************************/
    149 short AudioTrackTest::ComputeSine(long amplitude, long phi)
    150 {
    151     long pi13 = 25736;   // 2^13*pi
    152     long sample;
    153     long l0, l1;
    154 
    155     sample = (amplitude*sin1024[(phi>>22) & 0x3ff]) >> 15;
    156     // correct with interpolation
    157     l0 = (phi>>12) & 0x3ff;         // 2^20 * x / (2*pi)
    158     l1 = (amplitude*sin1024[((phi>>22) + 256) & 0x3ff]) >> 15;    // 2^15*cosine
    159     l0 = (l0 * l1) >> 10;
    160     l0 = (l0 * pi13) >> 22;
    161     sample = sample + l0;
    162 
    163     return (short)sample;
    164 }
    165 
    166 
    167 /************************************************************
    168 *
    169 *    init sine table
    170 *
    171 ************************************************************/
    172 void AudioTrackTest::InitSine(void) {
    173     double phi = 0;
    174     double dPhi = 2 * M_PI / SIN_SZ;
    175     for(int i0 = 0; i0<SIN_SZ; i0++) {
    176         long d0;
    177 
    178         d0 = 32768. * sin(phi);
    179         phi += dPhi;
    180         if(d0 >= 32767) d0 = 32767;
    181         if(d0 <= -32768) d0 = -32768;
    182         sin1024[i0] = (short)d0;
    183     }
    184 }
    185 
    186 /************************************************************
    187 *
    188 *    main in name space
    189 *
    190 ************************************************************/
    191 int main() {
    192     ProcessState::self()->startThreadPool();
    193     AudioTrackTest *test;
    194 
    195     test = new AudioTrackTest();
    196     test->Execute();
    197     delete test;
    198 
    199     return 0;
    200 }
    201 
    202 }
    203 
    204 /************************************************************
    205 *
    206 *    global main
    207 *
    208 ************************************************************/
    209 int main() {
    210 
    211     return android::main();
    212 }
    213