Código de Python - Teclado musical

Imágen de perfil
Val: 568
Bronce
Ha aumentado 1 puesto en Python (en relación al último mes)
Gráfica de Python

Teclado musicalgráfica de visualizaciones


Python

Actualizado el 16 de Abril del 2021 por Antonio (51 códigos) (Publicado el 15 de Marzo del 2021)
1.417 visualizaciones desde el 15 de Marzo del 2021
Teclado musical para distintas formas de onda (WAVEFORM) donde la entrada "DURATION" determina la duración (en milisegundos) de cada nota, "GAIN" determina la ganancia en la amplitud de onda (amplitud adicional) y "FADE IN" y "FADE OUT" sirven para suavizar los milisegundos iniciales y finales de cada nota respectivamente.

PARA CUALQUIER DUDA U OBSERVACIÓN, USEN LA SECCIÓN DE COMENTARIOS.

kbr

Requerimientos

Lenguaje: Python
Librerías y recursos: Tkinter, threading, pydub (se necesita tener instalado "ffmpeg").

1.2
estrellaestrellaestrellaestrellaestrella(2)

Actualizado el 18 de Abril del 2021 (Publicado el 15 de Marzo del 2021)gráfica de visualizaciones de la versión: 1.2
1.418 visualizaciones desde el 15 de Marzo del 2021
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from tkinter import *
from tkinter import ttk
#from pydub import AudioSegment
from pydub.generators import Sine, Square, Triangle, Sawtooth, Pulse
from pydub.playback import play
import threading
 
class m_keyboard():
    def __init__(self):
        self.kb = Tk()
        self.kb.geometry("1163x325")
        self.kb.title("Sound Keyboard")
        self.kb.configure(background="gray12")
 
        self.duration = IntVar()
        self.duration.set(1000)
        self.WaveForms = ["Sine","Square","Triangle","Sawtooth","Pulse"]
        validatecommand = self.kb.register(self.valid_duration)
 
        Label(self.kb,text="DURATION:",bg="gray12",fg="white").place(x=15,y=10)
        Label(self.kb,text="WAVEFORM:",bg="gray12",fg="white").place(x=185,y=10)
        Label(self.kb,text="FADE OUT",bg="gray12",fg="white").place(x=1094,y=10)
        Label(self.kb,text="FADE IN",bg="gray12",fg="white").place(x=1015,y=10)
        Label(self.kb,text="GAIN",bg="gray12",fg="white").place(x=911,y=10)
        Label(self.kb,text="VOLUME",bg="gray12",fg="white").place(x=820,y=10)
        self.durEntry = Entry(self.kb,width=8,textvariable=self.duration,validate="key",validatecommand=(validatecommand, "%S"))
        self.durEntry.place(x=90,y=10)
        self.waveEntry = ttk.Combobox(self.kb,width=9,state='readonly')
        self.waveEntry.place(x=268,y=10)
        self.waveEntry["values"] = self.WaveForms
        self.waveEntry.current(0)
        self.slider = Scale(self.kb,bg="gray12",fg="white",from_=500, to=1)
        self.slider.set(1)
        self.slider.place(x=1102,y=32)
        self.slider2 = Scale(self.kb,bg="gray12",fg="white", from_=500, to=1)
        self.slider2.set(1)
        self.slider2.place(x=1018,y=32)
        self.slider3 = Scale(self.kb,bg="gray12",fg="white",from_=100, to=-100)
        self.slider3.set(0)
        self.slider3.place(x=904,y=32)
        self.slider4 = Scale(self.kb,bg="gray12",fg="white", from_=20, to=0)
        self.slider4.set(0)
        self.slider4.place(x=827,y=32)
 
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(261.63)).place(x=15,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(293.66)).place(x=96,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(329.63)).place(x=177,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(349.23)).place(x=258,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(392.00)).place(x=339,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(440.00)).place(x=420,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(493.88)).place(x=501,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(523.25)).place(x=582,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(587.33)).place(x=663,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(659.25)).place(x=744,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(698.45)).place(x=825,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(783.99)).place(x=906,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(880.00)).place(x=987,y=150)
        Button(self.kb,width=10,height=11,command=lambda:self.init_task(987.76)).place(x=1068,y=150)
 
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(277.18)).place(x=72,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(311.13)).place(x=153,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(369.99)).place(x=315,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(415.30)).place(x=396,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(466.16)).place(x=477,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(554.36)).place(x=639,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(622.25)).place(x=720,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(740.00)).place(x=882,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(830.60)).place(x=963,y=150)
        Button(self.kb,width=5,height=6,bg="black",activebackground="light gray",command=lambda:self.init_task(932.32)).place(x=1044,y=150)
 
        self.kb.mainloop()
 
    def make_tone(self,freq):
        if self.waveEntry.get() == "Sine":
            tone = (Sine(freq).to_audio_segment(duration=int(self.durEntry.get())).fade_out(self.slider.get()).fade_in(self.slider2.get())).apply_gain(self.slider3.get())+self.slider4.get()
        elif self.waveEntry.get() == "Square":
            tone = (Square(freq).to_audio_segment(duration=int(self.durEntry.get())).fade_out(self.slider.get()).fade_in(self.slider2.get())).apply_gain(self.slider3.get())+self.slider4.get()
        elif self.waveEntry.get() == "Triangle":
            tone = (Triangle(freq).to_audio_segment(duration=int(self.durEntry.get())).fade_out(self.slider.get()).fade_in(self.slider2.get())).apply_gain(self.slider3.get())+self.slider4.get()
        elif self.waveEntry.get() == "Sawtooth":
            tone = (Sawtooth(freq).to_audio_segment(duration=int(self.durEntry.get())).fade_out(self.slider.get()).fade_in(self.slider2.get())).apply_gain(self.slider3.get())+self.slider4.get()
        elif self.waveEntry.get() == "Pulse":
            tone = (Pulse(freq).to_audio_segment(duration=int(self.durEntry.get())).fade_out(self.slider.get()).fade_in(self.slider2.get())).apply_gain(self.slider3.get())+self.slider4.get()
        play(tone)
 
    def valid_duration(self,char):
        return char in "0123456789"
 
    def init_task(self,fr):
        t = threading.Thread(target=self.make_tone, args=(fr,))
        t.start()
 
if __name__=="__main__":
    m_keyboard()



Comentarios sobre la versión: 1.2 (2)

Fernando
14 de Abril del 2021
estrellaestrellaestrellaestrellaestrella
Hola, buenas al ejecutar el programa en vc me lo ejecuta bien, pero al intentar presionar una tecla me manda los siguiente errores:
C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydub\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydub\utils.py:184: RuntimeWarning: Couldn't find ffplay or avplay - defaulting to ffplay, but may not work
warn("Couldn't find ffplay or avplay - defaulting to ffplay, but may not work", RuntimeWarning)
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "c:/Users/FBoni/Documents/Cursos/Practica - Python/teclamusical.py", line 84, in make_tone
play(tone)
File "C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydub\playback.py", line 71, in play
_play_with_ffplay(audio_segment)
File "C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydub\playback.py", line 15, in _play_with_ffplay
seg.export(f.name, "wav")
File "C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydub\audio_segment.py", line 867, in export
out_f, _ = _fd_or_path_or_tempfile(out_f, 'wb+')
File "C:\Users\FBoni\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pydub\utils.py", line 60, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\FBoni\\AppData\\Local\\Temp\\tmp680h9xqb.wav'

Hay forma de resolver esta falla? y ya instale el ffmpeg y nada.

Saludos
Responder
Imágen de perfil
16 de Abril del 2021
estrellaestrellaestrellaestrellaestrella
¿Qué procedimiento empleaste para instalar ffmpeg?
Responder

Comentar la versión: 1.2

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6943