|
|
|
@ -1,5 +1,3 @@
|
|
|
|
|
import time
|
|
|
|
|
from pydub import AudioSegment
|
|
|
|
|
import numpy as np
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
plt.style.use("bmh")
|
|
|
|
@ -9,6 +7,10 @@ from Notas import Nota, eNotas, tipoNota, modificador
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.notas = notas
|
|
|
|
@ -20,7 +22,10 @@ class Instrumento(threading.Thread):
|
|
|
|
|
self.stream.start()
|
|
|
|
|
|
|
|
|
|
def beep(self, nota: Nota, velocidad: int):
|
|
|
|
|
# Calculate the duration of the beep in seconds
|
|
|
|
|
duration = nota.duracion() * velocidad
|
|
|
|
|
|
|
|
|
|
# Calculate the frequency
|
|
|
|
|
f = nota.frec()
|
|
|
|
|
|
|
|
|
|
# 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 = audio.astype(np.float32)
|
|
|
|
|
|
|
|
|
|
# Apply volume
|
|
|
|
|
audio = audio * self.volumen
|
|
|
|
|
|
|
|
|
|
# Play the audio
|
|
|
|
@ -41,10 +47,12 @@ class Instrumento(threading.Thread):
|
|
|
|
|
def run(self):
|
|
|
|
|
# Wait for all threads to be ready
|
|
|
|
|
self.barrier.wait()
|
|
|
|
|
# Play the notes one by one
|
|
|
|
|
for nota in self.notas:
|
|
|
|
|
self.beep(nota, self.velocidad)
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
# Close the stream
|
|
|
|
|
self.stream.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -102,13 +110,21 @@ if __name__ == "__main__":
|
|
|
|
|
Nota(eNotas.DO, tipoNota.B, octava=3)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Create the barrier
|
|
|
|
|
barrier = threading.Barrier(2)
|
|
|
|
|
|
|
|
|
|
#Create the threads
|
|
|
|
|
instrumento1 = Instrumento(melodia, 0.5, 1, barrier)
|
|
|
|
|
instrumento2 = Instrumento(acompañamiento, 0.5, 0.5, barrier)
|
|
|
|
|
#Barrier to ensure that both threads start at the same time
|
|
|
|
|
|
|
|
|
|
#Start both threads
|
|
|
|
|
instrumento1.start()
|
|
|
|
|
instrumento2.start()
|
|
|
|
|
|
|
|
|
|
#Wait for both threads to finish
|
|
|
|
|
instrumento1.join()
|
|
|
|
|
instrumento2.join()
|
|
|
|
|
|
|
|
|
|
#Close the streams
|
|
|
|
|
instrumento1.close()
|
|
|
|
|
instrumento2.close()
|