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

Develop #4

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Binary file added audio_files/IMG_3570.mp3
Binary file not shown.
Binary file added audio_files/IMG_6043.mp3
Binary file not shown.
31 changes: 9 additions & 22 deletions connection.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import json
from UnityConnector import UnityConnector


# タイムアウト時のコールバック
def on_timeout():
print("timeout")


# Unityから停止命令が来たときのコールバック
def on_stopped():
print("stopped")


# インスタンス
connector = UnityConnector(on_timeout=on_timeout, on_stopped=on_stopped)


# データが飛んできたときのコールバック
def on_data_received(data_type, data):
print(data_type, data)


print("connecting...")

# Unity側の接続を待つ
Expand All @@ -28,22 +24,13 @@ def on_data_received(data_type, data):
print("connected")

# デモ用のループ
while True:
# Enterで送信を開始(入力内容は送信内容と関係ない)
input_data = input()

# Unityへ停止命令
if input_data == "q":
connector.stop_connection()
break

# 送るデータをdictionary形式で
data = {
"testValue0": 334,
"testValue1": "test",
}
def send_data_loop(data):
# 送るデータをJSON形式に変換
json_data = json.dumps(data)
print(f"send_data: {data}")

print(data)
# Unityへ送る
connector.send("test", data)

# Unityへ送る
connector.send("test", data)
if __name__ == "__main__":
send_data_loop({"test": "test"})
90 changes: 90 additions & 0 deletions converted_music.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from music21 import *
import json
import copy

# C, D, E, F, G, A, B の音符をドレミファソラシドに対応させる辞書
note_to_doremi = {
'C': 'ド',
'D': 'レ',
'E': 'ミ',
'F': 'ファ',
'G': 'ソ',
'A': 'ラ',
'B': 'シ'
}

# 音符をドレミファソラシドに変換する関数
def note_to_doremi_converter(note):
pitch = note.name[0] # 音符の最初の文字が C, D, E などの音名
return note_to_doremi.get(pitch, pitch) # 対応するドレミファソラシドを返す

# 音符のオクターブを取得する関数
def get_octave(note):
return note.pitch.octave if note.isNote else None

# 楽譜をドレミファソラシドとオクターブのリストに変換する関数
def score_to_doremi_list(score):
notes_list = []

# パートがあるかを確認
if hasattr(score, 'parts'):
# 複数のパートがある場合
for part in score.parts: # 楽譜の各パートをループ
for note in part.flat.notes: # 各音符をループ
if note.isNote: # 音符であるかを確認
note_doremi = note_to_doremi_converter(note) # 音符をドレミファソラシドに変換
octave = get_octave(note) # オクターブを取得
# 'ド4'のように音符とオクターブを結合してリストに追加
notes_list.append(f"{note_doremi}{octave}")
else:
# 単一のパートまたはフラット化されたスコア
for note in score.flat.notes:
if note.isNote:
note_doremi = note_to_doremi_converter(note)
octave = get_octave(note)
notes_list.append(f"{note_doremi}{octave}")

# リスト形式で返す
return notes_list

# 楽譜の全音符を8分音符に変換する関数
def convert_to_eighth_notes(score):
new_score = stream.Stream() # 新しい楽譜を作成
for part in score.parts: # 楽譜が複数のパートで構成されている場合
new_part = stream.Part()
for note in part.flat.notes: # 各音符を8分音符に変換
if note.quarterLength > 0.5: # 4分音符より長い場合
num_eighth_notes = int(note.quarterLength / 0.5)
for _ in range(num_eighth_notes):
new_note = copy.deepcopy(note) # 元の音符をディープコピー
new_note.quarterLength = 0.5 # 8分音符の長さに設定
new_part.append(new_note)
else:
# そのまま8分音符として追加
new_note = copy.deepcopy(note) # ディープコピー
new_note.quarterLength = 0.5 # 8分音符に調整
new_part.append(new_note)
new_score.append(new_part)
return new_score

# メイン処理部分
def main():
# 楽譜の読み込み
score = converter.parse('music_score.mxl')

# 楽譜を8分音符に変換
new_score = convert_to_eighth_notes(score)

# 楽譜をドレミファソラシドとオクターブのリストに変換
doremi_list = score_to_doremi_list(new_score)

# 結果をファイルに保存(リストをJSON形式に変換して保存)
with open('doremi_notes_list.json', 'w', encoding='utf-8') as f:
json.dump(doremi_list, f, ensure_ascii=False, indent=2)

# コンソールに出力
print(doremi_list)

# 実行
if __name__ == '__main__':
main()
200 changes: 200 additions & 0 deletions doremi_notes_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
[
"ファ5",
"ファ5",
"ミ5",
"ソ5",
"ソ5",
"ファ5",
"レ5",
"レ5",
"ラ4",
"ラ4",
"ラ4",
"ラ4",
"ラ4",
"ラ4",
"休符",
"休符",
"ファ5",
"ファ5",
"ミ5",
"ソ5",
"ソ5",
"ファ5",
"レ5",
"レ5",
"ファ5",
"ファ5",
"ファ5",
"ファ5",
"ミ5",
"ミ5",
"ミ5",
"ミ5",
"ミ5",
"ミ5",
"レ5",
"ファ5",
"ファ5",
"ミ5",
"ド5",
"ド5",
"ミ5",
"ミ5",
"レ5",
"レ5",
"ド5",
"ド5",
"レ5",
"レ5",
"シ4",
"ド5",
"ド5",
"ミ5",
"ミ5",
"レ5",
"レ5",
"ファ5",
"ファ5",
"ファ5",
"休符",
"ファ5",
"ソ5",
"ソ5",
"シ5",
"シ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"休符",
"休符",
"休符",
"ファ5",
"ソ5",
"ソ5",
"シ5",
"シ5",
"ラ5",
"ラ5",
"ミ5",
"ソ5",
"ソ5",
"ファ5",
"ファ5",
"ファ5",
"ファ5",
"ファ5",
"ファ5",
"ファ5",
"ソ5",
"ソ5",
"ラ5",
"ラ5",
"ド6",
"ド6",
"シ5",
"シ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ド6",
"ド6",
"シ5",
"シ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ファ5",
"ファ5",
"ミ5",
"ミ5",
"レ5",
"レ5",
"ミ5",
"ミ5",
"ソ5",
"ソ5",
"ファ5",
"ファ5",
"レ5",
"レ5",
"シ4",
"シ4",
"ミ5",
"ミ5",
"休符",
"ファ5",
"ファ5",
"ファ5",
"ミ5",
"レ5",
"ラ4",
"ラ4",
"ラ4",
"ファ5",
"ソ5",
"ソ5",
"シ5",
"シ5",
"ラ5",
"ラ5",
"ミ5",
"ソ5",
"ソ5",
"ファ5",
"ファ5",
"ファ5",
"ファ5",
"ファ5",
"ソ5",
"ソ5",
"ラ5",
"ラ5",
"ド6",
"ド6",
"シ5",
"シ5",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ド6",
"ド6",
"レ6",
"レ6",
"ラ5",
"ラ5",
"ラ5",
"ラ5",
"ファ5",
"ファ5",
"ミ5",
"ミ5",
"レ5",
"レ5",
"ミ5",
"ミ5",
"ソ5",
"ソ5",
"ファ5",
"ファ5",
"レ5",
"レ5",
"ド5",
"ド5",
"レ5",
"レ5",
"レ5",
"レ5",
"レ5",
"レ5",
"レ5",
"レ5"
]
3 changes: 3 additions & 0 deletions ms_dict/ms_dict_8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "休符,ド2,ファ4,ファ4,ミ4,ソ4,ソ4,ミ4"
}
Binary file added music_score.mxl
Binary file not shown.
Loading