-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from looptailG/iteration-utils
Iteration utils
- Loading branch information
Showing
4 changed files
with
244 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
out | ||
out/* | ||
!out/GenerateFifthGeneratedPlugin.py | ||
|
||
source/CustomTunings.tsv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
import shutil | ||
import re | ||
|
||
|
||
PLUGIN_FOLDER_NAME = "fifth_generated_tuner" | ||
SOURCE_FOLDER = "../source" | ||
LOGS_FOLDER = f"{PLUGIN_FOLDER_NAME}/logs" | ||
README_PATH = "../README.md" | ||
LICENSE_PATH = "../LICENSE" | ||
THUMBNAIL_PATH = "../thumbnail/FifthGeneratedTunerThumbnail.png" | ||
FILES_TO_COPY = [ | ||
LICENSE_PATH, | ||
THUMBNAIL_PATH, | ||
] | ||
FILES_TO_CREATE = [ | ||
f"{PLUGIN_FOLDER_NAME}/CustomTunings.tsv" | ||
] | ||
|
||
|
||
def main(): | ||
try: | ||
shutil.copytree(SOURCE_FOLDER, PLUGIN_FOLDER_NAME, dirs_exist_ok=True) | ||
for file_path in FILES_TO_COPY: | ||
file_name = file_path[file_path.rindex("/") + 1:] | ||
shutil.copyfile(file_path, f"{PLUGIN_FOLDER_NAME}/{file_name}") | ||
if not os.path.exists(LOGS_FOLDER): | ||
os.makedirs(LOGS_FOLDER) | ||
for file_path in FILES_TO_CREATE: | ||
open(file_path, "w") | ||
|
||
version_number = get_version_number() | ||
output_folder_name = f"{PLUGIN_FOLDER_NAME}_{version_number}" | ||
temporary_folder = "tmp" | ||
shutil.copytree(PLUGIN_FOLDER_NAME, f"{temporary_folder}/{PLUGIN_FOLDER_NAME}", dirs_exist_ok=True) | ||
shutil.make_archive(output_folder_name, "zip", temporary_folder) | ||
shutil.rmtree(temporary_folder) | ||
|
||
except Exception as e: | ||
print(e) | ||
input() | ||
|
||
|
||
def get_version_number() -> str: | ||
version_number_pattern = re.compile(r"version:\s*\"(.+)\";") | ||
for root, _, files in os.walk(SOURCE_FOLDER): | ||
for file_name in files: | ||
if simplify_file_name(PLUGIN_FOLDER_NAME) not in simplify_file_name(file_name): | ||
continue | ||
|
||
file_path = os.path.join(root, file_name) | ||
with open(file_path, "r") as file: | ||
for line in file: | ||
match = version_number_pattern.match(line.strip()) | ||
if match: | ||
return match.group(1) | ||
|
||
raise Exception("Could not get the version number.") | ||
|
||
|
||
def simplify_file_name(file_name: str) -> str: | ||
return file_name.replace("_", "").lower() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
A collection of functions and constants for iterating over a score. | ||
Copyright (C) 2024 Alessandro Culatti | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
const VERSION = "1.0.0"; | ||
|
||
const ELEMENT_STAFF_TEXT = 48; | ||
|
||
function iterate(curScore, actions, logger) | ||
{ | ||
let onStaffStart = actions.onStaffStart || null; | ||
let onNewMeasure = actions.onNewMeasure || null; | ||
let onKeySignatureChange = actions.onKeySignatureChange || null; | ||
let onAnnotation = actions.onAnnotation || null; | ||
let staffTextOnCurrentStaffOnly = actions.staffTextOnCurrentStaffOnly || true; | ||
let onNote = actions.onNote || null; | ||
|
||
curScore.startCmd(); | ||
let cursor = curScore.newCursor(); | ||
|
||
// Calculate the portion of the score to iterate on. | ||
let startStaff; | ||
let endStaff; | ||
let startTick; | ||
let endTick; | ||
cursor.rewind(Cursor.SELECTION_START); | ||
if (!cursor.segment) | ||
{ | ||
logger.log("Tuning the entire score."); | ||
startStaff = 0; | ||
endStaff = curScore.nstaves - 1; | ||
startTick = 0; | ||
endTick = curScore.lastSegment.tick; | ||
} | ||
else | ||
{ | ||
logger.log("Tuning only the current selection."); | ||
startStaff = cursor.staffIdx; | ||
startTick = cursor.tick; | ||
cursor.rewind(Cursor.SELECTION_END); | ||
endStaff = cursor.staffIdx; | ||
if (cursor.tick == 0) | ||
{ | ||
// If the selection includes the last note of the score, .rewind() | ||
// overflows and goes back to tick 0. | ||
endTick = curScore.lastSegment.tick; | ||
} | ||
else | ||
{ | ||
endTick = cursor.tick; | ||
} | ||
logger.trace("Tuning only ticks: " + startTick + " - " + endTick); | ||
logger.trace("Tuning only staffs: " + startStaff + " - " + endStaff); | ||
} | ||
|
||
// Iterate on the score. | ||
for (let staff = startStaff; staff <= endStaff; staff++) | ||
{ | ||
for (let voice = 0; voice < 4; voice++) | ||
{ | ||
logger.log("Tuning Staff: " + staff + "; Voice: " + voice); | ||
|
||
cursor.voice = voice; | ||
cursor.staffIdx = staff; | ||
cursor.rewindToTick(startTick); | ||
|
||
let previousKeySignature = cursor.keySignature; | ||
|
||
if (onStaffStart) | ||
{ | ||
onStaffStart(); | ||
} | ||
|
||
// Loop on the element of the current staff. | ||
while (cursor.segment && (cursor.tick <= endTick)) | ||
{ | ||
if (cursor.segment.tick == cursor.measure.firstSegment.tick) | ||
{ | ||
if (onNewMeasure) | ||
{ | ||
onNewMeasure(); | ||
} | ||
} | ||
|
||
if (cursor.keySignature != previousKeySignature) | ||
{ | ||
if (onKeySignatureChange) | ||
{ | ||
onKeySignatureChange(cursor.keySignature); | ||
} | ||
previousKeySignature = cursor.keySignature; | ||
} | ||
|
||
for (let i = 0; i < cursor.segment.annotations.length; i++) | ||
{ | ||
let annotation = cursor.segment.annotations[i]; | ||
let annotationText = annotation.text; | ||
if (annotationText) | ||
{ | ||
if (onAnnotation) | ||
{ | ||
if ((annotation.type === ELEMENT_STAFF_TEXT) && staffTextOnCurrentStaffOnly) | ||
{ | ||
let annotationPart = annotation.staff.part; | ||
if ((4 * staff >= annotationPart.startTrack) && (4 * staff < annotationPart.endTrack)) | ||
{ | ||
onAnnotation(annotation); | ||
} | ||
} | ||
else | ||
{ | ||
onAnnotation(annotation); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (cursor.element && (cursor.element.type == Element.CHORD)) | ||
{ | ||
let graceChords = cursor.element.graceNotes; | ||
for (let i = 0; i < graceChords.length; i++) | ||
{ | ||
let notes = graceChords[i].notes; | ||
for (let j = 0; j < notes.length; j++) | ||
{ | ||
if (onNote) | ||
{ | ||
onNote(notes[j]); | ||
} | ||
} | ||
} | ||
|
||
let notes = cursor.element.notes; | ||
for (let i = 0; i < notes.length; i++) | ||
{ | ||
if (onNote) | ||
{ | ||
onNote(notes[i]); | ||
} | ||
} | ||
} | ||
|
||
cursor.next(); | ||
} | ||
} | ||
} | ||
|
||
curScore.endCmd(); | ||
} |