2023-05-18 18:45:38 -05:00
|
|
|
#!/usr/bin/env sh
|
2023-02-18 05:51:03 -06:00
|
|
|
|
2022-12-30 06:11:52 -06:00
|
|
|
txt_to_wav() {
|
2023-02-18 05:51:03 -06:00
|
|
|
hash="$(md5sum "$1" | cut -d ' ' -f 1)"
|
|
|
|
filename="out-$hash.sbc"
|
|
|
|
|
|
|
|
xxd -r -p "$1" > "$filename"
|
|
|
|
|
2023-01-29 10:16:07 -06:00
|
|
|
ffmpeg -y \
|
2023-02-18 05:51:03 -06:00
|
|
|
-v quiet `# verbosity - other options are "quiet", "error", "panic"` \
|
|
|
|
-f sbc `# accept SBC format` \
|
|
|
|
-ac 1 `# audio channel: #1` \
|
|
|
|
-i "$filename" `# input file: out-$hash.sbc` \
|
|
|
|
"$2" `# output to $2`
|
|
|
|
|
|
|
|
rm "$filename"
|
2022-12-30 06:11:52 -06:00
|
|
|
}
|
|
|
|
|
2023-01-29 10:16:07 -06:00
|
|
|
audio_to_txt() {
|
2023-02-18 05:51:03 -06:00
|
|
|
hash="$(md5sum "$1" | cut -d ' ' -f 1)"
|
|
|
|
filename="out-$hash.sbc"
|
|
|
|
|
2023-01-29 10:16:07 -06:00
|
|
|
ffmpeg -y \
|
2023-02-18 05:51:03 -06:00
|
|
|
-v quiet `# verbosity - other options are "quiet", "error", "panic"` \
|
|
|
|
-i "$1" `# input file: $1` \
|
|
|
|
-f sbc `# output format: SBC` \
|
|
|
|
-ar 16000 `# audio rate: 16 kHz` \
|
|
|
|
-ac 1 `# audio channel: #1` \
|
|
|
|
-aq 16 `# audio quality: 16 (for SBC this means bitpool=16)` \
|
|
|
|
-map_metadata -1 `# strip metadata` \
|
|
|
|
"$filename" `# output to out-$hash.sbc`
|
|
|
|
|
|
|
|
xxd -i "$filename" `# output in C include file style` \
|
2023-01-29 10:16:07 -06:00
|
|
|
| head -n -2 `# skip last two xxd outline lines (skip C formatting)` \
|
|
|
|
| tail -n +2 `# start output on line 2 of xxd output (skip C formatting)` \
|
|
|
|
| sed 's/ //g' `# remove spaces` \
|
|
|
|
| tr --delete '\n' `# remove newlines` \
|
|
|
|
| sed 's/,/\,\n/16; P; D' `# collect into lines with the right length` \
|
2023-02-18 05:51:03 -06:00
|
|
|
> "$2"
|
|
|
|
|
|
|
|
rm "$filename"
|
2023-01-29 10:16:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
audio_to_cpp() {
|
2023-02-18 05:51:03 -06:00
|
|
|
varname="$(basename "$1" | rev | cut -f 2- -d '.' | rev | tr '[:lower:]/' '[:upper:]_')"
|
|
|
|
|
2023-01-29 10:16:07 -06:00
|
|
|
ffmpeg -y \
|
2023-02-18 05:51:03 -06:00
|
|
|
-v quiet `# verbosity - other options are "quiet", "error", "panic"` \
|
|
|
|
-i "$1" `# input file: $1` \
|
|
|
|
-f sbc `# output format: SBC` \
|
|
|
|
-ar 16000 `# audio rate: 16 kHz` \
|
|
|
|
-ac 1 `# audio channel: #1` \
|
|
|
|
-aq 16 `# audio quality: 16 (for SBC this means bitpool=16)` \
|
|
|
|
-map_metadata -1 `# strip metadata` \
|
|
|
|
"$varname" `# output to something like EN_SOUND_POWER_ON`
|
|
|
|
|
|
|
|
printf '#include <stdint.h>\n' > "$2"
|
|
|
|
|
|
|
|
xxd -i "$varname" `# output in C include file style` \
|
|
|
|
>> "$2"
|
|
|
|
|
|
|
|
sed -i 's/unsigned char/uint8_t/g' "$2"
|
|
|
|
|
|
|
|
rm "$varname"
|
2022-12-30 06:11:52 -06:00
|
|
|
}
|
|
|
|
|
2023-02-18 05:51:03 -06:00
|
|
|
[ "$1" = '-T' ] || [ "$1" = '--txt-to-wav' ] && shift 1 && txt_to_wav "$@" && exit
|
|
|
|
[ "$1" = '-A' ] || [ "$1" = '--audio-to-txt' ] && shift 1 && audio_to_txt "$@" && exit
|
|
|
|
[ "$1" = '-C' ] || [ "$1" = '--audio-to-cpp' ] && shift 1 && audio_to_cpp "$@" && exit
|
|
|
|
|
2022-12-30 06:11:52 -06:00
|
|
|
echo "
|
|
|
|
Sound format converter:
|
|
|
|
Usage:
|
2023-02-18 05:51:03 -06:00
|
|
|
$0 [option] [input-file] [output-file]
|
2022-12-30 06:11:52 -06:00
|
|
|
Options:
|
2023-02-18 05:51:03 -06:00
|
|
|
-T or --txt-to-wav Converts the text file to a wav audio file
|
|
|
|
-A or --audio-to-txt Converts an audio file to a file readable by the old pinebuds firmware
|
|
|
|
-C or --audio-to-cpp Converts an audio file to a file compilable into the pinebuds firmware
|
2022-12-30 06:11:52 -06:00
|
|
|
"
|