@@ -8,13 +8,16 @@ import androidx.appcompat.app.AppCompatActivity
88import android.os.Bundle
99import android.os.SystemClock
1010import android.text.method.ScrollingMovementMethod
11+ import android.util.Log
12+ import android.widget.Button
1113import android.widget.TextView
1214import java.nio.ByteOrder
1315import kotlin.math.ceil
1416
1517class MainActivity : AppCompatActivity () {
1618
1719 var text : TextView ? = null
20+ var isound = 0
1821
1922 override fun onCreate (savedInstanceState : Bundle ? ) {
2023 super .onCreate(savedInstanceState)
@@ -27,30 +30,29 @@ class MainActivity : AppCompatActivity() {
2730
2831 text?.movementMethod = ScrollingMovementMethod ()
2932
30- val textViewText = StringBuilder ( )
33+ val button = findViewById< Button >( R .id.button )
3134
32- for (s in sounds) {
33- audioToPCM(s, textViewText)
35+ button.setOnClickListener {
36+ if (isound == sounds.size)
37+ isound = 0
38+ val textViewText = StringBuilder ()
39+ textViewText.append(" Decoding sound $isound \n " )
40+ audioToPCM(sounds[isound], textViewText)
3441 textViewText.append(" \n " )
35- }
3642
37- text?.text = textViewText.toString()
38- text?.invalidate()
43+ text?.text = textViewText.toString()
44+ text?.invalidate()
45+ ++ isound
46+ }
3947 }
4048
4149 fun audioToPCM (id : Int , textViewText : StringBuilder ) : FloatArray {
4250
4351 val decodingStart = SystemClock .uptimeMillis()
4452
45- textViewText.append(" opening ressource\n " )
46- text?.text = textViewText.toString()
47- text?.invalidate()
4853 val sampleFD = resources.openRawResourceFd(id)
4954 val mediaExtractor = MediaExtractor ()
5055
51- textViewText.append(" setting data source\n " )
52- text?.text = textViewText.toString()
53- text?.invalidate()
5456 mediaExtractor.setDataSource(sampleFD.fileDescriptor, sampleFD.startOffset, sampleFD.length)
5557 val format = mediaExtractor.getTrackFormat(0 )
5658
@@ -59,9 +61,6 @@ class MainActivity : AppCompatActivity() {
5961 val duration = format.getLong(MediaFormat .KEY_DURATION )
6062 val channelCount = format.getInteger(MediaFormat .KEY_CHANNEL_COUNT )
6163
62- textViewText.append(" getting decoder format\n " )
63- text?.text = textViewText.toString()
64- text?.invalidate()
6564 val mediaCodecList = MediaCodecList (MediaCodecList .REGULAR_CODECS )
6665 val mediaCodecName = mediaCodecList.findDecoderForFormat(format)
6766
@@ -79,13 +78,11 @@ class MainActivity : AppCompatActivity() {
7978 // Log.v("AudioMixer", "AudioEncoder.decode: channel count $channelCount")
8079 // Log.v("AudioMixer", "AudioEncoder.decode: track count: " + mediaExtractor.trackCount)
8180 // Log.v("AudioMixer", "AudioEncoder.decode: media codec name: $mediaCodecName")
82- text?.text = textViewText.toString()
83- text?.invalidate()
8481
8582 if (nFrames > 50000 )
8683 return floatArrayOf(0f )
8784
88- val result = FloatArray (10 * nFrames) { 0f }
85+ val result = FloatArray (2 * nFrames) { 0f }
8986 // Log.v("AudioMixer", "AudioEncoder.decode: result.size = " + result.size)
9087
9188 // val codec = MediaCodec.createByCodecName(mediaCodecName)
@@ -94,52 +91,42 @@ class MainActivity : AppCompatActivity() {
9491 return floatArrayOf(0f )
9592 }
9693
97- textViewText.append(" creating decoder\n " )
98- text?.text = textViewText.toString()
99- text?.invalidate()
10094 val codec = MediaCodec .createDecoderByType(mime)
10195 textViewText.append(" codecs name based on mime: ${codec.name} \n " )
102- text?.text = textViewText.toString()
103- textViewText.append(" configuring codec\n " )
104- text?.text = textViewText.toString()
105- text?.invalidate()
10696 codec.configure(format, null , null , 0 )
10797
108- textViewText.append(" starting codec\n " )
109- text?.text = textViewText.toString()
110- text?.invalidate()
11198 codec.start()
11299
113- textViewText.append(" selecting track\n " )
114- text?.text = textViewText.toString()
115- text?.invalidate()
116100 mediaExtractor.selectTrack(0 )
117101 var numSamples = 0
118102 val bufferInfo = MediaCodec .BufferInfo ()
119- val timeOutUs = 5000L
103+ val timeOutUs = 1000L
120104 var sawOutputEOS = false
121105 var sawInputEOS = false
122106 var numNoOutput = 0
123107 var counter = 0
124108
125- textViewText.append(" starting loop\n " )
126- text?.text = textViewText.toString()
127- text?.invalidate()
128109 while (! sawOutputEOS) {
129110
130111 if (! sawInputEOS) {
131112 // get input buffer index and then the input buffer itself
113+ Log .v(" DecoderTest" , " dequeInputBuffer" )
132114 val inputBufferIndex = codec.dequeueInputBuffer(timeOutUs)
115+ Log .v(" DecoderTest" , " Done" )
133116 if (inputBufferIndex >= 0 ) {
134-
117+ Log .v( " DecoderTest " , " getInputBuffer " )
135118 val inputBuffer = codec.getInputBuffer(inputBufferIndex)
119+ Log .v(" DecoderTest" , " Done" )
136120 if (inputBuffer == null ) {
137121 textViewText.append(" AudioEncoder.decode: failed to acquire input buffer\n " )
122+ Log .v(" DecoderTest" , " failed to acquire input buffer" )
138123 return floatArrayOf(0f )
139124 }
140125
141126 // write the next bunch of data from our media file to the input buffer
127+ Log .v(" DecoderTest" , " read sample data" )
142128 var sampleSize = mediaExtractor.readSampleData(inputBuffer, 0 )
129+ Log .v(" DecoderTest" , " Done" )
143130
144131 var presentationTimeUs = 0L
145132 var eosFlag = 0
@@ -153,53 +140,70 @@ class MainActivity : AppCompatActivity() {
153140 }
154141
155142 // queue the input buffer such that the codec can decode it
143+ Log .v(" DecoderTest" , " queue input buffer" )
156144 codec.queueInputBuffer(
157145 inputBufferIndex,
158146 0 ,
159147 sampleSize,
160148 presentationTimeUs,
161149 eosFlag
162150 )
151+ Log .v(" DecoderTest" , " Done" )
163152
164- if (! sawInputEOS)
153+ if (! sawInputEOS) {
154+ Log .v(" DecoderTest" , " advance media extractor" )
165155 mediaExtractor.advance()
156+ Log .v(" DecoderTest" , " Done" )
157+ }
166158 }
167159 }
168160
169161 // we are done decoding and can now read our result
162+ Log .v(" DecoderTest" , " deque output buffer" )
170163 val outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, timeOutUs)
164+ Log .v(" DecoderTest" , " Done" )
171165
172166 if (outputBufferIndex >= 0 ) {
173167 // finally get our output data and create a view to a short buffer which is the
174168 // standard data type for 16bit audio
169+ Log .v(" DecoderTest" , " get output buffer" )
175170 val outputBuffer = codec.getOutputBuffer(outputBufferIndex)
171+ Log .v(" DecoderTest" , " Done" )
172+
176173 if (outputBuffer == null ) {
177174 textViewText.append(" Cannot acquire output buffer\n " )
178175 return floatArrayOf(0f )
179176 }
180177
178+ Log .v(" DecoderTest" , " create short buffer" )
181179 val shortBuffer = outputBuffer.order(ByteOrder .nativeOrder()).asShortBuffer()
180+ Log .v(" DecoderTest" , " Done" )
182181
183182 var c2 = 0
184183 // convert the short data to floats and store it to the result-array which will be
185184 // returned later. We want to have mono output stream, so we add different channel
186185 // to the same index.
186+ Log .v(" DecoderTest" , " read short buffer from ${shortBuffer.position()} to ${shortBuffer.limit()} " )
187187 while (shortBuffer.position() < shortBuffer.limit()) {
188188 if (numSamples/ channelCount >= result.size) {
189189 textViewText.append(" Too many samples, something is wrong with track duration" )
190+ Log .v(" DecoderTest" , " Too many samples, something is wrong with track duration" )
190191 return result
191192 }
192193 result[numSamples / channelCount] + = shortBuffer.get().toFloat()
193194 ++ numSamples
194195 ++ c2
195196 if (c2 > 50000 ) {
196197 textViewText.append(" c2 too big, something is wrong" )
198+ Log .v(" DecoderTest" , " c2 too big, something is wrong" )
197199 return result
198200 }
199201
200202 }
201203
204+ Log .v(" DecoderTest" , " release output buffer" )
202205 codec.releaseOutputBuffer(outputBufferIndex, false )
206+ Log .v(" DecoderTest" , " Done" )
203207
204208 if ((bufferInfo.flags and MediaCodec .BUFFER_FLAG_END_OF_STREAM ) != 0 )
205209 sawOutputEOS = true
@@ -209,30 +213,31 @@ class MainActivity : AppCompatActivity() {
209213 ++ numNoOutput
210214 if (numNoOutput > 50 ) {
211215 textViewText.append(" it seems as I don't get output data from codec\n " )
216+ Log .v(" DecoderTest" , " it seems as I don't get output data from codec" )
212217 break
213218 }
214219 }
215220
216221
217222 counter++
218- if (counter > 50000 ) {
223+ if (counter > 50 ) {
219224 textViewText.append(" stopping decoding early since something seems wrong\n " )
225+ Log .v(" DecoderTest" , " stopping decoding early since something seems wrong" )
220226 break
221227 }
222228 }
223-
229+ Log .v( " DecoderTest " , " COUNTER: $counter " )
224230 val decodingEnd = SystemClock .uptimeMillis()
225231 textViewText.append(" decoded frames (total)= $numSamples \n " )
226232 textViewText.append(" decoded frames (per channel)= ${numSamples/ channelCount} \n " )
227233 textViewText.append(" time for decoding = ${decodingEnd- decodingStart} ms\n " )
228- text?.text = textViewText.toString()
229- text?.invalidate()
230234
231235 mediaExtractor.release()
232236 codec.stop()
233237 codec.release()
234238 sampleFD.close()
235239
240+ Log .v(" DecoderTest" , " scaling result" )
236241 val channelCountInv = 1.0f / channelCount
237242 for (i in result.indices)
238243 result[i] = channelCountInv * result[i] / 32768.0f // peak * peakValue
0 commit comments