-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
soothing.py
executable file
·91 lines (74 loc) · 2.76 KB
/
soothing.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
"""
Michael Hirsch, Ph.D.
based on python-acoustics noise generator code
generates soothing sounds, e.g. for sleep enhancement
SUGGESTED USE:
write file to SD card, use it in speaker/phone capable of playing FLAC
Example (assuming fs=16000):
python soothing.py pink 8 -o test.raw # 931MB file, 8 hours of pink noise
ffmpeg -f s16le -ar 16000 -ac 1 -i test.raw test.flac #760.4MB file (or just .wav, gives same size file as .raw)
-nsec: 600 sec was too much for 512MB Rpi 1 in certain noise modes that do advanced computations
future: add GPIO input for Raspberry Pi, Beaglebone, Edison, etc.
I demonstrate using a few different packages.
The simplest way to use this program is by writing a file to disk without playing audio,
you can write a WAV file with
scipy (widely available)
or
scikits.audiolab (a little trickier to install)
saving as RAW requires using an external program like FFMPEG or Goldwave to convert to a more common format.
The huge advantage of RAW is that you can iteratively write several hours of random noise without consuming all your RAM.
"""
from time import sleep
import soothingsounds as ss
from argparse import ArgumentParser
try:
import soothingsounds.plots as plots
from matplotlib.pyplot import show
except (ImportError, RuntimeError):
show = None
soundmod = "sounddevice" # 'pygame'#'pyglet'#'pyaudio' #'pygame' #'scikits.audiolab'
wavapi = "raw" # 'skaudio' #'scipy'
nbitfile = 16
nbitfloat = 32 # from generator.py
def main():
P = ArgumentParser(
description="noise generation program for Raspberry Pi or any Python-capable computer"
)
P.add_argument(
"nmode",
help="what type of white noise [white, pink, brown...]",
nargs="?",
default="pink",
)
P.add_argument(
"hours",
help="how many hours do you want sound generated for [default=8 hours]",
type=float,
nargs="?",
default=8,
)
P.add_argument(
"-fs", help="sampling freq e.g. 16000 or 44100", type=int, default=16000
)
P.add_argument("-o", "--ofn", help="output .wav filename")
P.add_argument(
"-nsec",
help="length of unique noise sequence [seconds]",
type=float,
default=60,
)
p = P.parse_args()
samps = ss.computenoise(p.nmode, p.fs, p.nsec, nbitfloat, nbitfile)
if not p.ofn:
ss.liveplay(samps, p.hours, p.fs, p.nsec, soundmod)
sleep(p.nsec) # for async playback, else sound doesn't play
else:
ss.savenoise(samps, p.hours, p.ofn, p.fs, p.nsec, wavapi)
if show is not None:
plots.time(samps, p.fs, p.nmode)
plots.plotpsd(samps, p.fs, p.nmode)
# plots.plotspectrogram(samps, p.fs, p.nmode)
show()
if __name__ == "__main__":
main()