Android Question How to skip slience during audio recording

toby

Well-Known Member
Licensed User
Longtime User
Could someone show me how to skip silence during voice recording, please? I'm using Audio library 1.71 and I'm willing to switch to another library if necessary.

TIA
 

Midimaster

Active Member
Licensed User
It depents on how you are saving/using the resulting stream....

What do you mean with "skip". Skipping during listening the playback? Or not saving the silent parts?

Lets say you record 16bit/22.000Hz

16bit are values betwenn -32000 upto +32000, so a value betwenn -100 and +100 could be seen as "silence"
22.000Hz means 2.200 samples in 100msec. This is a small pause like a 16th-note.


I would keep the audio recorder open all the time and scan the buffer's SHORTs, whether there are samples lower than a threshold level (f.e. ABS(sample)< 100 )for a amount of 2200 SHORTS (100msec). Then I would not forward these samples to a (second) file stream until there are again samples higher than level>100 .
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Thank you @Midimaster very much for the detailed information which is very helpful! You're indeed the midi master.

It depents on how you are saving/using the resulting stream....

What do you mean with "skip". Skipping during listening the playback? Or not saving the silent parts?
My app will record audio in the background continuously. If the audio sample is lower than the threshold for 5 seconds or longer, for example, 1 hour long, I want to trim the silence from the final output wave file.

In this example app, if I check every byte of data Buffer contains in the following sub, the test app would be very slow. Is this the right place for me to check for silence? I think I didn't do it correctly. Could you give me more hints, please?

B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
    If recording Then output.WriteBytes(Buffer, 0, Buffer.length)
    'the above check is required as the last message will arrive after we call StopRecording.
End Sub

TIA
 
Upvote 0

Midimaster

Active Member
Licensed User
In Erel's example you have no influence on the amout of bytes that arrived in this moment, when streamer_RecordBuffer is called.
I think you will need a second buffer, where you copy the bytes of streamer_RecordBuffer instead of writing them directly to the file.

Also it it necessary to convert them into SHORTs to be able to check if the sample value is smaller then the threshold. As the AudioStreamer sends Bytes, two bytes together build the samples value: 256*Byte(1)+Byte(0). Therefore you need the ByteConverter library with the LittleEndian-Flag set to TRUE.

In the new SHORT-Buffer you first collect a lot of streamer_RecordBuffer Bytes, then check the buffer if you find any value higher than the threshold. Here starts your saving Now check the buffer if you find 110.250 Values (5*22050) below the threshold. Remember the beginning of this part. Then save the buffer from start to this beginning. Now check again for finding any value higher than the threshold to start the procedure again.
For saving the samples, you need to convert them back to BYTES with ByteConverter with the LittleEndian-Flag set to TRUE.

You could work with two SHORT-buffers. One for the receiving of the recordings, the other for checking an saving. Then switch the buffers.

Or you can use more elegant a ring buffer for continuously writing and reading in the same buffer. You will find my tutorial about audio ring buffers here in the forum. It is only a example for AUDIO OUTPUT RING BUFFER, but it shows the way and can be used also for develope a recording ring buffer.
 
Upvote 0
Top