-
Notifications
You must be signed in to change notification settings - Fork 2
/
sampleGen.py
129 lines (97 loc) · 3.48 KB
/
sampleGen.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
import json
import audioread
import hsaudiotag.auto
import ansi
import mainLoop
class SampleGen(object):
def __init__(self, filenames, config, **kwargs):
self.framesPerChunk = kwargs.get('framesPerChunk', 1024)
self.onSongChanged = set()
self.onStopped = set()
self.onSample = set()
self.file = None
self.totalFramesRead = 0.0
self.filenameIter = iter(filenames)
self.currentData = None
self.sampleIter = None
self.loadSettings(config)
def loadSettings(self, gcp):
self.bytes_per_frame_per_channel = int(gcp.get_def('input', 'bytes_per_frame_per_channel', 2))
def loadNextFile(self):
self.currentFilename = next(self.filenameIter).encode('utf-8')
if self.currentFilename is None:
mainLoop.currentProcess.queuedCallbacks.append(self.nextChunk)
return
print('Loading file {!r}.'.format(self.currentFilename))
tags = hsaudiotag.auto.File(self.currentFilename)
if not tags.valid:
print("Couldn't read tags!")
else:
print(json.dumps({
'artist': tags.artist,
'album': tags.album,
'title': tags.title,
'duration': tags.duration,
}))
self.tags = tags
self.file = audioread.audio_open(self.currentFilename)
songInfo = {
'channels': self.channels,
'samplerate': self.samplerate,
'duration': self.duration
}
ansi.info("Loaded song {!r}; channels: {}; samplerate: {}; duration: {} (duration from tags: {})",
self.currentFilename,
self.channels,
self.samplerate,
self.duration,
tags.duration
)
mainLoop.currentProcess.queueCall(self.onSongChanged, tags, songInfo)
blockSize = self.framesPerChunk * self.file.channels * self.bytes_per_frame_per_channel
try:
# MAD (pymad)
self.sampleIter = self.file.read_blocks(blockSize)
except AttributeError:
try:
# FFMpeg (command line)
self.sampleIter = self.file.read_data(blockSize)
except AttributeError:
# gstreamer (pygst)
self.sampleIter = iter(self.file)
@property
def elapsedTime(self):
if not self.file:
return 0
return self.totalFramesRead / self.file.samplerate
@property
def channels(self):
if not self.file:
return 0
return self.file.channels
@property
def samplerate(self):
if not self.file:
return 0
return self.file.samplerate
@property
def duration(self):
if not self.file:
return 0
return self.file.duration
def nextChunk(self):
if self.sampleIter is None:
self.loadNextFile()
try:
data = next(self.sampleIter)
except (StopIteration, AttributeError):
# Either we haven't loaded a song yet, or the one we were playing ended. Load another.
self.loadNextFile()
data = next(self.sampleIter)
self.totalFramesRead += self.framesPerChunk
mainLoop.currentProcess.queueCall(self.onSample, data)
self.currentData = data
return data
def close(self):
# Stop stream.
self.file.close()