#!/usr/bin/env python3
import pygame
from array import (array)
from pygame.mixer import (Sound, get_init, pre_init)
class Electrode(Sound):
SEQUENCE = [
['A4', 440.00], ["A#", 466.00], ['B', 493.88], ['C', 523.25],
["C#", 554.00], ['D', 587.33], ["D#", 622.00], ['E', 659.25],
['F', 698.46], ["F#", 739.00], ['G', 783.99], ["G#", 830.00],
['A5', 880.00]
]
N = len(SEQUENCE)
SCALE={k:v for (k,v) in SEQUENCE}
def __init__(self, coefficient=0, volume=0.5):
(self.active, self.duration) = (True, 3)
self.coefficient(coefficient)
Sound.__init__(self, self.build_samples())
self.set_volume(volume)
def __call__(self):
if self.active:
self.play(self.duration)
return self
def coefficient(self, coefficient):
index = int(min(max(1, coefficient), Electrode.N-1))
self.key = Electrode.SEQUENCE[index][0]
self.frequency = Electrode.SCALE[self.key]
return self
def set(self, on):
self.active = True if on else False
return self
def build_samples(self):
period = int(round(get_init()[0] / self.frequency))
samples = array("h", [0] * period)
amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
for time in range(period):
if time < (period / 2):
samples[time] = +amplitude
else:
samples[time] = -amplitude
return samples
if __name__ == "__main__":
from time import sleep
pre_init(44100, -16, 1, 1024)
pygame.init()
for n in range(0,Electrode.N):
Electrode(n)()
sleep(1)
Making ticking sounds of different frequencies. Solves problem with alsa and jack by avoiding them
60 Views Asked by jlettvin At
1
Replaced pysine calls with pygame calls.