|
|
@ -1,5 +1,3 @@
|
|
|
|
import time
|
|
|
|
|
|
|
|
from pydub import AudioSegment
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
plt.style.use("bmh")
|
|
|
|
plt.style.use("bmh")
|
|
|
@ -9,6 +7,10 @@ from Notas import Nota, eNotas, tipoNota, modificador
|
|
|
|
|
|
|
|
|
|
|
|
class Instrumento(threading.Thread):
|
|
|
|
class Instrumento(threading.Thread):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Notas: Lista de notas a reproducir
|
|
|
|
|
|
|
|
# Velocidad: Velocidad de reproduccion (bpm = 1/vel * 60) (1 = 1 negra por segundo)
|
|
|
|
|
|
|
|
# Volumen: Volumen de reproduccion (0 (silencio) - 1 (maximo))
|
|
|
|
|
|
|
|
# Barrier: Barrier para sincronizar los threads
|
|
|
|
def __init__(self, notas: list, velocidad: int, volumen: float, barrier=None):
|
|
|
|
def __init__(self, notas: list, velocidad: int, volumen: float, barrier=None):
|
|
|
|
super().__init__()
|
|
|
|
super().__init__()
|
|
|
|
self.notas = notas
|
|
|
|
self.notas = notas
|
|
|
@ -20,7 +22,10 @@ class Instrumento(threading.Thread):
|
|
|
|
self.stream.start()
|
|
|
|
self.stream.start()
|
|
|
|
|
|
|
|
|
|
|
|
def beep(self, nota: Nota, velocidad: int):
|
|
|
|
def beep(self, nota: Nota, velocidad: int):
|
|
|
|
|
|
|
|
# Calculate the duration of the beep in seconds
|
|
|
|
duration = nota.duracion() * velocidad
|
|
|
|
duration = nota.duracion() * velocidad
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Calculate the frequency
|
|
|
|
f = nota.frec()
|
|
|
|
f = nota.frec()
|
|
|
|
|
|
|
|
|
|
|
|
# Generate the time values for the samples
|
|
|
|
# Generate the time values for the samples
|
|
|
@ -33,6 +38,7 @@ class Instrumento(threading.Thread):
|
|
|
|
audio = wave * (2**15 - 1) / np.max(np.abs(wave))
|
|
|
|
audio = wave * (2**15 - 1) / np.max(np.abs(wave))
|
|
|
|
audio = audio.astype(np.float32)
|
|
|
|
audio = audio.astype(np.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Apply volume
|
|
|
|
audio = audio * self.volumen
|
|
|
|
audio = audio * self.volumen
|
|
|
|
|
|
|
|
|
|
|
|
# Play the audio
|
|
|
|
# Play the audio
|
|
|
@ -41,10 +47,12 @@ class Instrumento(threading.Thread):
|
|
|
|
def run(self):
|
|
|
|
def run(self):
|
|
|
|
# Wait for all threads to be ready
|
|
|
|
# Wait for all threads to be ready
|
|
|
|
self.barrier.wait()
|
|
|
|
self.barrier.wait()
|
|
|
|
|
|
|
|
# Play the notes one by one
|
|
|
|
for nota in self.notas:
|
|
|
|
for nota in self.notas:
|
|
|
|
self.beep(nota, self.velocidad)
|
|
|
|
self.beep(nota, self.velocidad)
|
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
def close(self):
|
|
|
|
|
|
|
|
# Close the stream
|
|
|
|
self.stream.close()
|
|
|
|
self.stream.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -102,13 +110,21 @@ if __name__ == "__main__":
|
|
|
|
Nota(eNotas.DO, tipoNota.B, octava=3)]
|
|
|
|
Nota(eNotas.DO, tipoNota.B, octava=3)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barrier = threading.Barrier(2)
|
|
|
|
#Create the barrier
|
|
|
|
instrumento1 = Instrumento(melodia, 0.5, 1, barrier)
|
|
|
|
barrier = threading.Barrier(2)
|
|
|
|
instrumento2 = Instrumento(acompañamiento, 0.5, 0.5, barrier)
|
|
|
|
|
|
|
|
#Barrier to ensure that both threads start at the same time
|
|
|
|
#Create the threads
|
|
|
|
instrumento1.start()
|
|
|
|
instrumento1 = Instrumento(melodia, 0.5, 1, barrier)
|
|
|
|
instrumento2.start()
|
|
|
|
instrumento2 = Instrumento(acompañamiento, 0.5, 0.5, barrier)
|
|
|
|
instrumento1.join()
|
|
|
|
|
|
|
|
instrumento2.join()
|
|
|
|
#Start both threads
|
|
|
|
instrumento1.close()
|
|
|
|
instrumento1.start()
|
|
|
|
instrumento2.close()
|
|
|
|
instrumento2.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Wait for both threads to finish
|
|
|
|
|
|
|
|
instrumento1.join()
|
|
|
|
|
|
|
|
instrumento2.join()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Close the streams
|
|
|
|
|
|
|
|
instrumento1.close()
|
|
|
|
|
|
|
|
instrumento2.close()
|