working fine
This commit is contained in:
parent
72e828aaae
commit
680093eee9
5 changed files with 57 additions and 29 deletions
28
core/nodes/sinenode.py
Normal file
28
core/nodes/sinenode.py
Normal file
|
|
@ -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
|
||||
|
||||
15
core/program.py
Normal file
15
core/program.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from .room import Room
|
||||
|
||||
class Program:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.room = Room()
|
||||
|
||||
def setup(self):
|
||||
|
||||
pass
|
||||
|
||||
def tick(t):
|
||||
|
||||
pass
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
34
test.py
34
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)
|
||||
Loading…
Add table
Reference in a new issue