chorizo.py : simple note generator
This commit is contained in:
parent
d501990000
commit
8fd18513ef
@ -1,4 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# TODO
|
||||
|
||||
import pyglet
|
||||
import pyglet.gl as gl
|
||||
|
82
chorizo.py
82
chorizo.py
@ -1,49 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# ============================================================================
|
||||
# This script allows you to play music using a chorizo (or whatever food you
|
||||
# like) keyboard ! See README for more info and links.
|
||||
|
||||
# As all the other scripts in this repository, I release it under a very
|
||||
# permissive license. To make a long story short : do whatever you want with
|
||||
# this script (but try to have fun :), I don't mind. It would be cool to quote
|
||||
# the origin of the script if you reuse it, but you don't have to. I'd like to
|
||||
# be noticed of what you did cool with it (if you think it's worth). :)
|
||||
# Ah, I almost forgot : If by chance we ever meet and you think this script is
|
||||
# worth, you can buy me a soda :)
|
||||
#
|
||||
# Phyks
|
||||
# =============================================================================
|
||||
|
||||
# TODO
|
||||
|
||||
import wave
|
||||
import math
|
||||
import pyaudio
|
||||
import sys
|
||||
|
||||
NomFichier = "temp"
|
||||
Monson = wave.open(NomFichier, 'w')
|
||||
|
||||
channels = 1 # Mono
|
||||
sample_size = 1 # Size of a sample -> 8 bits
|
||||
sampling_freq = 44100 # Sampling frequency
|
||||
# Params
|
||||
# ======
|
||||
nChannels = 1 # Mono
|
||||
sample_size = 2 # Size of a sample -> 1 = 8 bits
|
||||
framerate = 44100 # Sampling frequency
|
||||
length = 2 # Length in seconds
|
||||
frequency = 440
|
||||
level = 1
|
||||
# =====
|
||||
|
||||
freq = 440 # Note frequency
|
||||
length = 5 # Length of the note in seconds
|
||||
level = 150 # Between 0 (max < 0) and 255 (max > 0), 0 is 127
|
||||
if level < 0.0 or level > 1.0:
|
||||
sys.exit(1)
|
||||
|
||||
sample_number = int(length*sampling_freq)
|
||||
filename = "temp"
|
||||
w = wave.open(filename, 'w')
|
||||
|
||||
parameters = (channels, sample_size, sampling_freq, sample_number, 'NONE',
|
||||
'not compressed')
|
||||
# Computed params
|
||||
nFrames = int(length*framerate)
|
||||
max_amplitude = int(2**(sample_size * 8 - 1) - 1)
|
||||
amplitude = max_amplitude*level
|
||||
|
||||
Monson.setparams(parameters) # File header
|
||||
w.setparams((nChannels, sample_size, framerate, nFrames,
|
||||
'NONE', 'not compressed'))
|
||||
|
||||
for i in range(0, sample_number):
|
||||
value = int(128.0+level*math.sin(2.0*math.pi*freq*i/sampling_freq))
|
||||
wave.struct.pack('h', value)
|
||||
sine_wave = []
|
||||
for i in range(min(framerate, nFrames)):
|
||||
sine_wave.append(int(max_amplitude +
|
||||
amplitude*math.sin(2*math.pi*frequency*i/framerate)))
|
||||
|
||||
Monson.close()
|
||||
for i in range(nFrames):
|
||||
sine_wave = int(amplitude*math.sin(2*math.pi*frequency*i/framerate))
|
||||
data = wave.struct.pack('h', sine_wave)
|
||||
# ^ h is for "short" so each value can go from -2**15 to 2**15
|
||||
w.writeframesraw(data)
|
||||
|
||||
wf = open(NomFichier, 'rb')
|
||||
|
||||
p = pyaudio.PyAudio()
|
||||
|
||||
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
|
||||
channels=wf.getnchannels(),
|
||||
rate=wf.getframerate(),
|
||||
output=True)
|
||||
|
||||
data = wf.readframes(CHUNK)
|
||||
|
||||
while data != '':
|
||||
stream.write(data)
|
||||
data = wf.readframes(CHUNK)
|
||||
|
||||
stream.stop_stream()
|
||||
stream.close()
|
||||
|
||||
p.terminate()
|
||||
w.close()
|
||||
|
Loading…
Reference in New Issue
Block a user