1+ /*
2+ *
3+ * * Copyright (C) 2024 pedroSG94.
4+ * *
5+ * * Licensed under the Apache License, Version 2.0 (the "License");
6+ * * you may not use this file except in compliance with the License.
7+ * * You may obtain a copy of the License at
8+ * *
9+ * * http://www.apache.org/licenses/LICENSE-2.0
10+ * *
11+ * * Unless required by applicable law or agreed to in writing, software
12+ * * distributed under the License is distributed on an "AS IS" BASIS,
13+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * * See the License for the specific language governing permissions and
15+ * * limitations under the License.
16+ *
17+ */
18+
19+ package com.pedro.encoder.input.sources.audio
20+
21+ import com.pedro.common.TimeUtils
22+ import com.pedro.encoder.Frame
23+ import com.pedro.encoder.audio.AudioEncoder
24+ import com.pedro.encoder.input.audio.GetMicrophoneData
25+ import kotlinx.coroutines.CoroutineScope
26+ import kotlinx.coroutines.Dispatchers
27+ import kotlinx.coroutines.Job
28+ import kotlinx.coroutines.cancelAndJoin
29+ import kotlinx.coroutines.delay
30+ import kotlinx.coroutines.launch
31+ import kotlinx.coroutines.runBlocking
32+
33+ /* *
34+ * Created by pedro on 25/7/25.
35+ */
36+ class SilenceAudioSource : AudioSource (), GetMicrophoneData {
37+
38+ private var running = false
39+ private var job: Job ? = null
40+ private var sleepTime = 0L
41+ private val buffer = ByteArray (AudioEncoder .inputSize / 4 )
42+
43+ override fun create (sampleRate : Int , isStereo : Boolean , echoCanceler : Boolean , noiseSuppressor : Boolean ): Boolean {
44+ val channels = if (isStereo) 2 else 1
45+ sleepTime = ((buffer.size.toDouble() / (sampleRate * channels * 2L )) * 1000000L ).toLong()
46+ return true
47+ }
48+
49+ override fun start (getMicrophoneData : GetMicrophoneData ) {
50+ this .getMicrophoneData = getMicrophoneData
51+ running = true
52+ job = CoroutineScope (Dispatchers .IO ).launch {
53+ while (running) {
54+ getMicrophoneData.inputPCMData(Frame (buffer, 0 , buffer.size, TimeUtils .getCurrentTimeMicro()))
55+ delay(sleepTime)
56+ }
57+ }
58+ }
59+
60+ override fun stop () {
61+ running = false
62+ runBlocking { job?.cancelAndJoin() }
63+ }
64+
65+ override fun isRunning (): Boolean = running
66+
67+ override fun release () {}
68+
69+ override fun inputPCMData (frame : Frame ) {
70+ getMicrophoneData?.inputPCMData(frame)
71+ }
72+ }
0 commit comments