音楽演奏をpythonで！誰でも簡単に！

誰でも簡単に作れる(？)音楽作成ソフトをつくっていこう。
カット!
結局こうなる。

<details><summary>pythonコード</summary>

```py:start.py
import time, glob, winsound, math, tkinter, tkinter.ttk as ttk

def playing(music,tempos):
    tem = 60000 / tempos
    for i in range(0, len(music)):
        Hz = float(music[i][0])
        Le = float(music[i][1]) * tem
        if(Hz == 0):
            time.sleep(Le * 1000)
        else:
            winsound.Beep(math.floor(Hz), math.floor(Le))

def SetMusic():
    mus = sett.get()
    for i in range(0, len(title)):
        if(mus == title[i]):
            num = i
    
    playing(file[num], tempo[num])

def Settings():
    global file, files, title, tempo
    file = []
    files = glob.glob("gakuhu\*.hib")
    for i in range(0, len(files)):
        with open(files[i], "r", encoding="UTF-8")as f:
            file.append(f.readlines())
    
    title = []
    tempo = []
    for j in range(0, len(file)):
        for i in range(0, len(file[j])):
            file[j][i] = file[j][i].replace('\n', '')
        title.append(file[j].pop(0))
        tempo.append(int(file[j].pop(0).replace('tempo-', '')))
        delt = file[j].pop(0)
    for j in range(0, len(file)):
        for i in range(0, len(file[j])):
            file[j][i] = file[j][i].split('-')

title = []
tempo = []
Settings()
root = tkinter.Tk()
root.geometry("400x320")
sett = ttk.Combobox(root, values=title)
sett.pack()
butt = tkinter.Button(root, command=SetMusic, text="実行")
butt.pack()
root.mainloop()
```
</details>

これで、プログラム自体は完成である。あとは、外部フォルダのみ。ここで使えるのは、「.hib」という形のみである。
```hib:てきとう.hib
"タイトル"
tempo-"テンポ"

"周波数"-"長さ"
"周波数"-"長さ"
"周波数"-"長さ"...
```

最後に、pythonを実行し、
ウィンドウのコンボボックスからタイトルを選び、
実行ボタンをおせばなります。

<details><summary>pythonコード</summary>

```py:start.py
import time, glob, winsound, math, tkinter, tkinter.ttk as ttk, json

def playing(music,tempos):
    tem = 60000 / tempos
    for i in range(0, len(music)):
        Hz = float(music[i][0])
        Le = float(music[i][1]) * tem
        if(Hz == 0):
            time.sleep(Le / 1000)
        else:
            winsound.Beep(math.floor(Hz), math.floor(Le))

def playing2(music,tempos):
    tem = 60000 / tempos
    for i in range(0, len(music)):
        Hz = int(ontei[str(music[i][0])])
        Le = float(music[i][1]) * tem
        if(Hz == 0):
            time.sleep(Le / 1000)
        else:
            winsound.Beep(math.floor(Hz), math.floor(Le))

def SetMusic():
    mus = sett.get()
    for i in range(0, len(title)):
        if(mus == title[i]):
            num = i
    
    if num <= len(hib) - 1:
        playing(hib[num], tempo[num])
    else:
        playing2(hib2[num - len(hib)], tempo[num])

def Settings():
    global hib, files, title, tempo, ontei, hib2
    hib = []
    hib2 = []
    files = glob.glob("gakuhu\*.hib")
    for i in range(0, len(files)):
        with open(files[i], "r", encoding="UTF-8")as f:
            hib.append(f.readlines())
    files = glob.glob("gakuhu\*.hib2")
    for i in range(0, len(files)):
        with open(files[i], "r", encoding="UTF-8")as f:
            hib2.append(f.readlines())
    
    title = []
    tempo = []
    for j in range(0, len(hib)):
        for i in range(0, len(hib[j])):
            hib[j][i] = hib[j][i].replace('\n', '')
        title.append(hib[j].pop(0))
        tempo.append(int(hib[j].pop(0).replace('tempo-', '')))
        delt = hib[j].pop(0)
    for j in range(0, len(hib)):
        for i in range(0, len(hib[j])):
            hib[j][i] = hib[j][i].split('-')

    for j in range(0, len(hib2)):
        for i in range(0, len(hib2[j])):
            hib2[j][i] = hib2[j][i].replace('\n', '')
        title.append(hib2[j].pop(0))
        tempo.append(int(hib2[j].pop(0).replace('tempo-', '')))
        delt = hib2[j].pop(0)
    for j in range(0, len(hib2)):
        for i in range(0, len(hib2[j])):
            hib2[j][i] = hib2[j][i].split('-')
    
    print(hib)
    print(hib2)

ontei = {
    "ド||": 65,
    "ド#||":69,
    "レ||":72,
    "レ#||":77,
    "ミ||":82,
    "ファ||":87,
    "ファ#||":92,
    "ソ||":97,
    "ソ#||":103,
    "ラ||":110,
    "ラ#||":116,
    "シ||":123,
    "ド|":130,
    "ド#|":138,
    "レ|":146,
    "レ#|":155,
    "ミ|":164,
    "ファ|":174,
    "ファ#|":184,
    "ソ|":195,
    "ソ#|":207,
    "ラ|":220,
    "ラ#|":233,
    "シ|":246,
    "ド":261,
    "ド#":277,
    "レ":293,
    "レ#":311,
    "ミ":329,
    "ファ":349,
    "ファ#":370,
    "ソ":391,
    "ソ#":415,
    "ラ":440,
    "ラ#":466,
    "シ":493,
    "|ド":523,
    "|ド#":554,
    "|レ":587,
    "|レ#":622,
    "|ミ":659,
    "|ファ":698,
    "|ファ#":739,
    "|ソ":783,
    "|ソ#":830,
    "|ラ":880,
    "|ラ#":932,
    "|シ":987,
    "||ド":1046,
    "||ド#":1108,
    "||レ":1174,
    "||レ#":1244,
    "||ミ":1318,
    "||ファ":1396,
    "||ファ#":1479,
    "||ソ":1567,
    "||ソ#":1661,
    "||ラ":1760,
    "||ラ#":1864,
    "||シ":1975,
    "|||ド":2093
}

title = []
tempo = []
Settings()
root = tkinter.Tk()
root.geometry("400x320")
sett = ttk.Combobox(root, values=title)
sett.pack()
butt = tkinter.Button(root, command=SetMusic, text="実行")
butt.pack()
root.mainloop()
```
</details>

このように書くと、「.hib2」で書くとき、
```hib2:てきとう.hib2
"タイトル"
tempo-"テンポ"

"音階"-"長さ"
"音階"-"長さ"
"音階"-"長さ"...
```
のように書くこともできます。
※ただし、一つ下の時は|ド
などと書きます。