Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'application'
}
group 'com.ps'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
jcenter()
}
dependencies {
//implementation 'com.github.prokash-sarkar:vosktest:v1.0.0'
//implementation 'com.github.prokash-sarkar:marytest:v1.0.0'
implementation files('libs/vosk-1.0.0-all.jar')
implementation files('libs/mary-1.0.0-all.jar')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
mainClassName = 'RunProject'
shadowJar {
archiveBaseName.set('alchemy')
archiveClassifier.set('all')
archiveVersion.set('1.0.0')
mergeServiceFiles()
}
test {
useJUnitPlatform()
}
package library;
/**
* Created by Prokash Sarkar on March, 2021
* Copyright https://prokashsarkar.com
*/
public class CommandLibrary {
public static final String KEY_FUNNY = "funny";
public static final String KEY_JOKE = "joke";
public static final String KEY_QUOTE = "quote";
public static final String KEY_MOTIVATIONAL = "motivational";
public static final String KEY_PLAY = "play";
public static final String KEY_GAME = "game";
}
package library;
/**
* Created by Prokash Sarkar on March, 2021
* Copyright https://prokashsarkar.com
*/
public class ResponseLibrary {
public static final String OUTPUT_LISTENING = "Listening";
public static final String OUTPUT_PROCESSING = "Processing";
public static final String OUTPUT_COMMAND_NOT_RECOGNIZED = "Sorry I could not understand";
}
import helper.playback.PlaybackHelper;
import helper.recorder.Record;
import helper.recorder.RecordHelper;
import library.CommandLibrary;
import library.ResponseLibrary;
import library.SentenceLibrary;
import processor.Context;
import javax.sound.sampled.AudioInputStream;
/**
* Created by Prokash Sarkar on March, 2021
* Copyright https://prokashsarkar.com
*/
public class RunProject {
private static final Long REC_DURATION_SHORT = 5000L;
private static final Long REC_DURATION_MEDIUM = 10000L;
private static final Long REC_DURATION_LONG = 15000L;
private static final Long SLEEP_DURATION_SHORT = 2000L;
private static final Long SLEEP_DURATION_MEDIUM = 5000L;
private static boolean isProcessRunning = false;
private static Context currentContext = Context.NONE;
private static int currentStep = 1;
public static void main(String[] args) {
start(REC_DURATION_SHORT);
}
private static void start(Long recordingDuration) {
sleep(SLEEP_DURATION_MEDIUM);
if (!isProcessRunning) {
isProcessRunning = true;
playTextToSpeech(ResponseLibrary.OUTPUT_LISTENING);
Record record = RecordHelper.startRecording(recordingDuration, true);
playTextToSpeech(ResponseLibrary.OUTPUT_PROCESSING);
String command = VoskHelper.getTextFromRecord(record);
processCommand(command);
}
}
private static void processCommand(String input) {
switch (currentContext) {
case NONE:
// Tell me a funny joke
if (input.toLowerCase().contains(CommandLibrary.KEY_FUNNY.toLowerCase())
|| input.toLowerCase().contains(CommandLibrary.KEY_JOKE.toLowerCase())) {
playTextToSpeech(SentenceLibrary.getJoke());
isProcessRunning = false;
start(REC_DURATION_SHORT);
// Tell me a motivational quote
} else if (input.toLowerCase().contains(CommandLibrary.KEY_QUOTE.toLowerCase())
|| input.toLowerCase().contains(CommandLibrary.KEY_MOTIVATIONAL.toLowerCase())) {
playTextToSpeech(SentenceLibrary.getQuotes());
isProcessRunning = false;
start(REC_DURATION_SHORT);
// Let's play an antakshari game
} else if (input.toLowerCase().contains(CommandLibrary.KEY_PLAY.toLowerCase())
|| input.toLowerCase().contains(CommandLibrary.KEY_GAME.toLowerCase())) {
playAntakshari(input);
isProcessRunning = false;
start(REC_DURATION_MEDIUM);
} else {
playTextToSpeech(ResponseLibrary.OUTPUT_COMMAND_NOT_RECOGNIZED);
isProcessRunning = false;
start(REC_DURATION_SHORT);
}
break;
case ANTAKSHARI:
playAntakshari(input);
break;
}
}
private static void playTextToSpeech(String output) {
// play tex to speech
MaryHelper maryHelper = new MaryHelper();
AudioInputStream inputStream = maryHelper.getAudioInputStreamFromText(output);
PlaybackHelper.playInputStream(inputStream);
}
/**
* Currently the game has only 2 levels
* @param input of the user
*/
private static void playAntakshari(String input) {
if (currentStep == 1) {
currentContext = Context.ANTAKSHARI;
currentStep++;
playTextToSpeech(SentenceLibrary.START_ANTAKSHARI);
} else if (currentStep > 1 && currentStep < 4) {
String antakshari = SentenceLibrary.getAntakshari(input);
if (antakshari != null) {
currentStep++;
playTextToSpeech(antakshari);
} else {
playTextToSpeech(ResponseLibrary.OUTPUT_COMMAND_NOT_RECOGNIZED);
}
}
if (currentStep == 4) {
currentContext = Context.NONE;
currentStep = 1;
sleep(SLEEP_DURATION_SHORT);
playTextToSpeech(SentenceLibrary.STOP_ANTAKSHARI);
isProcessRunning = false;
start(REC_DURATION_SHORT);
} else {
isProcessRunning = false;
start(REC_DURATION_MEDIUM);
}
}
private static void sleep(Long duration) {
try {
Thread.sleep(duration); //Process after given duration
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package library;
import java.util.Random;
/**
* Created by Prokash Sarkar on March, 2021
* Copyright https://prokashsarkar.com
*/
public class SentenceLibrary {
public static String START_ANTAKSHARI = "Lets start. Sing a song first";
public static String MY_TURN = "My Turn";
public static String YOUR_TURN = "Your Turn";
public static String STOP_ANTAKSHARI = "Great game! I enjoyed playing with you.";
public static String getAntakshari(String input) {
String key_little = "little";
String key_star = "start";
String key_nobody = "nobody";
String key_loves = "loves";
String[] songs = new String[]{
"Every night in my dreams, I see you, I feel you, That is how I know you go on",
"Nobody loves me like you, Nobody loves me like you",
"Twinkle, twinkle, little star. How I wonder what you are",
"Will you still love me When I'm no longer, young and beautiful? Will you still love me When I got nothing but my aching soul?",
};
if (input.toLowerCase().contains(key_little.toLowerCase()) || input.toLowerCase().contains(key_star.toLowerCase())) {
return songs[0];
} else if (input.toLowerCase().contains(key_nobody.toLowerCase()) || input.toLowerCase().contains(key_loves.toLowerCase())) {
return songs[3];
} else
return null;
}
public static String getQuotes() {
String[] quotes = new String[]{
"The greatest glory in living lies not in never falling, but in rising every time we fall. Nelson Mandela",
"The way to get started is to quit talking and begin doing. Walt Disney",
"Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma which is living with the results of other people's thinking. Steve Jobs",
"If life were predictable it would cease to be life, and be without flavor. Eleanor Roosevelt",
"If you look at what you have in life, you'll always have more. If you look at what you don't have in life, you'll never have enough. Oprah Winfrey"
};
Random r = new Random();
int randomNumber = r.nextInt(quotes.length);
return quotes[randomNumber];
}
public static String getJoke() {
String[] jokes = new String[]{
"What did the shark say when he ate the clownfish? This tastes a little funny.",
"I invented a new word! Plagiarism",
"Hear about the new restaurant called Karma? There’s no menu: You get what you deserve.",
"Why don’t scientists trust atoms? Because they make up everything.",
"How does Moses make tea? He brews"
};
Random r = new Random();
int randomNumber = r.nextInt(jokes.length);
return jokes[randomNumber];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment