September 13, 2013♦
from array import array from time import sleep import pygame from pygame.mixer import Sound, get_init, pre_init class Note(Sound): def __init__(self, frequency, volume=.1): self.frequency = frequency Sound.__init__(self, self.build_samples()) self.set_volume(volume) 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 xrange(period): if time < period / 2: samples[time] = amplitude else: samples[time] = -amplitude return samples if __name__ == "__main__": pre_init(44100, -16, 1, 1024) pygame.init() Note(440).play(-1) sleep(5)
This program generates and plays a 440 Hz tone for 5 seconds. It can be extended to generate the spectrum of notes with a frequency table or the frequency formula. Because the rewards in Send are idealized ocean waves, they can also be represented as tones. Each level has a tone in its goal and a tone based on where the player's disc lands. Both play at the end of a level, sounding harmonic for a close shot and discordant for a near miss. The game can dynamically create these tones using the program as a basis.
I'm also building an algorithmically generated song: Silk Routes (Scissored). Here is an example of how it sounds so far.