forked from openlearnapp/openlearnapp.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-audio.sh
More file actions
executable file
Β·359 lines (302 loc) Β· 10.7 KB
/
Copy pathgenerate-audio.sh
File metadata and controls
executable file
Β·359 lines (302 loc) Β· 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
# Generate audio files for YAML lessons
# Uses yq for YAML parsing, macOS 'say' for TTS, and ffmpeg for MP3 conversion
#
# New folder-based structure:
# lessons/{learning}/{teaching}/{lesson-folder}/content.yaml
# lessons/{learning}/{teaching}/{lesson-folder}/audio/ (output)
#
# Usage:
# ./generate-audio.sh # Generate all built-in lessons
# ./generate-audio.sh -f # Force regenerate all
# ./generate-audio.sh path/to/lesson-folder/ # Generate single lesson
# ./generate-audio.sh -f path/to/lesson-folder/ # Force single lesson
#
# Works with external workshops too β auto-detects the content root
# by walking up to find index.yaml:
# ./generate-audio.sh /path/to/workshop/deutsch/topic/01-lesson/
LESSONS_DIR="public/lessons"
FORCE_REGENERATE=false
SINGLE_LESSON=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-f|--force)
FORCE_REGENERATE=true
shift
;;
*)
SINGLE_LESSON="$1"
shift
;;
esac
done
# Voice mapping by language code
declare -A VOICES
VOICES["de-DE"]="Anna" # German
VOICES["de"]="Anna"
VOICES["pt-PT"]="Joana" # Portuguese (Portugal)
VOICES["pt"]="Joana"
VOICES["en-US"]="Samantha" # English
VOICES["en"]="Samantha"
VOICES["es-ES"]="MΓ³nica" # Spanish
VOICES["es"]="MΓ³nica"
echo "π΅ Generating audio files for lessons..."
if [[ "$FORCE_REGENERATE" == true ]]; then
echo " π Force mode: regenerating existing files"
fi
echo ""
# Check dependencies
if ! command -v yq &> /dev/null; then
echo "β Error: yq is not installed"
echo " Install with: brew install yq"
exit 1
fi
if ! command -v ffmpeg &> /dev/null; then
echo "β οΈ Warning: ffmpeg not found. Audio will be saved as AIFF instead of MP3"
echo " Install with: brew install ffmpeg"
HAS_FFMPEG=false
else
HAS_FFMPEG=true
fi
# Function to get language code from YAML file
get_language_code() {
local yaml_file="$1"
local folder_name="$2"
if [[ ! -f "$yaml_file" ]]; then
echo ""
return
fi
# Try to find the code for this folder
local code=$(yq eval ".languages[] | select(.folder == \"$folder_name\") | .code" "$yaml_file" 2>/dev/null)
if [[ -z "$code" || "$code" == "null" ]]; then
code=$(yq eval ".topics[] | select(.folder == \"$folder_name\") | .code" "$yaml_file" 2>/dev/null)
fi
echo "$code"
}
# Function to get voice for a language
get_voice() {
local folder_name="$1"
local parent_dir="$2"
local content_root="${3:-$LESSONS_DIR}"
# Try to get code from parent's topics.yaml or index.yaml
local code=""
if [[ -f "$parent_dir/topics.yaml" ]]; then
code=$(get_language_code "$parent_dir/topics.yaml" "$folder_name")
fi
if [[ -z "$code" || "$code" == "null" ]] && [[ -f "$parent_dir/index.yaml" ]]; then
code=$(get_language_code "$parent_dir/index.yaml" "$folder_name")
fi
# Try main index.yaml
if [[ -z "$code" || "$code" == "null" ]]; then
code=$(get_language_code "$content_root/index.yaml" "$folder_name")
fi
# Look up voice by code
if [[ -n "$code" && "$code" != "null" ]]; then
local voice="${VOICES[$code]}"
if [[ -n "$voice" ]]; then
echo "$voice"
return
fi
fi
# Fallback: try folder name directly (legacy support)
local voice="${VOICES[$folder_name]}"
if [[ -n "$voice" ]]; then
echo "$voice"
return
fi
# Default fallback
echo "Alex"
}
# Function to find the content root by walking up to find index.yaml
find_content_root() {
local dir="$1"
while [[ "$dir" != "/" ]]; do
if [[ -f "$dir/index.yaml" ]]; then
echo "$dir"
return
fi
dir=$(dirname "$dir")
done
echo ""
}
# Function to process a single lesson folder
process_lesson() {
local lesson_folder="$1"
local lesson_file="$lesson_folder/content.yaml"
# Check if content.yaml exists
if [[ ! -f "$lesson_file" ]]; then
echo "β οΈ Warning: No content.yaml found in $lesson_folder"
return
fi
# Resolve absolute path for reliable path extraction
local abs_folder=$(cd "$lesson_folder" && pwd)
# Auto-detect content root if lesson is outside LESSONS_DIR
local effective_root="$LESSONS_DIR"
if [[ ! "$abs_folder" == "$(cd "$LESSONS_DIR" 2>/dev/null && pwd)"* ]]; then
local detected_root=$(find_content_root "$abs_folder")
if [[ -n "$detected_root" ]]; then
effective_root="$detected_root"
fi
fi
# Extract path components
local rel_path="${abs_folder#$effective_root/}"
local learning=$(echo "$rel_path" | cut -d'/' -f1)
local teaching=$(echo "$rel_path" | cut -d'/' -f2)
local folder_name=$(basename "$abs_folder")
echo "π Processing: $learning/$teaching/$folder_name"
# Output directory is inside the lesson folder
local audio_dir="$lesson_folder/audio"
mkdir -p "$audio_dir"
# Get voices for this lesson
local teaching_voice=$(get_voice "$teaching" "$effective_root/$learning" "$effective_root")
local learning_voice=$(get_voice "$learning" "$effective_root" "$effective_root")
if [[ "$teaching_voice" == "Alex" ]]; then
echo " β οΈ No voice found for '$teaching', using default"
fi
if [[ "$learning_voice" == "Alex" ]]; then
echo " β οΈ No voice found for '$learning', using default"
fi
local files_generated=0
local files_skipped=0
# Extract and generate title audio (in base/learning language)
local title=$(yq eval '.title' "$lesson_file")
if [[ "$title" != "null" && -n "$title" ]]; then
local final_file="$audio_dir/title.mp3"
if [[ "$FORCE_REGENERATE" == true ]] || [[ ! -f "$final_file" ]]; then
echo " ποΈ Title: $title"
local temp_file="$audio_dir/title.aiff"
say -v "$learning_voice" "$title" -o "$temp_file" 2>/dev/null
if [[ "$HAS_FFMPEG" == true ]]; then
ffmpeg -i "$temp_file" -codec:a libmp3lame -qscale:a 2 "$final_file" -y 2>/dev/null
rm "$temp_file"
else
mv "$temp_file" "${temp_file%.aiff}.aiff"
fi
((files_generated++))
else
((files_skipped++))
fi
fi
# Process sections and examples
local section_count=$(yq eval '.sections | length' "$lesson_file")
for ((s=0; s<section_count; s++)); do
# Generate section title audio (in topic/teaching language)
local section_title=$(yq eval ".sections[$s].title" "$lesson_file")
if [[ "$section_title" != "null" && -n "$section_title" ]]; then
local final_section="$audio_dir/$s-title.mp3"
if [[ "$FORCE_REGENERATE" == true ]] || [[ ! -f "$final_section" ]]; then
local temp_section="$audio_dir/$s-title.aiff"
say -v "$teaching_voice" "$section_title" -o "$temp_section" 2>/dev/null
if [[ "$HAS_FFMPEG" == true ]]; then
ffmpeg -i "$temp_section" -codec:a libmp3lame -qscale:a 2 "$final_section" -y 2>/dev/null
rm "$temp_section"
fi
((files_generated++))
else
((files_skipped++))
fi
fi
local example_count=$(yq eval ".sections[$s].examples | length" "$lesson_file")
for ((e=0; e<example_count; e++)); do
# Get question and answer
local question=$(yq eval ".sections[$s].examples[$e].q" "$lesson_file")
local answer=$(yq eval ".sections[$s].examples[$e].a" "$lesson_file")
if [[ "$question" != "null" && -n "$question" ]]; then
# Generate question audio (in teaching language)
local final_q="$audio_dir/$s-$e-q.mp3"
if [[ "$FORCE_REGENERATE" == true ]] || [[ ! -f "$final_q" ]]; then
local temp_q="$audio_dir/$s-$e-q.aiff"
say -v "$teaching_voice" "$question" -o "$temp_q" 2>/dev/null
if [[ "$HAS_FFMPEG" == true ]]; then
ffmpeg -i "$temp_q" -codec:a libmp3lame -qscale:a 2 "$final_q" -y 2>/dev/null
rm "$temp_q"
fi
((files_generated++))
else
((files_skipped++))
fi
fi
if [[ "$answer" != "null" && -n "$answer" ]]; then
# Generate answer audio (in learning language)
local final_a="$audio_dir/$s-$e-a.mp3"
if [[ "$FORCE_REGENERATE" == true ]] || [[ ! -f "$final_a" ]]; then
local temp_a="$audio_dir/$s-$e-a.aiff"
say -v "$learning_voice" "$answer" -o "$temp_a" 2>/dev/null
if [[ "$HAS_FFMPEG" == true ]]; then
ffmpeg -i "$temp_a" -codec:a libmp3lame -qscale:a 2 "$final_a" -y 2>/dev/null
rm "$temp_a"
fi
((files_generated++))
else
((files_skipped++))
fi
fi
done
done
# Generate manifest.yaml listing all audio files
local manifest_file="$audio_dir/manifest.yaml"
echo "files:" > "$manifest_file"
for mp3 in "$audio_dir"/*.mp3; do
if [[ -f "$mp3" ]]; then
echo " - $(basename "$mp3")" >> "$manifest_file"
fi
done
echo " π Written $manifest_file"
# Report results
if [[ $files_skipped -gt 0 ]]; then
echo " β
Generated $files_generated, skipped $files_skipped existing files"
else
echo " β
Generated $files_generated audio files"
fi
echo ""
}
# Main execution
if [[ -n "$SINGLE_LESSON" ]]; then
# Process single lesson folder
# Remove trailing slash if present
SINGLE_LESSON="${SINGLE_LESSON%/}"
if [[ ! -d "$SINGLE_LESSON" ]]; then
echo "β Error: Directory not found: $SINGLE_LESSON"
exit 1
fi
if [[ ! -f "$SINGLE_LESSON/content.yaml" ]]; then
echo "β Error: No content.yaml found in $SINGLE_LESSON"
echo " This should be a lesson folder containing content.yaml"
exit 1
fi
process_lesson "$SINGLE_LESSON"
echo "ββββββββββββββββββββββββββββββββββββββββ"
echo "β
Complete!"
echo " Audio files saved in: $SINGLE_LESSON/audio/"
else
# Process all lessons (find all content.yaml files)
total_files=0
processed_files=0
while IFS= read -r -d '' content_file; do
# Get the lesson folder (parent directory of content.yaml)
lesson_folder=$(dirname "$content_file")
((total_files++))
process_lesson "$lesson_folder"
((processed_files++))
done < <(find "$LESSONS_DIR" -name "content.yaml" -type f -print0)
echo "ββββββββββββββββββββββββββββββββββββββββ"
echo "β
Complete!"
echo " Processed $processed_files lessons"
echo ""
# Show example directory structure
if command -v tree &> /dev/null; then
echo "Example directory structure:"
# Find first lesson folder and show its structure
first_lesson=$(find "$LESSONS_DIR" -name "content.yaml" -type f | head -1 | xargs dirname)
if [[ -n "$first_lesson" ]]; then
tree -L 1 "$first_lesson"
fi
else
echo "Example audio files:"
first_audio=$(find "$LESSONS_DIR" -path "*/audio/*.mp3" -type f | head -5)
if [[ -n "$first_audio" ]]; then
echo "$first_audio"
fi
fi
fi