Added audio recorder files and speech callbacks

This commit is contained in:
Luca Warmenhoven
2024-04-19 14:24:51 +02:00
parent 96ec6b47c3
commit 11517ca2f0
5 changed files with 137 additions and 17 deletions

33
.idea/workspace.xml generated
View File

@@ -15,11 +15,10 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="00599d5b-7eb5-44da-ad7f-98bf42384c16" name="Changes" comment="Changes"> <list default="true" id="00599d5b-7eb5-44da-ad7f-98bf42384c16" name="Changes" comment="Changes">
<change afterPath="$PROJECT_DIR$/src/app/res/activity_main.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/app/src/main/com/fitbot/chat/ISpeechCallback.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/app/src/main/com/fitbot/MainActivity.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/app/src/main/com/fitbot/chat/SpeechGenerator.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/app/src/main/com/fitbot/audio/AudioRecorder.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/AndroidManifest.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/AndroidManifest.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/app/src/main/com/fitbot/audio/AudioRecorder.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/com/fitbot/audio/AudioRecorder.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -33,12 +32,24 @@
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Class" /> <option value="Class" />
<option value="Interface" />
</list> </list>
</option> </option>
</component> </component>
<component name="Git.Merge.Settings">
<option name="BRANCH" value="origin/main" />
</component>
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component> </component>
<component name="GitSEFilterConfiguration">
<file-type-list>
<filtered-out-file-type name="LOCAL_BRANCH" />
<filtered-out-file-type name="REMOTE_BRANCH" />
<filtered-out-file-type name="TAG" />
<filtered-out-file-type name="COMMIT_BY_MESSAGE" />
</file-type-list>
</component>
<component name="ProjectId" id="2fE3N2CwEPDo9wBtexBLxU20tCJ" /> <component name="ProjectId" id="2fE3N2CwEPDo9wBtexBLxU20tCJ" />
<component name="ProjectLevelVcsManager"> <component name="ProjectLevelVcsManager">
<ConfirmationsSetting value="2" id="Add" /> <ConfirmationsSetting value="2" id="Add" />
@@ -56,7 +67,7 @@
<property name="project.structure.last.edited" value="SDKs" /> <property name="project.structure.last.edited" value="SDKs" />
<property name="project.structure.proportion" value="0.1322751" /> <property name="project.structure.proportion" value="0.1322751" />
<property name="project.structure.side.proportion" value="0.3937198" /> <property name="project.structure.side.proportion" value="0.3937198" />
<property name="settings.editor.selected.configurable" value="preferences.lookFeel" /> <property name="settings.editor.selected.configurable" value="preferences.keymap" />
</component> </component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager"> <component name="TaskManager">
@@ -69,6 +80,14 @@
<workItem from="1713352368383" duration="57000" /> <workItem from="1713352368383" duration="57000" />
<workItem from="1713352446871" duration="1766000" /> <workItem from="1713352446871" duration="1766000" />
</task> </task>
<task id="LOCAL-00001" summary="Changes">
<created>1713528225837</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1713528225837</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@@ -86,4 +105,8 @@
</option> </option>
<option name="oldMeFiltersMigrated" value="true" /> <option name="oldMeFiltersMigrated" value="true" />
</component> </component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Changes" />
<option name="LAST_COMMIT_MESSAGE" value="Changes" />
</component>
</project> </project>

View File

@@ -0,0 +1,10 @@
package com.fitbot.audio;
import android.app.Activity;
import android.media.MediaRecorder;
public class AudioAnalyser extends Activity {
}

View File

@@ -1,13 +1,8 @@
package com.fitbot.audio; package com.fitbot.audio;
import android.app.Activity;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import java.io.FileInputStream; public class AudioRecorder {
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
public class AudioRecorder extends Activity {
private MediaRecorder recorder; private MediaRecorder recorder;
@@ -23,13 +18,9 @@ public class AudioRecorder extends Activity {
recorder = new MediaRecorder(); recorder = new MediaRecorder();
File file = new File()
FileInputStream stream = new FileInputStream();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(stream.getFD());
// Attempt to prepare and start the audio recorder // Attempt to prepare and start the audio recorder
try { try {
@@ -40,8 +31,6 @@ public class AudioRecorder extends Activity {
e.printStackTrace(); e.printStackTrace();
} }
recorder.set
return this; return this;
} }
@@ -54,4 +43,5 @@ public class AudioRecorder extends Activity {
recorder = null; recorder = null;
} }
} }

View File

@@ -0,0 +1,8 @@
package com.fitbot.speech;
/**
* Interface for a callback that is called when speech is generated
*/
public interface ISpeechCallback {
void onSpeechGenerated(String speech);
}

View File

@@ -0,0 +1,89 @@
package com.fitbot.speech;
import java.util.Arrays;
import java.util.Locale;
public class SpeechGenerator {
private ISpeechCallback speechCallback = (content) -> {}; // Default empty callback
private StringBuilder speechBuffer = new StringBuilder(); // Buffer to store the speech content
private String languageIso639_1; // ISO 639-1 language code
/**
* Constructor that sets the language of the speech to be generated
* @param languageIso639_1 The ISO 639-1 language code, e.g. "en" for English
*/
public SpeechGenerator(String languageIso639_1)
{
if ( !Arrays.asList(Locale.getISOLanguages()).contains(languageIso639_1) )
throw new IllegalArgumentException("Invalid language code");
this.languageIso639_1 = languageIso639_1;
}
/**
* Default constructor, sets the language to English
*/
public SpeechGenerator()
{
this("en");
}
/**
* Set the language of the speech to be generated
* @param languageIso639_1 The ISO 639-1 language code, e.g. "en" for English
* @return An instance of the SpeechGenerator class
*/
public SpeechGenerator language(String languageIso639_1)
{
this.languageIso639_1 = languageIso639_1;
return this;
}
/**
* Appends the provided text to the speech buffer
* @param text The text to append
* @return An instance of the SpeechGenerator class
*/
public SpeechGenerator print(String text)
{
speechBuffer.append(text);
return this;
}
/**
* Appends the provided text to the speech buffer, followed by a newline character
* @param text The text to append
* @return An instance of the SpeechGenerator class
*/
public SpeechGenerator println(String text)
{
return print(text).print("\n");
}
/**
* Get the contents of the speech buffer
*/
public String getBuffer()
{
return speechBuffer.toString();
}
/**
* Clear the contents of the speech buffer
*/
public void clearBuffer()
{
speechBuffer = new StringBuilder();
}
/**
* Print the contents of the speech buffer to the console
*/
public void speak()
{
speechCallback.onSpeechGenerated(speechBuffer.toString());
}
}