본문 바로가기

딥러닝/소리데이터

music 21 사용하기 & 에러 해결 .show()

쥬피터 환경 

music21 설치하기  & musescore 설치하기 

pip install music21

https://musescore.org/en

 

Free music composition and notation software | MuseScore

I found this wonderful program by googling 'how do you compose music'? It's easy to use.

musescore.org

 

한가지 이슈가 있었던게 

음표를 입력하고 .show 하는 과정에서 에러가 발생했다. 

에러 내용은 musecore 경로에러 

from music21 import *
# get environment
print(env)
us = environment.UserSettings()
us['musicxmlPath'] = 'C:\\Program Files\\MuseScore 3\\bin\\MuseScore3.exe'
us['musescoreDirectPNGPath'] = 'C:\\Program Files\\MuseScore 3\\bin\\MuseScore3.exe'
us['musicxmlPath']

해당위의 코드로 경로 변경해주면 .show() 가 잘 되는것을 확인할 수 있다. 

from music21 import *
f = note.Note("F5")
 
f.show()

 

 

만약 구글 코랩 환경이라면 

from music21 import *

!add-apt-repository ppa:mscore-ubuntu/mscore-stable -y
!apt-get update
!apt-get install musescore

!apt-get install xvfb

!sh -e /etc/init.d/x11-common start

import os
os.putenv('DISPLAY', ':99.0')

!start-stop-daemon --start --pidfile /var/run/xvfb.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -screen 0 1024x768x24 -ac +extension GLX +render -noreset

us = environment.UserSettings()
us['musescoreDirectPNGPath'] = '/usr/bin/mscore'
us['directoryScratch'] = '/tmp'

-> 해당 코드는 꽤 오래 걸림 

%%capture

# converts midi files to wav files into order to play them
print('installing fluidsynth...')
!apt-get install fluidsynth > /dev/null
!cp /usr/share/sounds/sf2/FluidR3_GM.sf2 ./font.sf2

from IPython.display import Image, Audio
from music21 import *

def play(x):
    filename = x.write('mid')
    !fluidsynth -ni font.sf2 $filename -F $filename\.wav -r 16000 > /dev/null
    display(Audio(filename + '.wav'))

 

실행해 주고나면 

a = note.Note("C#4")
a.show()

play(a)

플레이도 잘 된다.

https://financial-engineering.medium.com/google-colab%EC%97%90%EC%84%9C-music21-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0-b02e330e7080

 

Google Colab에서 Music21 사용하기

Python에서 사용할 수 있는 음악모듈 중에 music21이라는 것이 있습니다. 음악을 전문적으로 분석하기 위한 모듈인데, 여기서는 google colab에서 사용하는 방법을 설명하겠습니다.

financial-engineering.medium.com

참고사이트