Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
botbahlul committed Feb 20, 2023
1 parent bcb81fc commit 8d4e86d
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 342 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0.1"
versionName "1.0.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/org/vosk/livesubtitle/Decompress.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.util.zip.ZipInputStream;

public class Decompress {
private String _zipFile;
private String _extract_location;
private final String _zipFile;
private final String _extract_location;

public Decompress(String zipFile, String extract_location) {
_zipFile = zipFile;
Expand All @@ -26,9 +26,9 @@ public void unzip() {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);

byte b[] = new byte[1024];
byte[] b = new byte[1024];

ZipEntry ze = null;
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());

Expand Down Expand Up @@ -60,8 +60,8 @@ public void unzip() {
private void _dirChecker(String dir) {
File f = new File(_extract_location + dir);

if(!f.isDirectory()) {
f.mkdirs();
if(!f.isDirectory() && f.mkdirs()) {
Log.d(f.toString(), "created");
}
}
}
641 changes: 359 additions & 282 deletions app/src/main/java/org/vosk/livesubtitle/MainActivity.java

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions app/src/main/java/org/vosk/livesubtitle/VOSK_MODEL.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ public class VOSK_MODEL {
public static String ISO_CODE;
public static String URL_ADDRESS;
public static String ZIP_FILENAME;
public static int ZIP_FILESIZE;
public static int ZIP_FILE_SIZE;
public static String USED_PATH;
public static String ZIP_FILE_COMPLETE_PATH;
public static String SAVE_AS;
public static String EXTRACTED_PATH;
public static boolean DOWNLOADED;
}
33 changes: 19 additions & 14 deletions app/src/main/java/org/vosk/livesubtitle/VoskVoiceRecognizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.vosk.android.StorageService;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
if (MainActivity.voice_text != null) {
//get_translation(MainActivity.voice_text.getText(), LANGUAGE.SRC, LANGUAGE.DST);
if (MainActivity.voice_text.length() > 0) {
MainActivity.textview_debug2.setText(mlkit_status_message);
MainActivity.textview_output_messages2.setText(mlkit_status_message);
}
if (TRANSLATION_TEXT.STRING.length() == 0) {
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -144,9 +145,11 @@ public void run() {
timer.schedule(timerTask,0,1000);
}
else {
timerTask.cancel();
timer.cancel();
timer.purge();
if (timerTask != null) timerTask.cancel();
if (timer != null) {
timer.cancel();
timer.purge();
}
}
}

Expand All @@ -158,7 +161,7 @@ private void initModel() {
}

private void initDownloadedModel() {
if (VOSK_MODEL.DOWNLOADED) {
if (new File(VOSK_MODEL.EXTRACTED_PATH + VOSK_MODEL.ISO_CODE).exists()) {
model = new Model(VOSK_MODEL.USED_PATH);
recognizeMicrophone();
} else {
Expand All @@ -169,9 +172,9 @@ private void initDownloadedModel() {
String hints = "Recognized words";
MainActivity.voice_text.setHint(hints);
stopSelf();
String msg = "You need to download the model first";
String msg = "You have to download the model first";
//toast(msg);
setText(MainActivity.textview_debug, msg);
setText(MainActivity.textview_output_messages, msg);
}
}

Expand All @@ -187,9 +190,11 @@ public void onDestroy() {
speechStreamService.stop();
speechStreamService = null;
}
timerTask.cancel();
timer.cancel();
timer.purge();
if (timerTask != null) timerTask.cancel();
if (timer != null) {
timer.cancel();
timer.purge();
}
}

@Override
Expand Down Expand Up @@ -272,7 +277,7 @@ public void onTimeout() {
}

private void setErrorState(String message) {
MainActivity.textview_debug.setText(message);
MainActivity.textview_output_messages.setText(message);
if (speechService != null) speechService.startListening(this);
}

Expand All @@ -285,7 +290,7 @@ private void recognizeMicrophone() {
OVERLAYING_STATUS.STRING = "OVERLAYING_STATUS.IS_OVERLAYING = " + OVERLAYING_STATUS.IS_OVERLAYING;
MainActivity.textview_overlaying.setText(OVERLAYING_STATUS.STRING);
} else {
MainActivity.textview_debug.setText("");
MainActivity.textview_output_messages.setText("");
try {
Recognizer rec = new Recognizer(model, 16000.0f);
speechService = new SpeechService(rec, 16000.0f);
Expand Down Expand Up @@ -413,7 +418,7 @@ public void onCompleted(String text) {
@Override
public void onError(Exception e) {
//toast("Unknown error");
setText(MainActivity.textview_debug, e.getMessage());
setText(MainActivity.textview_output_messages, e.getMessage());
}
});
translate.execute(t, src, dst);
Expand Down Expand Up @@ -447,7 +452,7 @@ public void onCompleted(String translation) {
@Override
public void onError(Exception e) {
//toast("Unknown error");
setText(MainActivity.textview_debug, e.getMessage());
setText(MainActivity.textview_output_messages, e.getMessage());
}
});
translate.execute(t, src, dst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import android.view.View;
import android.widget.ImageView;

import java.io.File;
import java.util.Objects;

public class create_overlay_mic_button extends Service{

public create_overlay_mic_button() {}
Expand Down Expand Up @@ -45,7 +48,7 @@ public void onDestroy() {
OVERLAYING_STATUS.IS_OVERLAYING = false;
OVERLAYING_STATUS.STRING = "OVERLAYING_STATUS.IS_OVERLAYING = " + OVERLAYING_STATUS.IS_OVERLAYING;
MainActivity.textview_overlaying.setText(OVERLAYING_STATUS.STRING);
MainActivity.textview_debug.setText("");
MainActivity.textview_output_messages.setText("");
VOICE_TEXT.STRING = "";
TRANSLATION_TEXT.STRING = "";
MainActivity.voice_text.setText("");
Expand All @@ -70,14 +73,13 @@ public void onDestroy() {
create_overlay_mic_button.mic_button.setVisibility(View.INVISIBLE);
}
}
MainActivity.textview_debug.setText("");
VOICE_TEXT.STRING = "";
TRANSLATION_TEXT.STRING = "";
MainActivity.voice_text.setText("");
String string_recognizing = "recognizing=" + RECOGNIZING_STATUS.IS_RECOGNIZING;
MainActivity.textview_recognizing.setText(string_recognizing);
String string_overlaying = "overlaying=" + OVERLAYING_STATUS.IS_OVERLAYING;
MainActivity.textview_overlaying.setText(string_overlaying);
RECOGNIZING_STATUS.STRING = "RECOGNIZING_STATUS.IS_RECOGNIZING = " + RECOGNIZING_STATUS.IS_RECOGNIZING;
MainActivity.textview_recognizing.setText(RECOGNIZING_STATUS.STRING);
OVERLAYING_STATUS.STRING = "OVERLAYING_STATUS.IS_OVERLAYING = " + OVERLAYING_STATUS.IS_OVERLAYING;
MainActivity.textview_overlaying.setText(OVERLAYING_STATUS.STRING);
MainActivity.voice_text.setHint(hints);
}

Expand All @@ -98,12 +100,12 @@ private void create_mic_button() {
//new View.OnClickListener() {
v -> {
RECOGNIZING_STATUS.IS_RECOGNIZING = !RECOGNIZING_STATUS.IS_RECOGNIZING;
String string_recognizing = "recognizing=" + RECOGNIZING_STATUS.IS_RECOGNIZING;
MainActivity.textview_recognizing.setText(string_recognizing);
RECOGNIZING_STATUS.STRING = "RECOGNIZING_STATUS.IS_RECOGNIZING = " + RECOGNIZING_STATUS.IS_RECOGNIZING;
MainActivity.textview_recognizing.setText(RECOGNIZING_STATUS.STRING);
if (!RECOGNIZING_STATUS.IS_RECOGNIZING) {
stop_vosk_voice_recognizer();
mic_button.setImageResource(R.drawable.ic_mic_black_off);
MainActivity.textview_debug.setText("");
MainActivity.textview_output_messages.setText("");
VOICE_TEXT.STRING = "";
TRANSLATION_TEXT.STRING = "";
MainActivity.voice_text.setText("");
Expand Down Expand Up @@ -136,18 +138,52 @@ private void create_mic_button() {
startActivity(intent);
}

start_vosk_voice_recognizer();
mic_button.setImageResource(R.drawable.ic_mic_black_on);
MainActivity.textview_debug.setText(R.string.say_something);
if (TRANSLATION_TEXT.STRING.length() == 0) {
if (create_overlay_translation_text.overlay_translation_text != null) {
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.INVISIBLE);
if (Objects.equals(VOSK_MODEL.ISO_CODE, "en-US")) {
start_vosk_voice_recognizer();
mic_button.setImageResource(R.drawable.ic_mic_black_on);
MainActivity.textview_output_messages.setText(R.string.say_something);
if (TRANSLATION_TEXT.STRING.length() == 0) {
if (create_overlay_translation_text.overlay_translation_text != null) {
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.INVISIBLE);
}
} else {
if (create_overlay_translation_text.overlay_translation_text != null) {
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.VISIBLE);
create_overlay_translation_text.overlay_translation_text.setVisibility(View.VISIBLE);
}
}
}
else {
if (new File(VOSK_MODEL.EXTRACTED_PATH + VOSK_MODEL.ISO_CODE).exists()) {
start_vosk_voice_recognizer();
mic_button.setImageResource(R.drawable.ic_mic_black_on);
MainActivity.textview_output_messages.setText(R.string.say_something);
if (TRANSLATION_TEXT.STRING.length() == 0) {
if (create_overlay_translation_text.overlay_translation_text != null) {
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.INVISIBLE);
}
} else {
if (create_overlay_translation_text.overlay_translation_text != null) {
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.VISIBLE);
create_overlay_translation_text.overlay_translation_text.setVisibility(View.VISIBLE);
}
}
}
} else {
if (create_overlay_translation_text.overlay_translation_text != null) {
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.VISIBLE);
create_overlay_translation_text.overlay_translation_text.setVisibility(View.VISIBLE);
else {
String msg = "You have to download the model first";
MainActivity.textview_output_messages.setText(msg);

RECOGNIZING_STATUS.IS_RECOGNIZING = false;
RECOGNIZING_STATUS.STRING = "RECOGNIZING_STATUS.IS_RECOGNIZING = " + RECOGNIZING_STATUS.IS_RECOGNIZING;
MainActivity.textview_recognizing.setText(RECOGNIZING_STATUS.STRING);

OVERLAYING_STATUS.IS_OVERLAYING = false;
OVERLAYING_STATUS.STRING = "OVERLAYING_STATUS.IS_OVERLAYING = " + OVERLAYING_STATUS.IS_OVERLAYING;
MainActivity.textview_overlaying.setText(OVERLAYING_STATUS.STRING);

stopSelf();
}
}
}
Expand Down
23 changes: 7 additions & 16 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
/>
<CheckBox
android:id="@+id/checkbox_debug_mode"
android:text="Debug mode"
android:text="@string/debug_mode"
android:minHeight="32dp"
android:padding="0dp"
android:gravity="start|center"
android:textAlignment="gravity"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
Expand Down Expand Up @@ -117,7 +117,7 @@
android:visibility="invisible"
/>
<TextView
android:id="@+id/textview_filesize"
android:id="@+id/textview_server_response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
Expand All @@ -126,7 +126,7 @@
android:visibility="invisible"
/>
<TextView
android:id="@+id/textview_bytesdownloaded"
android:id="@+id/textview_file_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
Expand All @@ -135,12 +135,13 @@
android:visibility="invisible"
/>
<TextView
android:id="@+id/textview_downloaded_status"
android:id="@+id/textview_bytes_downloaded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="gravity"
android:textSize="12sp"
android:visibility="invisible"
/>
<TextView
android:id="@+id/textview_model_used_path"
Expand Down Expand Up @@ -240,17 +241,7 @@
android:textSize="12sp"
/>
<TextView
android:id="@+id/textview_debug"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center"
android:gravity="center"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/textview_debug2"
android:id="@+id/textview_output_messages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
<string name="download_model">Download Model</string>
<string name="delete_model">Delete Model</string>
<string name="recognized_words">Recognized words</string>
<string name="debug_mode">Debug mode</string>
</resources>

0 comments on commit 8d4e86d

Please sign in to comment.