diff --git a/core/nodes/sinenode.py b/core/nodes/sinenode.py new file mode 100644 index 0000000..5e98cf5 --- /dev/null +++ b/core/nodes/sinenode.py @@ -0,0 +1,28 @@ +import math + +from ..soundnode import SoundNode + +class SineNode(SoundNode): + + def __init__(self, freqs, room): + + super().__init__("sine", room) + self.freqs = freqs + + def calc_freqs_volumes(self, t): + # This function returns volumes of each relevant freq + # at tick t + + res = dict() + for freq in self.freqs: + res[freq] = 1 / float(len(self.freqs)) + return res + + def fill_amp_cache(self, t): + + tdct = dict() + for freq, vol in self.calc_freqs_volumes(t).items(): + tdct[freq] = vol * math.sin(self.room.sine_multiplier * freq * t) + + self.amp_cache[t] = tdct + \ No newline at end of file diff --git a/core/program.py b/core/program.py new file mode 100644 index 0000000..715c293 --- /dev/null +++ b/core/program.py @@ -0,0 +1,15 @@ +from .room import Room + +class Program: + + def __init__(self): + + self.room = Room() + + def setup(self): + + pass + + def tick(t): + + pass \ No newline at end of file diff --git a/core/room.py b/core/room.py index a916c6c..311b11a 100644 --- a/core/room.py +++ b/core/room.py @@ -52,6 +52,8 @@ class Room: self.sine_multiplier = TAU / self.sample_rate + self.running_program = None + def set_bit_depth(self, bit_depth): self.bit_depth = bit_depth @@ -59,7 +61,7 @@ class Room: self.min_amp = -int((2**bit_depth) / 2.0) self.sample_width = int(self.bit_depth / 8.0) - + def generate_frames(self, start_t_sec, end_t_sec): frames = [] @@ -69,6 +71,9 @@ class Room: for t in range(start_t, end_t): + if self.running_program: + self.running_program.tick(t) + for node in self.nodes: node.tick_done = False diff --git a/core/soundnode.py b/core/soundnode.py index 6b2b44d..689602a 100644 --- a/core/soundnode.py +++ b/core/soundnode.py @@ -50,7 +50,7 @@ class SoundNode: sample_t = current_t - int(dist / self.room.speed_of_sound) if sample_t in source_node.amp_cache: - return source_node.amp_cache[sample_t] + return {f: a / float(dist) for f, a in source_node.amp_cache[sample_t].items()} return dict() diff --git a/test.py b/test.py index 922428f..cd8d428 100644 --- a/test.py +++ b/test.py @@ -1,41 +1,21 @@ from core.room import Room +from core.prorgam import Program from core.soundnode import SoundNode +from core.nodes.sinenode import SineNode import math R = Room() R.left_sink.start_location = (-1, 0, 0) R.right_sink.start_location = (2, 0, 0) -class SineNode(SoundNode): - - def __init__(self, freq, room): - - super().__init__("sine", room) - self.freq = freq - self.volume = 0.8 - - - def calc_freqs_volumes(self, t): - # This function returns volumes of each relevant freq - # at tick t - - res = dict() - for freq in range(self.freq-20, self.freq+20): - res[freq] = self.volume*0.05*math.sin(freq) - return res - - def fill_amp_cache(self, t): - - tdct = dict() - for freq, vol in self.calc_freqs_volumes(t).items(): - tdct[freq] = vol * math.sin(self.room.sine_multiplier * freq * t) - - self.amp_cache[t] = tdct - -sn = SineNode(440, R) +sn = SineNode([440, 440*2, 440*3, 440*4, 220], R) +sn2 = SineNode([523.25, 523.25*2, 523.25*3, 523.25*4, 220], R) sn.add_air_output(R.left_sink) sn.add_air_output(R.right_sink) +sn2.add_air_output(R.left_sink) +sn2.add_air_output(R.right_sink) +sn2.start_location = (1, 0, 0) R.record('test6.wav', 0, 2) \ No newline at end of file