-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_snapshot.py
executable file
·149 lines (127 loc) · 4.41 KB
/
play_snapshot.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env python
# -*- coding: utf-8 -*-
###
# Grab SpectrOMat snapshot files and try to play them as audio.
# Copyright (C) 2017-2020 Tobias Dussa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###
from time import sleep
import sys
import contextlib
import numpy
import pygame
channels = 2048 # number of audio channels
samplerate = 44100 # audio sample rate [Hz]
amplitude = 32767 # audio amplitude
fade = 50 # audio fade-in/fade-out time [ms]
def synthesize_sound(amplitudes):
spectrum = numpy.concatenate([numpy.zeros(100), amplitudes, numpy.zeros(samplerate - len(amplitudes) - 99)])
sample = numpy.fft.irfft(spectrum).real
return(normalize(sample, amplitude).astype(numpy.int16))
def normalize(amplitudes, amplitude=1):
loudness = numpy.max(numpy.abs(amplitudes), axis=0)
if (loudness > 0):
amplitudes = numpy.multiply(amplitudes, amplitude/loudness)
return(amplitudes)
def fadeout(sound):
sound.fadeout(fade)
pygame.time.wait(fade)
def demo():
demodelay=100
index = numpy.random.normal(size=channels)
newsound = pygame.sndarray.make_sound(synthesize_sound(index))
newsound.play(-1, fade_ms=fade)
oldsound=newsound
for count in range(8):
pygame.time.wait(demodelay)
print(count)
index = numpy.random.normal(size=channels)
newsound = pygame.sndarray.make_sound(synthesize_sound(index))
fadeout(oldsound)
newsound.play(-1, fade_ms=fade)
oldsound=newsound
index = numpy.zeros(channels)
index[0:1] = numpy.ones(1)
newsound = pygame.sndarray.make_sound(synthesize_sound(index))
fadeout(oldsound)
newsound.play(-1, fade_ms=fade)
oldsound=newsound
for count in range(32):
pygame.time.wait(demodelay)
print(count)
index = numpy.roll(index, 177)
newsound = pygame.sndarray.make_sound(synthesize_sound(index))
fadeout(oldsound)
newsound.play(-1, fade_ms=fade)
oldsound=newsound
index = numpy.zeros(channels)
index[0:200] = numpy.ones(200)
newsound = pygame.sndarray.make_sound(synthesize_sound(index))
fadeout(oldsound)
newsound.play(-1, fade_ms=fade)
oldsound=newsound
for count in range(32):
pygame.time.wait(demodelay)
print(count)
index = numpy.roll(index, 450)
newsound = pygame.sndarray.make_sound(synthesize_sound(index))
fadeout(oldsound)
newsound.play(-1, fade_ms=fade)
oldsound=newsound
@contextlib.contextmanager
def _smart_open(filename, mode='Ur'):
if filename == '-':
if mode is None or mode == '' or 'r' in mode:
fh = sys.stdin
else:
fh = sys.stdout
else:
fh = open(filename, mode)
try:
yield fh
finally:
if filename is not '-':
fh.close()
if __name__ == "__main__":
# Print license info
print('''
SpectrOMat play_snapshot Copyright (C) 2017-2020 Tobias Dussa
This program comes with ABSOLUTELY NO WARRANTY; for details see LICENSE.
This is free software, and you are welcome to redistribute it
under certain conditions; refer to LICENSE for details.
''');
args = sys.argv[1:]
pygame.mixer.pre_init(44100, -16, 1, 1024)
pygame.init()
if args == []:
demo()
sys.exit(0)
sound = None
for filearg in args:
data = []
print(filearg)
with _smart_open(filearg) as handle:
for line in handle:
if line.startswith('#'):
continue
data.append(float(line.split()[-1]))
data = normalize(data, 50)
data = numpy.exp(data)
data = normalize(data)
if not sound is None:
fadeout(sound)
sound = pygame.sndarray.make_sound(synthesize_sound(data))
sound.play(-1, fade_ms=fade)
pygame.time.wait(2500)