From 8fd18513ef633b5aa42230020a03bc990df987e0 Mon Sep 17 00:00:00 2001 From: Phyks Date: Tue, 29 Oct 2013 22:55:08 +0100 Subject: [PATCH] chorizo.py : simple note generator --- 3d_view.py | 4 ++- chorizo.py | 82 +++++++++++++++++++++++++++++++----------------------- color.py | 2 +- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/3d_view.py b/3d_view.py index dfde8c7..93d97e9 100755 --- a/3d_view.py +++ b/3d_view.py @@ -1,4 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +# TODO import pyglet import pyglet.gl as gl diff --git a/chorizo.py b/chorizo.py index 61a13d3..a693590 100755 --- a/chorizo.py +++ b/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() diff --git a/color.py b/color.py index 178f869..1b45bc4 100755 --- a/color.py +++ b/color.py @@ -1,4 +1,4 @@ -#!/bin/env python +#!/bin/env python3 # ============================================================================ # This script maps the 3D position of your hand, as detected by the electrodes