commit 4a011689abb747f40ab96dcf5e798f333b18e28b Author: augustin64 Date: Sun Dec 31 15:05:04 2023 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d777fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.mkv +out/ + +.venv +mscz/ +recordings/ +**/__pycache__ \ No newline at end of file diff --git a/src/mscz_to_mp3.py b/src/mscz_to_mp3.py new file mode 100644 index 0000000..d4ab5c4 --- /dev/null +++ b/src/mscz_to_mp3.py @@ -0,0 +1,100 @@ +import music21 +import subprocess +import sys +import os + + +MSCORE="mscore" +REMOVE_MID_FILES=False + + +def get_elements(stream, classe): + elements = [] + for el in stream[classe]: + offset = el.activeSite.getOffsetBySite(stream.parts[0]) + el.getOffsetBySite(el.activeSite) + elements.append((offset, el)) + return elements + + +def add_elements(stream, elements): + st_elements = [(offset, el.number) for (offset, el) in get_elements(stream, 'MetronomeMark')] + #print(st_elements) + for offset, el in elements: + if (offset, el.number) not in st_elements: + stream.insert(offset, el) + #print(f"Inserting {el}@{offset}") + + return stream + +def separate_instruments(input_midi_path, output_folder): + # Load MIDI file + stream = music21.converter.parse(input_midi_path) + outputs = [] + + elements = get_elements(stream, 'MetronomeMark') + #print(elements) + + # Extract each part (music21.instrument) and create a separate MIDI file for each + for i, part in enumerate(stream.parts): + part.makeRests(fillGaps=True, inPlace=True) + part.makeMeasures(inPlace=True) + part.makeTies(inPlace=True) + + # Create a new music21.stream for each part + instrument_stream = music21.stream.Score() + instrument_stream.append(part) + + instrument_stream = add_elements(instrument_stream, elements) + + # Create a new MIDI file for each music21.instrument + output_path = f"{output_folder}/instrument_{i + 1}.mid" + instrument_stream.write('midi', fp=output_path) + outputs.append(output_path) + return outputs + + +def change_instrument(input, output): + s = music21.converter.parse(input) + + for el in s.recurse(): + if 'Instrument' in el.classes: # or 'Piano' + el.activeSite.replace(el, music21.instrument.Piano()) + + s.write('midi', output) + +def basename(name): + return '.'.join(name.split('/')[-1].split('.')[:-1]) + +def create_audios(files, dest="out"): + if len(files) == 0: + return + + for el in files: + subprocess.call(["mscore", el, "-o", os.path.join(dest, basename(el))+".mp3"]) + print(f"Converted {basename(el)}.mp3") + + +def __main__(): + SCORE=sys.argv[1] + OUT=sys.argv[2] + os.makedirs(OUT, exist_ok=True) + + # Create midi file + subprocess.call([MSCORE, SCORE, "-o", f"{OUT}/{basename(SCORE)}.mid"]) + + midi_files = separate_instruments(f"{OUT}/{basename(SCORE)}.mid", OUT) + + for file in midi_files: + change_instrument(file, file) + + print("=== Creating audios ===") + #create_audios(midi_files, OUT) + + if REMOVE_MID_FILES: + os.remove(f"{OUT}/{basename(SCORE)}.mid") + for file in midi_files: + os.remove(file) + + +if __name__ == "__main__": + __main__() \ No newline at end of file diff --git a/src/recording.sh b/src/recording.sh new file mode 100755 index 0000000..3fcbf42 --- /dev/null +++ b/src/recording.sh @@ -0,0 +1,6 @@ +#!/bin/bash +SURFACE=$(slurp) + +sleep 5 +wlrctl pointer click +wf-recorder -g "$SURFACE" \ No newline at end of file diff --git a/src/set_audio.sh b/src/set_audio.sh new file mode 100755 index 0000000..9e16898 --- /dev/null +++ b/src/set_audio.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +IN=recordings/le_roi.mkv +AUDIOS=out +OUT=videos +#TEMPO=90 +#ADDED_TEMPS=3 +#TEMP_DIR=$(mktemp -d) + +#OFFSET=$(bc <<< "scale=20; 60.0/$TEMPO * $ADDED_TEMPS") + +mkdir -p "$VIDEOS" + +add_audio () { + #temp_file=$TEMP_DIR/$(basename $2) + #echo temp: $temp_file + #ffmpeg -i $2 \ + # -ss $OFFSET \ + # -acodec copy \ + # $temp_file + + ffmpeg -y \ + -i $1 \ + -i $2 \ + -c:v copy \ + -map 0:v:0 \ + -map 1:a:0 \ + $3 + + #rm $temp_file +} + +add_audio "$IN" "$AUDIOS/instrument_1.mp3" "$OUT/soprane.mkv" +add_audio "$IN" "$AUDIOS/instrument_2.mp3" "$OUT/alti.mkv" +add_audio "$IN" "$AUDIOS/instrument_3.mp3" "$OUT/tenor.mkv" +add_audio "$IN" "$AUDIOS/instrument_4.mp3" "$OUT/basse.mkv"