-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
* Add Android sample * Use main project C files * Stop existing playback before starting new playback * Make text scrollable * Stop playback when starting to record * Remove extra var
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
A sample Android app using [whisper.cpp](https://github.com/ggerganov/whisper.cpp/) to do voice-to-text transcriptions. | ||
|
||
To use: | ||
|
||
1. Select a model from the [whisper.cpp repository](https://github.com/ggerganov/whisper.cpp/tree/master/models).[^1] | ||
2. Copy the model to the "app/src/main/assets/models" folder. | ||
3. Select a sample audio file (for example, [jfk.wav](https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav)). | ||
4. Copy the sample to the "app/src/main/assets/samples" folder. | ||
5. Select the "release" active build variant, and use Android Studio to run and deploy to your device. | ||
[^1]: I recommend the tiny or base models for running on an Android device. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
plugins { | ||
id 'com.android.application' | ||
id 'org.jetbrains.kotlin.android' | ||
} | ||
|
||
android { | ||
namespace 'com.whispercppdemo' | ||
compileSdk 33 | ||
|
||
defaultConfig { | ||
applicationId "com.whispercppdemo" | ||
minSdk 26 | ||
targetSdk 32 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
ndk { | ||
abiFilters 'arm64-v8a', 'x86_64' | ||
} | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
vectorDrawables { | ||
useSupportLibrary true | ||
} | ||
} | ||
|
||
buildTypes { | ||
release { | ||
signingConfig signingConfigs.debug | ||
minifyEnabled true | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
buildFeatures { | ||
compose true | ||
} | ||
composeOptions { | ||
kotlinCompilerExtensionVersion '1.3.1' | ||
} | ||
ndkVersion "25.0.8528842" | ||
externalNativeBuild { | ||
ndkBuild { | ||
path 'src/main/jni/whisper/Android.mk' | ||
} | ||
} | ||
packagingOptions { | ||
resources { | ||
excludes += '/META-INF/{AL2.0,LGPL2.1}' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation 'androidx.activity:activity-compose:1.6.1' | ||
implementation 'androidx.compose.material:material-icons-core:1.3.1' | ||
implementation 'androidx.compose.material3:material3:1.0.1' | ||
implementation "androidx.compose.ui:ui:1.3.2" | ||
implementation "androidx.compose.ui:ui-tooling-preview:1.3.2" | ||
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1' | ||
implementation "com.google.accompanist:accompanist-permissions:0.28.0" | ||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' | ||
|
||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.4' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' | ||
androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.3.2" | ||
debugImplementation "androidx.compose.ui:ui-tooling:1.3.2" | ||
debugImplementation "androidx.compose.ui:ui-test-manifest:1.3.2" | ||
} |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.whispercppdemo | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.whispercppdemo", appContext.packageName) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.WhisperCppDemo" | ||
tools:targetApi="31"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true" | ||
android:theme="@style/Theme.WhisperCppDemo"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
<meta-data | ||
android:name="android.app.lib_name" | ||
android:value="" /> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.whispercppdemo | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.viewModels | ||
import com.whispercppdemo.ui.main.MainScreen | ||
import com.whispercppdemo.ui.main.MainScreenViewModel | ||
import com.whispercppdemo.ui.theme.WhisperCppDemoTheme | ||
|
||
class MainActivity : ComponentActivity() { | ||
private val viewModel: MainScreenViewModel by viewModels { MainScreenViewModel.factory() } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
WhisperCppDemoTheme { | ||
MainScreen(viewModel) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.whispercppdemo.media | ||
|
||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.nio.ByteBuffer | ||
import java.nio.ByteOrder | ||
|
||
fun decodeWaveFile(file: File): FloatArray { | ||
val baos = ByteArrayOutputStream() | ||
file.inputStream().use { it.copyTo(baos) } | ||
val buffer = ByteBuffer.wrap(baos.toByteArray()) | ||
buffer.order(ByteOrder.LITTLE_ENDIAN) | ||
buffer.position(44) | ||
val shortBuffer = buffer.asShortBuffer() | ||
val shortArray = ShortArray(shortBuffer.limit()) | ||
shortBuffer.get(shortArray) | ||
return FloatArray(shortArray.size) { index -> | ||
(shortArray[index] / 32767.0f).coerceIn(-1f..1f) | ||
} | ||
} | ||
|
||
fun encodeWaveFile(file: File, data: ShortArray) { | ||
file.outputStream().use { | ||
it.write(headerBytes(data.size * 2)) | ||
val buffer = ByteBuffer.allocate(data.size * 2) | ||
buffer.order(ByteOrder.LITTLE_ENDIAN) | ||
buffer.asShortBuffer().put(data) | ||
val bytes = ByteArray(buffer.limit()) | ||
buffer.get(bytes) | ||
it.write(bytes) | ||
} | ||
} | ||
|
||
private fun headerBytes(totalLength: Int): ByteArray { | ||
require(totalLength >= 44) | ||
ByteBuffer.allocate(44).apply { | ||
order(ByteOrder.LITTLE_ENDIAN) | ||
|
||
put('R'.code.toByte()) | ||
put('I'.code.toByte()) | ||
put('F'.code.toByte()) | ||
put('F'.code.toByte()) | ||
|
||
putInt(totalLength - 8) | ||
|
||
put('W'.code.toByte()) | ||
put('A'.code.toByte()) | ||
put('V'.code.toByte()) | ||
put('E'.code.toByte()) | ||
|
||
put('f'.code.toByte()) | ||
put('m'.code.toByte()) | ||
put('t'.code.toByte()) | ||
put(' '.code.toByte()) | ||
|
||
putInt(16) | ||
putShort(1.toShort()) | ||
putShort(1.toShort()) | ||
putInt(16000) | ||
putInt(32000) | ||
putShort(2.toShort()) | ||
putShort(16.toShort()) | ||
|
||
put('d'.code.toByte()) | ||
put('a'.code.toByte()) | ||
put('t'.code.toByte()) | ||
put('a'.code.toByte()) | ||
|
||
putInt(totalLength - 44) | ||
position(0) | ||
}.also { | ||
val bytes = ByteArray(it.limit()) | ||
it.get(bytes) | ||
return bytes | ||
} | ||
} |