-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultitone-gen.py
83 lines (58 loc) · 2.24 KB
/
multitone-gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
#
import os
import sys
import sounddevice as sd # https://python-sounddevice.readthedocs.io/en/
import numpy as np # https://numpy.org/doc/stable/reference/
import soundfile as sf # https://python-soundfile.readthedocs.io/
from typing import Type
from functools import singledispatchmethod
import json
OUTPUT_DIR = "./output"
class Tone:
@singledispatchmethod
def __init__(self,hertz,seconds=1,amplitude=0.80):
self.seconds = seconds
self.hertz = hertz
self.amplitude = amplitude
@__init__.register(tuple)
def _(self,arg):
if not len(arg) == 3:
sys.exit("Tuple not 3 items.")
self.hertz, self.amplitude, self.seconds = arg
#raise Exception
input_device, output_device = sd.default.device
output_defaults = sd.query_devices(device=output_device)
sd.default.samplerate = output_defaults["default_samplerate"]
sr = int(sd.default.samplerate)
def generate_tone(d: Type[Tone]):
sample_set = np.arange(d.seconds * sr)
tone = np.sin(2 * np.pi * sample_set * d.hertz / sr)
tone = tone * d.amplitude
return tone
with open('tones.json','r') as json_file:
data = json.load(json_file)
for x in data['tone_data']:
mtf_data = x['mtf_data']
for mtf_tone in mtf_data:
print([i for i in mtf_tone])
tt = []
# construct as tuples of 3 as (frequency, amplitude, duration)
for i in mtf_tone:
if type(i) is list:
i.append(x['duration']); t = tuple(i)
else:
t = tuple([i,x['amplitude'],x['duration']])
tt.append(t)
# build the list of tones
mt = [ Tone(i) for i in tt ]
# construct file name from frequency values
ss = [ str(t.hertz) for t in mt ]
os.makedirs(OUTPUT_DIR, exist_ok=True)
output_filename = f"{OUTPUT_DIR}/mt_{'-'.join(ss)}.ogg"
# generate each tone separately then combine
audio_tones = [ generate_tone(t) for t in mt ]
final_tone = np.average(np.asanyarray(audio_tones),axis=0)
# write mixed tones as multitone frequency to filename
sf.write(output_filename, final_tone, sr, 'VORBIS')
print(f'Wrote file "{output_filename}"...', file=sys.stderr)