Skip to content

Commit

Permalink
updated android and ios sample code with demo videos
Browse files Browse the repository at this point in the history
  • Loading branch information
vilassn committed Oct 22, 2023
1 parent 8219bce commit 07ac227
Show file tree
Hide file tree
Showing 7,363 changed files with 1,698,107 additions and 2 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 0 additions & 1 deletion whisper_android
Submodule whisper_android deleted from afd127
107 changes: 107 additions & 0 deletions whisper_android/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

# Offline Speech Recognition with OpenAI Whisper and TensorFlow Lite

This guide explains how to integrate Whisper and Recorder class in Android apps for audio recording and speech recognition.

## Whisper ASR Integration Guide

Here are separate code snippets for using `Whisper` and `Recorder`:

### Whisper (Speech Recognition)

**Initialization and Configuration:**
```java
// Initialize Whisper
Whisper mWhisper = new Whisper(this); // Create Whisper instance

// Load model and vocabulary for Whisper
String modelPath = getFilePath("whisper-tiny.tflite"); // Provide model file path
String vocabPath = getFilePath("filters_vocab_multilingual.bin"); // Provide vocabulary file path
mWhisper.loadModel(modelPath, vocabPath, true); // Load model and set multilingual mode

// Set a listener for Whisper to handle updates and results
mWhisper.setListener(new IWhisperListener() {
@Override
public void onUpdateReceived(String message) {
// Handle Whisper status updates
}

@Override
public void onResultReceived(String result) {
// Handle transcribed results
}
});
```

**Transcription:**
```java
// Set the audio file path for transcription. Audio format should be in 16K, mono, 16bits
String waveFilePath = getFilePath("your_audio_file.wav"); // Provide audio file path
mWhisper.setFilePath(waveFilePath); // Set audio file path

// Start transcription
mWhisper.setAction(Whisper.ACTION_TRANSCRIBE); // Set action to transcription
mWhisper.start(); // Start transcription

// Perform other operations
// Add your additional code here

// Stop transcription
mWhisper.stop(); // Stop transcription
```

### Recorder (Audio Recording)

**Initialization and Configuration:**
```java
// Initialize Recorder
Recorder mRecorder = new Recorder(this); // Create Recorder instance

// Set a listener for Recorder to handle updates and audio data
mRecorder.setListener(new IRecorderListener() {
@Override
public void onUpdateReceived(String message) {
// Handle Recorder status updates
}

@Override
public void onDataReceived(float[] samples) {
// Handle audio data received during recording
// You can forward this data to Whisper for live recognition using writeBuffer()
// mWhisper.writeBuffer(samples);
}
});
```

**Recording:**
```java
// Check and request recording permissions
checkRecordPermission(); // Check and request recording permissions

// Set the audio file path for recording. It record audio in 16K, mono, 16bits format
String waveFilePath = getFilePath("your_audio_file.wav"); // Provide audio file path
mRecorder.setFilePath(waveFilePath); // Set audio file path

// Start recording
mRecorder.start(); // Start recording

// Perform other operations
// Add your additional code here

// Stop recording
mRecorder.stop(); // Stop recording
```

Please adapt these code snippets to your specific use case, provide the correct file paths, and handle exceptions appropriately in your application.

**Note**: Ensure that you have the necessary permissions, error handling, and file path management in your application when using the `Recorder` class.

## Important Note

Whisper ASR is a powerful tool for transcribing speech into text. However, keep in mind that handling audio data and transcriptions may require careful synchronization and error handling in your Android application to ensure a smooth user experience.

## Demo Video
[![Video](https://img.youtube.com/vi/zGxJeFZW0cI/0.jpg)](https://www.youtube.com/watch?v=zGxJeFZW0cI)


Enjoy using the Whisper ASR Android app to enhance your speech recognition capabilities!
Binary file added whisper_android/Screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added whisper_android/WhisperASR.apk
Binary file not shown.
18 changes: 18 additions & 0 deletions whisper_android/WhisperAndroid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
*.iml
.gradle
/local.properties
.idea
/.idea
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
app/src/main/cpp/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions whisper_android/WhisperAndroid/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
66 changes: 66 additions & 0 deletions whisper_android/WhisperAndroid/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
signingConfigs {
release {
}
}
namespace 'com.whispertflite'
compileSdk 34

defaultConfig {
applicationId "com.whispertflite"
minSdk 26
targetSdk 34
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

// Specify the ABIs to exclude
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = '11'
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:1.12.0'

implementation 'org.tensorflow:tensorflow-lite:2.11.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.4.3'
// implementation 'org.tensorflow:tensorflow-lite-api:2.11.0'
// implementation 'org.tensorflow:tensorflow-lite-support-api:0.4.3'
// implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:2.11.0'

// implementation 'org.tensorflow:tensorflow-lite-gpu:2.11.0'
// implementation 'org.tensorflow:tensorflow-lite-gpu-api:2.11.0'

// implementation files('libs/jlibrosa-1.1.8-SNAPSHOT-jar-with-dependencies.jar')
}
21 changes: 21 additions & 0 deletions whisper_android/WhisperAndroid/app/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
25 changes: 25 additions & 0 deletions whisper_android/WhisperAndroid/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.whispertflite">

<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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TFLiteAudio"
tools:targetApi="31">
<activity android:name="com.whispertflite.MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Loading

0 comments on commit 07ac227

Please sign in to comment.