Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature to skip a phrase on a recording session. #52

Merged
merged 6 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions backend/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,39 @@ class AudioAPI:

def save_audio(self, audio: bytes, uuid: str, prompt: str):
user_audio_dir = AudioFS.get_audio_path(uuid)
os.makedirs(user_audio_dir, exist_ok=True)
wav_file_id = AudioFS.create_file_name(prompt)
path = os.path.join(user_audio_dir, wav_file_id)

if prompt[0:13] == "___SKIPPED___":
krisgesling marked this conversation as resolved.
Show resolved Hide resolved
res = DB.skipPhrase(uuid)

try:
# save wav file. This step is needed before trimming.
AudioFS.save_audio(path, audio)
AudioFS.save_meta_data(user_audio_dir, uuid, wav_file_id, prompt)
# Save skipped phrase to textfile
AudioFS.save_skipped_data(user_audio_dir,uuid,prompt)
return response(True)
else:
os.makedirs(user_audio_dir, exist_ok=True)
wav_file_id = AudioFS.create_file_name(prompt)
path = os.path.join(user_audio_dir, wav_file_id)

# trim silence and save
trimmed_sound = Audio.trim_silence(path)
Audio.save_audio(path, trimmed_sound)
try:
# save wav file. This step is needed before trimming.
AudioFS.save_audio(path, audio)
AudioFS.save_meta_data(user_audio_dir, uuid, wav_file_id, prompt)

res = DB.save_audio(wav_file_id, prompt, 'english', uuid)
if res.success:
audio_len = Audio.get_audio_len(trimmed_sound)
char_len = len(prompt)
res = DB.update_user_metrics(uuid, audio_len, char_len)
# trim silence and save
trimmed_sound = Audio.trim_silence(path)
Audio.save_audio(path, trimmed_sound)

res = DB.save_audio(wav_file_id, prompt, 'english', uuid)
if res.success:
return response(True)
return response(False)
except Exception as e:
# TODO: log Exception
print(e)
return response(False)
audio_len = Audio.get_audio_len(trimmed_sound)
char_len = len(prompt)
res = DB.update_user_metrics(uuid, audio_len, char_len)
if res.success:
return response(True)
return response(False)
except Exception as e:
# TODO: log Exception
print(e)
return response(False)

def get_audio_len(self, audio: bytes):
try:
Expand Down
14 changes: 14 additions & 0 deletions backend/app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ def update_user_metrics(uuid: str, time: float, char_len: int) -> response:
print(e)
response(False)

@staticmethod
def skipPhrase(uuid: str) -> response:
thorstenMueller marked this conversation as resolved.
Show resolved Hide resolved
try:
query = UserModel \
.update(
prompt_num=UserModel.prompt_num + 1,
) \
.where(uuid == uuid)
query.execute()
return response(True)
except Exception as e:
print(e)
response(False)

@staticmethod
def save_audio(audio_id: str, prompt: str,
language: str, uuid: str) -> response:
Expand Down
11 changes: 11 additions & 0 deletions backend/app/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class AudioFS:
@staticmethod
def save_audio(path: str, audio: bytes):
webm_file_name = path + ".webm"

with open(webm_file_name, 'wb+') as f:
f.write(audio)

subprocess.call(
'ffmpeg -i {} -ab 160k -ac 2 -ar 44100 -vn {}.wav -y'.format(
webm_file_name, path
Expand All @@ -65,6 +67,15 @@ def save_meta_data(user_audio_dir, uuid, wav_file_id, prompt):
with open(path, 'a') as f:
f.write(data)

@staticmethod
def save_skipped_data(user_audio_dir, uuid, prompt):
path = os.path.join(user_audio_dir, '%s-skipped.txt' % uuid)
data = "{}\n".format(prompt[13:len(prompt)+13])
krisgesling marked this conversation as resolved.
Show resolved Hide resolved

with open(path, 'a') as f:
f.write(data)


@staticmethod
def get_audio_path(uuid: str) -> str:
return os.path.join(audio_dir, uuid)
Expand Down
27 changes: 26 additions & 1 deletion frontend/src/App/Record.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Record extends Component {
<div className="indicator-container">
{this.state.shouldRecord
? "Read Now [Esc] to cancel"
: "[Spacebar] to Start Recording [R] to review [->] for next"}
: "[Spacebar] to Start Recording [R] to review [S] to skip [->] for next"}
</div>
<div id="controls">
<a
Expand Down Expand Up @@ -237,6 +237,11 @@ class Record extends Component {
});
}

// skip current phrase (S)
if (event.keyCode === 83) {
this.skipCurrent();
}

// play wav
if (event.keyCode === 82) {
this.playWav();
Expand Down Expand Up @@ -282,6 +287,26 @@ class Record extends Component {
}
};

skipCurrent = () => {
// prompt_num in DB um 1 erhöhen und Textfile <uuid>-skipped.txt anlegen und Sätze dort protokollieren
krisgesling marked this conversation as resolved.
Show resolved Hide resolved
postAudio("", "___SKIPPED___" + this.state.prompt, this.uuid)
.then(res => res.json())
.then(res => {
if (res.success) {
this.setState({ displayWav: false });
this.requestPrompts(this.uuid);
this.requestUserDetails(this.uuid);
this.setState({
blob: undefined,
audioLen: 0
});
} else {
alert("There was an error in saving that audio");
}
})
.catch(err => console.log(err));
};

silenceDetection = stream => {
const options = {
interval: "150",
Expand Down