Understand the basics of Android Audio Record

I used the AudioRecord class to do audio processing on Android. This is a bit of a songwriter, and I've referred to some Japanese pages, but I'm afraid there are a lot of ambiguities in the specs ...

It may be difficult to understand without basic knowledge of speech processing in the first place, but there are many APIs such as `positionNotificationPeriod` and `` `notificationMarkerPosition``` that are not clear what is different.

So, I will leave a memo (code with comments) of the official document and the specifications investigated in the local test.

AudioRecordSample.kt



import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
import android.util.Log
import kotlin.math.max

/**
 *Sample code for AudioRecord class
 */
class AudioRecordSample {

    //Sampling rate(Hz)
    //All device support guarantee is 44100 only
    private val samplingRate = 44100

    //frame rate(fps)
    //How many times you want to process audio data per second
    //Decide on your own
    private val frameRate = 10

    //1 frame of audio data(=Short value)Number of
    private val oneFrameDataCount = samplingRate / frameRate

    //Number of bytes of audio data in one frame(byte)
    // Byte = 8 bit, Short =Because it's 16 bit,Double short
    private val oneFrameSizeInByte = oneFrameDataCount * 2

    //Audio data buffer size(byte)
    //Requirement 1:Must be larger than oneFrameSizeInByte
    //Requirement 2:Must be greater than the minimum required by the device
    private val audioBufferSizeInByte =
            max(oneFrameSizeInByte * 10, //Appropriately provided a buffer for 10 frames
                    android.media.AudioRecord.getMinBufferSize(samplingRate,
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT))

    fun startRecording() {

        //Create an instance
        val audioRecord = AudioRecord(
                MediaRecorder.AudioSource.MIC, //Audio source
                samplingRate, //Sampling rate
                AudioFormat.CHANNEL_IN_MONO, //Channel settings.MONO and STEREO guarantees support for all devices
                AudioFormat.ENCODING_PCM_16BIT, //PCM16 guarantees support for all devices
                audioBufferSizeInByte) //buffer

        //How many audio data to process( =Number of data in one frame)
        audioRecord.positionNotificationPeriod = oneFrameDataCount

        //At the timing when the number specified here is reached,Subsequent onMarkerReached is called
        //Doesn't it seem necessary for normal streaming processing?
        audioRecord.notificationMarkerPosition = 40000 //Do not set if not used.

        //Array to store audio data
        val audioDataArray = ShortArray(oneFrameDataCount)

        //Specify callback
        audioRecord.setRecordPositionUpdateListener(object : AudioRecord.OnRecordPositionUpdateListener {

            //Processing for each frame
            override fun onPeriodicNotification(recorder: AudioRecord) {
                recorder.read(audioDataArray, 0, oneFrameDataCount) //Read voice data
                Log.v("AudioRecord", "onPeriodicNotification size=${audioDataArray.size}")
                //Process as you like
            }

            //Marker timing processing.
            //Called when notificationMarkerPosition is reached
            override fun onMarkerReached(recorder: AudioRecord) {
                recorder.read(audioDataArray, 0, oneFrameDataCount) //Read voice data
                Log.v("AudioRecord", "onMarkerReached size=${audioDataArray.size}")
                //Process as you like
            }
        })

        audioRecord.startRecording()
    }
}

It's just basic. I'll write it again if I can get more advanced insights such as performance.

Recommended Posts

Understand the basics of Android Audio Record
Understand the basics of docker
About the basics of Android development
Docker monitoring-explaining the basics of basics-
[For beginners] Quickly understand the basics of Java 8 Lambda
The basics of Swift's TableView
Understand the basic mechanism of log4j2.xml
The basics of SpringBoot + MyBatis + MySQL
The basics of the process of making a call with an Android app
[Challenge CircleCI from 0] Learn the basics of CircleCI
Understand the official sample Coffee of Dagger2
Now, I've summarized the basics of RecyclerView
[day: 5] I summarized the basics of Java
Looking back on the basics of Java
What is JSP? ~ Let's know the basics of JSP !! ~
[Ruby] Summary of class definitions. Master the basics.
[Android] [Java] Manage the state of CheckBox of ListView
I understood the very basics of character input
Basics of jQuery that even freeters can understand
The basics of the App Store "automatic renewal subscription"
The story of tuning android apps with libGDX
Basics of Ruby
Read the official Dagger2 documentation to understand the basics
[For beginners] DI ~ The basics of DI and DI in Spring ~
I was addicted to the record of the associated model
I saw the list view of Android development collectively
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
<Android> Change the background color of the List row of ListView
[Android] How to get the setting language of the terminal
I summarized the types and basics of Java exceptions
[Ruby] Class nesting, inheritance, and the basics of self
A record of studying the Spring Framework from scratch
[Android] Hook the tap of the anchor link in WebViewClient
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
Let's understand the function!
The world of clara-rules (3)
Basics of try-with-resources statement
The world of clara-rules (5)
The idea of quicksort
Definition of Android constants
The idea of jQuery
I didn't understand the behavior of Java Scanner and .nextLine ().
About truncation by the number of bytes of String on Android
I tried to summarize the basics of kotlin and java
Now, I understand the coordinate transformation method of UIView (Swift)
[Android] Exit the activity of the transition source at the time of screen transition
A story packed with the basics of Spring Boot (solved)
Follow function association memorandum (understand the description of the User model)
[Summary of technical books] Summary of reading "Learn Docker from the basics"