Home | History | Annotate | Download | only in src
      1 /*---------------------------------------------------------------------------*
      2  *  AudioInRecord.c  *
      3  *                                                                           *
      4  *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
      5  *                                                                           *
      6  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
      7  *  you may not use this file except in compliance with the License.         *
      8  *                                                                           *
      9  *  You may obtain a copy of the License at                                  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0                           *
     11  *                                                                           *
     12  *  Unless required by applicable law or agreed to in writing, software      *
     13  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
     14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
     15  *  See the License for the specific language governing permissions and      *
     16  *  limitations under the License.                                           *
     17  *                                                                           *
     18  *---------------------------------------------------------------------------*/
     19 
     20 #include <stdlib.h>
     21 #include <stdio.h>
     22 #include <limits.h>
     23 #include <string.h>
     24 #ifdef WIN32
     25 #include <windows.h>
     26 #include <mmsystem.h>
     27 #endif
     28 #include "audioin.h"
     29 
     30 #define SAMPLING_RATE       11025
     31 
     32 #define N_FRAMES_PER_BUFFER   512  /* low-level driver counts in terms of frames, not samples */
     33 #define N_TUPLES_PER_FRAME      1  /* tuple: a set of samples (set of 1 if mono, set of 2 if stereo */
     34 #define N_CHANNELS_PER_TUPLE    1  /* 1: mono; 2: stereo */
     35 
     36 #define N_TUPLES_PER_BUFFER   (N_FRAMES_PER_BUFFER * N_TUPLES_PER_FRAME)
     37 #define N_SAMPLES_PER_BUFFER  (N_TUPLES_PER_BUFFER * N_CHANNELS_PER_TUPLE)
     38 
     39 #define N_SECONDS_TO_RECORD    10
     40 #define N_SAMPLES_TO_RECORD   (N_SECONDS_TO_RECORD * SAMPLING_RATE * N_CHANNELS_PER_TUPLE)
     41 
     42 #define OUTPUT_FILENAME       "output_AudioInRecord.pcm"
     43 
     44 typedef short typeSample;
     45 
     46 /* store incoming samples here, then write to file at the end */
     47 typeSample recordedSamples[N_SAMPLES_TO_RECORD];
     48 
     49 
     50 int main(int argc, char* argv[])
     51 {
     52     AUDIOIN_H         hAudioIn;
     53     AUDIOIN_INFO      AudioInInfo;
     54     LHS_AUDIOIN_ERROR lhsErr;
     55     unsigned int      nSamples;
     56 
     57     printf("\nAudioTestRecord: capturing %u seconds of audio at %u Hz\n\n", N_SECONDS_TO_RECORD, SAMPLING_RATE);
     58 
     59     memset(recordedSamples, 0, N_SAMPLES_TO_RECORD * sizeof(typeSample));
     60 
     61     printf("Opening the AudioIn device:  ");
     62     lhsErr = lhs_audioinOpen(WAVE_MAPPER, SAMPLING_RATE, &hAudioIn);
     63     printf("lhs_audioinOpen()  returns %ld\n", lhsErr);
     64     if (lhsErr != LHS_AUDIOIN_OK)
     65     {
     66         printf("ERROR: Unable to open audio device\n\n");
     67         return 1;
     68     }
     69 
     70     printf("Starting the AudioIn device: ");
     71     lhsErr = lhs_audioinStart(hAudioIn);
     72     printf("lhs_audioinStart() returns %ld\n", lhsErr);
     73     if (lhsErr != LHS_AUDIOIN_OK)
     74     {
     75         printf("ERROR: Unable to start audio device\n\n");
     76         printf("Closing the AudioIn device: ");
     77         lhsErr = lhs_audioinClose(&hAudioIn);
     78         printf("lhs_audioinClose() returns %ld\n", lhsErr);
     79         if (lhsErr != LHS_AUDIOIN_OK)
     80         {
     81             printf("ERROR: Unable to close audio device\n\n");
     82             return 1;
     83         }
     84         return 1;
     85     }
     86 
     87     printf("... Start Speaking ...\n");
     88 
     89     nSamples = 0;
     90     while (nSamples <= N_SAMPLES_TO_RECORD - N_SAMPLES_PER_BUFFER)
     91     {
     92         unsigned long u32NbrOfSamples;
     93 
     94         u32NbrOfSamples = N_SAMPLES_PER_BUFFER / N_CHANNELS_PER_TUPLE;  /* audioin only does mono */
     95         lhsErr = lhs_audioinGetSamples(hAudioIn, &u32NbrOfSamples, &(recordedSamples[nSamples]), &AudioInInfo);
     96         if (lhsErr == LHS_AUDIOIN_OK)
     97             nSamples += u32NbrOfSamples;
     98         else
     99             printf("ERROR: lhs_audioinGetSamples() returns %ld\n", lhsErr);
    100     }
    101 
    102     printf("Stopping the AudioIn device: ");
    103     lhsErr = lhs_audioinStop(hAudioIn);
    104     printf("lhs_audioinStop()  returns %ld\n", lhsErr);
    105     if (lhsErr != LHS_AUDIOIN_OK)
    106     {
    107         printf("ERROR: Unable to stop audio device\n\n");
    108         printf("Closing the AudioIn device: ");
    109         lhsErr = lhs_audioinClose(&hAudioIn);
    110         printf("lhs_audioinClose() returns %ld\n", lhsErr);
    111         if (lhsErr != LHS_AUDIOIN_OK)
    112         {
    113             printf("ERROR: Unable to close audio device\n\n");
    114             return 1;
    115         }
    116         return 1;
    117     }
    118 
    119     printf("Closing the AudioIn device:  ");
    120     lhsErr = lhs_audioinClose(&hAudioIn);
    121     printf("lhs_audioinClose() returns %ld\n", lhsErr);
    122     if (lhsErr != LHS_AUDIOIN_OK)
    123     {
    124         printf("ERROR: Unable to close audio device\n\n");
    125         return 1;
    126     }
    127 
    128     /* write to file  */
    129     {
    130         FILE *fpOutput;
    131         char *szFilename = OUTPUT_FILENAME;
    132 
    133         fpOutput = fopen(szFilename, "wb");
    134         if (fpOutput == NULL)
    135         {
    136             printf("ERROR: cannot create output file: '%s'\n", szFilename);
    137             return 1;
    138         }
    139         fwrite(recordedSamples, sizeof(typeSample), nSamples, fpOutput);
    140         fclose(fpOutput);
    141 
    142         printf("\nOutput audio saved to '%s'\n\n", szFilename);
    143     }
    144 
    145 
    146     return 0;
    147 }
    148