How do computers hear music?
Audio representation
import IPython.display as ipd
import numpy
sr = 22050 # Sampling rate (Hz)
f = 440 # Frequency (Hz)
tmax = 1 # Duration (s)
### Construct a time line
t = numpy.linspace(0, tmax, sr * tmax)
### Construct the audio signal data
x = numpy.sin(2 * numpy.pi * f * t)
### Show the sound in an audio player
ipd.Audio(x, rate=sr)
import pandas as pd
df = pd.DataFrame(
{
"t": t[0:220],
"x": x[0:220],
}
)
### Here the data only shows first 220 frames (0.01s) of the audio data
df
import pandas as pd
df2 = pd.DataFrame(
{
"t": t,
"x": x,
}
)
### Here the data shows all frames (1s) of the audio data
df2
Symbolic representation
!pip install music21
!add-apt-repository ppa:mscore-ubuntu/mscore-stable -y
!apt-get update
!apt-get -y install musescore
from music21 import *
us = environment.UserSettings()
us['musescoreDirectPNGPath'] = '/usr/bin/mscore'
us['directoryScratch'] = '/tmp'
from music21 import *
s = stream.Stream()
melody = [60,60,67,67,69,69,67,]
for p in melody:
s.append(note.Note(p))
s.show()
s.show('midi')