## Python Homework Help on Musical Score Player

• 13th May, 2022
• 15:10 PM
```import numpy as np
import simpleaudio as sa
#item contain the symbol of the sheet music and it is encoded in the matriculation number i.e 'abdc'
#here a is pitch of sine wave which we want to generate
#here b is the octave
#here c is duration of note
#here d is the augmentation of note duration
#list is define so that the sound feels like a tune
item=["264 c 0 1",
"275 c# 0 2",
"297 D 0 1",
"330 E 0 2",
"352 F 0 1",
"396 G 0 2",
"440 A 0 1"]
for i in item:#used variable i to iterate over every string in item
print(i)
temp=i.split()#split the string into list
frequency = int(temp[0])  # Our played note frequency
fs = 44100  # 44100 samples per second
duration = int(temp[3])+int(temp[2])  #  duration+augmentation
t = np.linspace(0, duration, duration * fs, False)# here we generate array with duration*sample_rate steps, ranging between 0 and duration
note = np.sin(frequency * t * 2 * np.pi)# here we generate a sine wave
audio = note * (2**15 - 1) / np.max(np.abs(note))# here we ensure that highest value is in 16-bit range
audio = audio.astype(np.int16)# here we convert to 16-bit data
play_obj = sa.play_buffer(audio, 1, 2, fs)# Play audio, here 1 specify number of channels
play_obj.wait_done()# Wait for audio to finish before exiting
```