• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*---------------------------------------------------------------------------*
2  *  AudioHardwareRecord.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 #include "audioinwrapper.h"
25 
26 #define SAMPLING_RATE       44100
27 
28 #define N_FRAMES_PER_BUFFER   512  /* low-level driver counts in terms of frames, not samples */
29 #define N_TUPLES_PER_FRAME      1  /* tuple: a set of samples (set of 1 if mono, set of 2 if stereo */
30 
31 #define N_CHANNELS_PER_TUPLE    1  /* 1: mono; 2: stereo */
32 
33 #define N_TUPLES_PER_BUFFER   (N_FRAMES_PER_BUFFER * N_TUPLES_PER_FRAME)
34 #define N_SAMPLES_PER_BUFFER  (N_TUPLES_PER_BUFFER * N_CHANNELS_PER_TUPLE)
35 
36 #define N_SECONDS_TO_RECORD    10
37 #define N_SAMPLES_TO_RECORD   (SAMPLING_RATE * N_SECONDS_TO_RECORD * N_CHANNELS_PER_TUPLE)
38 
39 typedef short typeSample;
40 
41 /* store incoming samples here, then write to file at the end */
42 typeSample recordedSamples[N_SAMPLES_TO_RECORD];
43 
44 
main(int argc,char * argv[])45 int main(int argc, char* argv[])
46 {
47     int           rc;
48     unsigned int  i;
49 
50     memset(recordedSamples, 0, N_SAMPLES_TO_RECORD * sizeof(typeSample));
51 
52     rc = AudioSetInputFormat(SAMPLING_RATE, N_CHANNELS_PER_TUPLE);
53     if (rc != 0)
54     {
55         printf("ERROR: AudioSetInputFormat() returns %d\n", rc);
56         exit(1);
57     }
58 
59     rc = AudioOpen();
60     if (rc < 0)
61     {
62         printf("ERROR: AudioOpen() returns %d (device handle/ID)\n", rc);
63         exit(1);
64     }
65 
66     i = 0;
67     while (i <= N_SAMPLES_TO_RECORD - N_SAMPLES_PER_BUFFER)
68     {
69         rc = AudioRead(&(recordedSamples[i]), N_FRAMES_PER_BUFFER);
70         if (rc > 0)
71             i += (rc * N_TUPLES_PER_FRAME * N_CHANNELS_PER_TUPLE);
72         else
73             printf("ERROR: AudioRead() returns %d\n", rc);
74     }
75 
76     rc = AudioClose();
77     if (rc != 0)
78     {
79         printf("ERROR: AudioClose() returns %d\n", rc);
80         exit(1);
81     }
82 
83     /* write to file  */
84     {
85         FILE *fpOutput;
86         char *szFilename = "output_AudioHardwareRecord.pcm";
87 
88         fpOutput = fopen(szFilename, "wb");
89         if (fpOutput == NULL)
90         {
91             printf("ERROR: cannot create '%s'\n", szFilename);
92             exit(1);
93         }
94         fwrite(recordedSamples, sizeof(typeSample), i, fpOutput);
95         fclose(fpOutput);
96     }
97 
98     return 0;
99 }
100