Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring in writeByteAudioDataToStorage #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,7 @@ class WaveRecorder {
private suspend fun writeByteAudioDataToStorage() {
val bufferSize = calculateMinBufferSize(waveConfig)
val data = ByteArray(bufferSize)
val outputStream: OutputStream = if (fileUri != null) {
context.contentResolver.openOutputStream(fileUri ?: return) ?: return
} else {
val file = File(filePath!!)
FileOutputStream(file)
}
val outputStream = getOutputStream() ?: return
val dataOutputStream = DataOutputStream(outputStream)
val fileWriter = FileWriter(dataOutputStream)

Expand All @@ -198,42 +193,58 @@ class WaveRecorder {

val lastSkippedData = LinkedList<ByteArray>()

var fileDurationInMillis = BigDecimal.ZERO

while (audioRecorder.recordingState == AudioRecord.RECORDSTATE_RECORDING) {
val operationStatus = audioRecorder.read(data, 0, bufferSize)
processAudioData(data, bufferSize, fileWriter, lastSkippedData, bufferSizeToKeep)
}
updateState(RecorderState.STOP)
cleanup(dataOutputStream)
}

if (operationStatus != AudioRecord.ERROR_INVALID_OPERATION) {
val amplitude = getByteArrayAmplitude(data)
private fun getOutputStream(): OutputStream? {
return if (fileUri != null) {
context.contentResolver.openOutputStream(fileUri!!)
} else {
val file = File(filePath!!)
FileOutputStream(file)
}
}

if (isPaused.get())
updateState(RecorderState.PAUSE)
else {
private suspend fun processAudioData(
data: ByteArray,
bufferSize: Int,
fileWriter: FileWriter,
lastSkippedData: LinkedList<ByteArray>,
bufferSizeToKeep: Int,
) {
val operationStatus = audioRecorder.read(data, 0, bufferSize)
var fileDurationInMillis = BigDecimal.ZERO

if (silenceDetection) {
handleByteSilenceState(
amplitude,
lastSkippedData,
data,
bufferSizeToKeep
)
}
if (operationStatus != AudioRecord.ERROR_INVALID_OPERATION) {
val amplitude = getByteArrayAmplitude(data)

if (!isSkipping.get()) {
updateState(RecorderState.RECORDING)
lastSkippedData.forEach {
fileDurationInMillis += calculateDurationInMillis(it, waveConfig)
}
fileWriter.writeDataToStream(lastSkippedData, data)
fileDurationInMillis += calculateDurationInMillis(data, waveConfig)
}
if (isPaused.get()) {
updateState(RecorderState.PAUSE)
} else {
if (silenceDetection) {
handleByteSilenceState(amplitude, lastSkippedData, data, bufferSizeToKeep)
}

updateListeners(amplitude, fileDurationInMillis.toLong())
if (!isSkipping.get()) {
updateState(RecorderState.RECORDING)
lastSkippedData.forEach {
fileDurationInMillis += calculateDurationInMillis(it, waveConfig)
}
fileWriter.writeDataToStream(lastSkippedData, data)
fileDurationInMillis += calculateDurationInMillis(data, waveConfig)
}
}

updateListeners(amplitude, fileDurationInMillis.toLong())
}
updateState(RecorderState.STOP)
cleanup(dataOutputStream)
}

private fun cleanup(dataOutputStream: DataOutputStream) {
dataOutputStream.close()
}

@RequiresApi(Build.VERSION_CODES.M)
Expand Down