python音楽の一般的な操作

4149 ワード

一、python合成音楽
import json
import numpy as np
import matplotlib.pyplot as plt
from scipy.io.wavfile import write

#        
tone_freq_map={"#g3": 1863.7, "#b3": 2348.3, "e1": 329.5, "f2": 739.5, "d1": 293.4, "#c3": 1244.0,
 "#D": 155.46175, "E": 164.745, "E1": 82.3725, "#a1": 466.0, "d2": 621.8, "d3": 1318.0,
 "c3": 1174.1, "G1": 97.93175, "C": 130.75, "#C": 138.529625, "#d1": 310.9, "e3": 1479.3,
 "#b2": 1108.2, "c2": 554.1, "a1": 439.8, "D": 146.766875, "#F1": 92.44,"b1": 493.7,
 "#F": 184.881, "D1": 73.35075, "#a2": 987.2, "#G": 207.566,"b3": 2216.5, "g3": 1759.1,
 "#D1": 77.730875, "F1": 87.276, "#c2": 587.1, "B": 246.790625, "#C1": 69.232125,
 "#f1": 369.8, "#a3": 2092.0, "#d2": 659.0, "#g2": 879.7, "#f2": 783.7, "#b1": 523.0,
 "#f3": 1660.5, "#g1": 415.0, "f3": 1567.2, "#c1": 276.9, "C1": 65.375, "A": 219.922,
 "#A1": 116.49825, "A1": 109.9608, "g2": 830.3, "F": 174.5513, "f1": 349.1, "#A": 232.9965,
 "c1": 261.5, "a2": 932.0, "a3": 1974.6, "#d3": 1396.1, "#G1": 103.750125, "g1": 391.7,
 "G": 195.928875, "B1": 123.428, "b2": 1046.0, "e2": 698.2}


def synthesizer(freq,duration,amp=1.0,sampling_freq=44100):
    '''      '''
    t=np.linspace(0,duration,duration*sampling_freq) #      
    audio=amp*np.sin(2*np.pi*freq*t)
    audio=audio.astype(np.int16)
    return audio


if __name__=='__main__':
    lengths=60

    #   2  G 
    #input_tone='G'
    #duration=12  #   : 
    amplitude=10000
    sampling_freq=44100  #   :Hz

    #        
    #tone_seq=[('D',0.3),('G',0.6),('C',0.5),('A',0.3),('#A',0.7)]
    tone_seq=[]
    keyss=list(tone_freq_map.keys())
    keys=[]
    ind=0
    for i in range(lengths):
        try:
            keys.append(keyss[ind])
        except IndexError:
            ind=0
            keys.append(keyss[ind])
        ind+=1
    
    for i in range(lengths):
        d=np.random.choice(keys[i:i+3])
        pl=np.random.randint(2,8)/10
        tone_seq.append((d,pl))
    output=np.array([])
    for i in tone_seq:
        input_tone=i[0]
        duration=i[1]

        #     
        synthesized_tone=synthesizer(tone_freq_map[input_tone],duration,
                                     amplitude,sampling_freq)
        output=np.append(output,synthesized_tone,axis=0)
    
    #            
    write('output_tone.wav',sampling_freq,output)
    print('    : output_tone.wav   !')

実行結果は次のとおりです.
生成ファイル:output_tone.wav成功!
二、音楽を再生する:
pygameモジュールで再生
コマンドラインに入ってpygameをインストールする:pip install pygame
import pygame

"""
pygame --        
pygame.init()           ,
pygame.mixer.init()           
pygame.mixer.music.load('xx.mp3')               ,     ogg、mp3   。              ,          ,                  。
pygame.mixer.music.play()       。       ,         。
play           
pygame.mixer.music.play(loops=0, start=0.0) loops start                 。
pygame.mixer.music.stop()     ,
pygame.mixer.music.pause()     。
pygame.mixer.music.unpause()     。
pygame.mixer.music.fadeout(time)       , time               0,      。
pygame.mixer.music.set_volume(value)         ,  value    0.0 1.0。
pygame.mixer.music.get_busy()          ,  1     。
pygame.mixer.music.set_endevent(pygame.USEREVENT + 1)         ,            ,
            pygame.USEREVENT+1       。 pygame.mixer.music.queue(filename)                ,
                      。                 。
"""

#    
pygame.mixer.init()
#     
pygame.mixer.music.load('   .mp3'.encode())
#     
pygame.mixer.music.play()
#     
# pygame.mixer.music.stop()