jiayan
jiayan

Reputation: 11

how audioRecord retrieve data with a specified sampling rate

my experiment is like this: first, I use matlab to create a specified wave file with a rate of 44100, which means any fragment lasting 1s contains 44100 elements and these elements are presented as double. then, I use smartphone's microphone to retrieve the wave. And the sampling rate is 44100, in order to restore the wave. But, audioRecord store the data as byte, while what i want is double. Converting from byte to double sounds reasonable, I still confused that sampling rate 44100 means the audioRecord should record 44100 bytes in 1s or 44100*4 bytes, since double contains 4 bytes? Other experiment i have committed: using recording software to retrieve wave and store in .wav read the .wav by matlab's wavread and by java respectively. To 1s, we get 44100 elements, and list below: -0.00164794921875
1.52587890625E-4
2.74658203125E-4
-0.003326416015625
0.001373291015625
-4.2724609375E-4
0.00445556640625
9.1552734375E-5
-9.1552734375E-4
7.62939453125E-4
-0.003997802734375
9.46044921875E-4
-0.00103759765625
0.002471923828125
0.001922607421875
-0.00250244140625
8.85009765625E-4
-0.0032958984375
8.23974609375E-4
8.23974609375E-4
anyone know how many elements the audioRecord will retrieve in 1s with the sampling rate of 44100?

Upvotes: 1

Views: 526

Answers (1)

nvuono
nvuono

Reputation: 3363

The default for AudioRecord is to return 16-bits per channel for each sample (ENCODING_PCM_16BIT).

Now there are two read overloads that let you specify either a short[] (16 bits) or a byte[] (8 bits) buffer.

int read(short[] audioData, int offsetInShorts, int sizeInShorts)
int read(byte[] audioData, int offsetInBytes, int sizeInBytes)

So a 1 second mono buffer (1 channel) should have a short[] buffer of length 44100. Stereo (2 channels) would have 88200, etc...

I would avoid using the byte[] buffer unless you had set the AudioRecord format to ENCODING_PCM_8BIT for some reason (it is not guaranteed to be supported by all devices).

Now if you want to convert those short values to doubles you have to realize that the double values you record in matlab are double-precision normalized samples which are normalized from [-1 to 1] while the short values are going to be from [-32768 to 32767] so you would have to write a conversion function instead of just trying to cast the numbers from a short to a double.

Upvotes: 3

Related Questions