1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Posted at

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

pythonコード
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()

これで、プログラム自体は完成である。あとは、外部フォルダのみ。ここで使えるのは、「.hib」という形のみである。

てきとう.hib
"タイトル"
tempo-"テンポ"

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

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

pythonコード
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()

このように書くと、「.hib2」で書くとき、

てきとう.hib2
"タイトル"
tempo-"テンポ"

"音階"-"長さ"
"音階"-"長さ"
"音階"-"長さ"...

のように書くこともできます。
※ただし、一つ下の時は|ド
などと書きます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?