Skip to content

Commit

Permalink
feat: add new lib for ogg conversion
Browse files Browse the repository at this point in the history
- exorecordogg
  • Loading branch information
brookmg committed May 9, 2021
1 parent b21201b commit 81d24af
Show file tree
Hide file tree
Showing 121 changed files with 69,439 additions and 155 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
local.properties

/.idea
exorecordogg/src/main/obj/
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ android {
dependencies {

api project(':exorecord')
api project(':exorecordogg')
implementation 'com.google.android.exoplayer:exoplayer:2.13.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.32"
ext.kotlin_version = "1.5.0"
repositories {
google()
jcenter()
Expand Down
41 changes: 39 additions & 2 deletions exorecord/src/main/java/dev/brookmg/exorecord/lib/ExoRecord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,46 @@ import android.app.Application
class ExoRecord(private val application: Application) : IExoRecord{

val exoRecordProcessor: ExoRecordProcessor by lazy { ExoRecordProcessor(applicationContext = application) }
private val _listeners: HashMap<String, ExoRecordListener> = hashMapOf()

override suspend fun startRecording() = exoRecordProcessor.startRecording()
interface ExoRecordListener {
fun onStartRecording(recordFileName: String)
fun onStopRecording(record: IExoRecord.Record)
}

override suspend fun stopRecording(saveAsAAC: Boolean): IExoRecord.Record = exoRecordProcessor.stopRecording(saveAsAAC)
override suspend fun startRecording() : String {
val fileName = exoRecordProcessor.startRecording()
for (listener in _listeners.values) listener.onStartRecording(fileName)
return fileName
}

override suspend fun stopRecording(): IExoRecord.Record {
val record = exoRecordProcessor.stopRecording()
for (listener in _listeners.values) listener.onStopRecording(record)
return record
}

fun addExoRecordListener(tag: String, listener: ExoRecordListener) : Boolean {
if (_listeners.containsKey(tag)) return false
_listeners[tag] = listener
return true
}

fun clearListeners() = _listeners.clear()

fun removeExoRecordListener(tag: String) : Boolean{
return if (_listeners.containsKey(tag)) {
_listeners.remove(tag)
true
}
else false
}

fun removeExoRecordListener(listener: ExoRecordListener) : Boolean {
return if (_listeners.containsValue(listener)) {
val tags = _listeners.filterValues { it == listener }.keys
for (tag in tags) _listeners.remove(tag)
true
} else false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ExoRecordProcessor internal constructor(private val applicationContext: Co

private var inputEnded: Boolean = false
private var wavFile: WavFile? = null
private var filePath: String = ""
private var fileName: String = ""

init {
outputBuffer = AudioProcessor.EMPTY_BUFFER
Expand Down Expand Up @@ -114,19 +114,20 @@ class ExoRecordProcessor internal constructor(private val applicationContext: Co
}
}

override suspend fun startRecording() {
override suspend fun startRecording() : String {
stopRecording()
filePath = "radio-${System.nanoTime()}.wav"
wavFile = WavFile(applicationContext = applicationContext, filePath)
fileName = "radio-${System.nanoTime()}.wav"
wavFile = WavFile(applicationContext = applicationContext, fileName)
wavFile?.writeHeaders(sampleRateHz, bytePerFrame, channelCount)
isActive = true
return fileName
}

override suspend fun stopRecording(saveAsAAC: Boolean): IExoRecord.Record {
override suspend fun stopRecording(): IExoRecord.Record {
isActive = false
val aac = wavFile?.save(saveAsAAC = saveAsAAC)
val wav = wavFile?.save()
wavFile = null
return IExoRecord.Record(filePath, sampleRateHz, bytePerFrame, channelCount, aacFilePath = aac)
return IExoRecord.Record(fileName, sampleRateHz, bytePerFrame, channelCount)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package dev.brookmg.exorecord.lib

interface IExoRecord {

data class Record(val filePath: String, val sampleBitRate: Int, val bytePerFrame: Int,
val channelCount: Int, val aacFilePath: String? = null)
data class Record(val filePath: String, val sampleBitRate: Int, val bitRate: Int,
val channelCount: Int)

suspend fun startRecording()
suspend fun stopRecording(saveAsAAC: Boolean = false) : Record
suspend fun startRecording() : String
suspend fun stopRecording() : Record

}
33 changes: 11 additions & 22 deletions exorecord/src/main/java/dev/brookmg/exorecord/lib/WavFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import android.content.Context
import android.util.Log
import com.google.android.exoplayer2.Format.NO_VALUE
import dev.brookmg.exorecord.lib.Util.toByteArray
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.DataOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.RandomAccessFile
import java.io.*

class WavFile(
private val applicationContext: Context, private val fileName: String,
Expand Down Expand Up @@ -53,38 +48,32 @@ class WavFile(
fileOutputStream.write(2.toShort().toByteArray(2), 0, 2) // 22 - 24 - mono or stereo? 1 or 2? (or 5 or ???)

// THE CHAOS
fileOutputStream.writeInt(Integer.reverseBytes(sampleRateHz / 2)) // 24 - 28 - samples per second (numbers per second)
fileOutputStream.writeInt(Integer.reverseBytes(((sampleRateHz / 2) * bytePerFrame * channelCount))) // 28 - 32 - bytes per second
fileOutputStream.writeInt(Integer.reverseBytes(sampleRateHz)) // 24 - 28 - samples per second (numbers per second)
fileOutputStream.writeInt(Integer.reverseBytes(((sampleRateHz) * bytePerFrame ))) // 28 - 32 - bytes per second
fileOutputStream.write((bytePerFrame * channelCount).toShort().toByteArray(2), 0, 2) // 32 - 34 - # of bytes in one sample, for all channels
fileOutputStream.write((bytePerFrame * 8).toShort().toByteArray(2), 0, 2) // 34 - 36 // - how many bits in a sample(number)? usually 16 or 24
fileOutputStream.write((bytePerFrame * 8 / channelCount).toShort().toByteArray(2), 0, 2) // 34 - 36 // - how many bits in a sample(number)? usually 16 or 24

fileOutputStream.write("data".toByteArray()) // 36 40 - data
fileOutputStream.writeInt(Integer.reverseBytes(audioArray.size))
}

suspend fun save(saveAsAAC: Boolean = false) : String? = withContext(Dispatchers.IO){
suspend fun save() : String? = withContext(Dispatchers.IO){
try {

// Change the wav content size
fileOutputStream.write(audioArray.toByteArray())
fileOutputStream.close()

val randomAccessFile = RandomAccessFile(applicationContext.filesDir.absolutePath + File.separator + fileName, "rw")
randomAccessFile.seek(40)
randomAccessFile.write(Integer.reverseBytes(fileOutputStream.size() - 40).toByteArray(4), 0, 4)

randomAccessFile.seek(4)
randomAccessFile.write(Integer.reverseBytes(fileOutputStream.size() - 8).toByteArray(4), 0, 4)

randomAccessFile.write((fileOutputStream.size() - 8).toByteArray(4), 0, 4)
randomAccessFile.seek(40)
randomAccessFile.write((fileOutputStream.size() - 40).toByteArray(4), 0, 4)
randomAccessFile.close()
audioArray.clear()

if (saveAsAAC && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
return@withContext WavToAAC().convertWavToAAC(applicationContext, fileName,
samplingRate = currentSampleRateHZ,
audioBitRate = (currentSampleRateHZ) * currentBytePerFrame * currentChannelCount,
channelCount = currentChannelCount) { progress ->
Log.v("WaveToAAC" , "Working @ $progress%")
}
}
return@withContext fileName

} catch (e: Exception) {
e.printStackTrace()
Expand Down
118 changes: 0 additions & 118 deletions exorecord/src/main/java/dev/brookmg/exorecord/lib/WavToAAC.kt

This file was deleted.

1 change: 1 addition & 0 deletions exorecordogg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
59 changes: 59 additions & 0 deletions exorecordogg/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
ndkVersion "23.0.7123448"

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"

ndk { abiFilters 'arm64-v8a', 'x86_64', 'armeabi-v7a', 'x86' }
}

externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
}
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Empty file added exorecordogg/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions exorecordogg/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Loading

0 comments on commit 81d24af

Please sign in to comment.