aboutsummaryrefslogtreecommitdiff
path: root/music_sampler/music_file.py
blob: ec50951661196a5b4c23bc841f086ebc6549fb9d (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import threading
import pydub
import time
from transitions.extensions import HierarchicalMachine as Machine

import os.path

import audioop

from .lock import Lock
from .helpers import Config, gain, debug_print, error_print
from .mixer import Mixer
from .music_effect import GainEffect

file_lock = Lock("file")

class MusicFile:
    STATES = [
        'initial',
        'loading',
        'failed',
        {
            'name': 'loaded',
            'children': [
                'stopped',
                'playing',
                'paused',
                'stopping'
            ]
        }
    ]
    TRANSITIONS = [
        {
            'trigger': 'load',
            'source': ['initial', 'failed'],
            'dest': 'loading'
        },
        {
            'trigger': 'fail',
            'source': 'loading',
            'dest': 'failed'
        },
        {
            'trigger': 'unload',
            'source': ['failed', 'loaded_stopped'],
            'dest': 'initial',
        },
        {
            'trigger': 'success',
            'source': 'loading',
            'dest': 'loaded_stopped'
        },
        {
            'trigger': 'start_playing',
            'source': 'loaded_stopped',
            'dest': 'loaded_playing'
        },
        {
            'trigger': 'pause',
            'source': 'loaded_playing',
            'dest': 'loaded_paused'
        },
        {
            'trigger': 'unpause',
            'source': 'loaded_paused',
            'dest': 'loaded_playing'
        },
        {
            'trigger': 'stop_playing',
            'source': ['loaded_playing','loaded_paused'],
            'dest': 'loaded_stopping'
        },
        {
            'trigger': 'stopped',
            'source': 'loaded',
            'dest': 'loaded_stopped',
            'before': 'trigger_stopped_events',
            'unless': 'is_loaded_stopped',
        }
    ]

    def __init__(self, filename, mapping, name=None, gain=1):
        machine = Machine(model=self, states=self.STATES,
                transitions=self.TRANSITIONS, initial='initial',
                auto_transitions=False,
                after_state_change=self.notify_state_change)

        self.state_change_callbacks = []
        self.mapping = mapping
        self.filename = filename
        self.name = name or filename
        self.audio_segment = None
        self.initial_volume_factor = gain
        self.music_lock = Lock("music__" + filename)

        if Config.load_all_musics:
            threading.Thread(name="MSMusicLoad", target=self.load).start()

    def reload_properties(self, name=None, gain=1):
        self.name = name or self.filename
        if gain != self.initial_volume_factor:
            self.initial_volume_factor = gain
            self.stopped()
            self.unload()
            self.load(reloading=True)

    # Machine related events
    def on_enter_initial(self):
        self.audio_segment = None

    def on_enter_loading(self, reloading=False):
        if reloading:
            prefix = 'Rel'
            prefix_s = 'rel'
        else:
            prefix = 'L'
            prefix_s = 'l'

        with file_lock:
            if self.mapping.is_leaving_application:
                self.fail()
                return

            try:
                if self.filename.startswith("/"):
                    filename = self.filename
                else:
                    filename = Config.music_path + self.filename

                debug_print("{}oading « {} »".format(prefix, self.name))
                self.mixer = self.mapping.mixer or Mixer()
                initial_db_gain = gain(self.initial_volume_factor * 100)
                self.audio_segment = pydub.AudioSegment \
                        .from_file(filename) \
                        .set_frame_rate(Config.frame_rate) \
                        .set_channels(Config.channels) \
                        .set_sample_width(Config.sample_width) \
                        .apply_gain(initial_db_gain)
                self.sound_duration = self.audio_segment.duration_seconds
            except Exception as e:
                error_print("failed to {}oad « {} »: {}".format(
                    prefix_s, self.name, e))
                self.loading_error = e
                self.fail()
            else:
                self.success()
                debug_print("{}oaded « {} »".format(prefix, self.name))

    def on_enter_loaded(self):
        self.cleanup()

    def cleanup(self):
        self.gain_effects = []
        self.set_gain(0, absolute=True)
        self.current_audio_segment = None
        self.volume = 100
        self.wait_event = threading.Event()
        self.current_loop = 0

    def on_enter_loaded_playing(self):
        self.mixer.add_file(self)

    # Machine related states
    def is_in_use(self):
        return self.is_loaded(allow_substates=True) and\
                not self.is_loaded_stopped()

    def is_in_use_not_stopping(self):
        return self.is_loaded_playing() or self.is_loaded_paused()

    def is_unloadable(self):
        return self.is_loaded_stopped() or self.is_failed()

    # Machine related triggers
    def trigger_stopped_events(self):
        self.mixer.remove_file(self)
        self.wait_event.set()
        self.cleanup()

    # Actions and properties called externally
    @property
    def sound_position(self):
        if self.is_in_use():
            return self.current_frame / self.current_audio_segment.frame_rate
        else:
            return 0

    def play(self, fade_in=0, volume=100, loop=0, start_at=0):
        self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True)
        self.volume = volume
        if loop < 0:
            self.last_loop = float('inf')
        else:
            self.last_loop = loop

        with self.music_lock:
            self.current_audio_segment = self.audio_segment
            self.current_frame = int(start_at * self.audio_segment.frame_rate)

        self.start_playing()

        if fade_in > 0:
            db_gain = gain(self.volume, 0)[0]
            self.set_gain(-db_gain)
            self.add_fade_effect(db_gain, fade_in)

    def seek(self, value=0, delta=False):
        if not self.is_in_use_not_stopping():
            return

        with self.music_lock:
            self.abandon_all_effects()
            if delta:
                frame_count = int(self.audio_segment.frame_count())
                frame_diff = int(value * self.audio_segment.frame_rate)
                self.current_frame += frame_diff
                while self.current_frame < 0:
                    self.current_loop -= 1
                    self.current_frame += frame_count
                while self.current_frame > frame_count:
                    self.current_loop += 1
                    self.current_frame -= frame_count
                if self.current_loop < 0:
                    self.current_loop = 0
                    self.current_frame = 0
                if self.current_loop > self.last_loop:
                    self.current_loop = self.last_loop
                    self.current_frame = frame_count
            else:
                self.current_frame = max(
                        0,
                        int(value * self.audio_segment.frame_rate))

    def stop(self, fade_out=0, wait=False, set_wait_id=None):
        if self.is_loaded_playing():
            ms = int(self.sound_position * 1000)
            ms_fo = max(1, int(fade_out * 1000))

            new_audio_segment = self.current_audio_segment[: ms+ms_fo] \
                .fade_out(ms_fo)
            with self.music_lock:
                self.current_audio_segment = new_audio_segment
            self.stop_playing()
            if wait:
                self.mapping.add_wait(self.wait_event, wait_id=set_wait_id)
                self.wait_end()
        elif self.is_loaded(allow_substates=True):
            self.stopped()

    def abandon_all_effects(self):
        db_gain = 0
        for gain_effect in self.gain_effects:
            db_gain += gain_effect.get_last_gain()

        self.gain_effects = []
        self.set_gain(db_gain)

    def set_volume(self, value, delta=False, fade=0):
        [db_gain, self.volume] = gain(
                value + int(delta) * self.volume,
                self.volume)

        self.set_gain_with_effect(db_gain, fade=fade)

    def set_gain_with_effect(self, db_gain, fade=0):
        if not self.is_in_use():
            return

        if fade > 0:
            self.add_fade_effect(db_gain, fade)
        else:
            self.set_gain(db_gain)

    def wait_end(self):
        self.wait_event.clear()
        self.wait_event.wait()

    # Let other subscribe for state change
    def notify_state_change(self, **kwargs):
        for callback in self.state_change_callbacks:
            callback(self.state)

    def subscribe_state_change(self, callback):
        if callback not in self.state_change_callbacks:
            self.state_change_callbacks.append(callback)
        callback(self.state)

    def unsubscribe_state_change(self, callback):
        if callback in self.state_change_callbacks:
            self.state_change_callbacks.remove(callback)

    # Callbacks
    def finished_callback(self):
        self.stopped()

    def play_callback(self, out_data_length, frame_count):
        if self.is_loaded_paused():
            return b'\0' * out_data_length

        with self.music_lock:
            [data, nb_frames] = self.get_next_sample(frame_count)
            if nb_frames < frame_count:
                if self.is_loaded_playing() and\
                        self.current_loop < self.last_loop:
                    self.current_loop += 1
                    self.current_frame = 0
                    [new_data, new_nb_frames] = self.get_next_sample(
                            frame_count - nb_frames)
                    data += new_data
                    nb_frames += new_nb_frames
                elif nb_frames == 0:
                    # FIXME: too slow when mixing multiple streams
                    threading.Thread(
                            name="MSFinishedCallback",
                            target=self.finished_callback).start()

            return data.ljust(out_data_length, b'\0')

    # Helpers
    def set_gain(self, db_gain, absolute=False):
        if absolute:
            self.db_gain = db_gain
        else:
            self.db_gain += db_gain

    def get_next_sample(self, frame_count):
        fw = self.audio_segment.frame_width

        data = b""
        nb_frames = 0

        segment = self.current_audio_segment
        max_val = int(segment.frame_count())

        start_i = max(self.current_frame, 0)
        end_i   = min(self.current_frame + frame_count, max_val)
        data += segment._data[start_i*fw : end_i*fw]
        nb_frames += end_i - start_i
        self.current_frame += end_i - start_i

        volume_factor = self.volume_factor(self.effects_next_gain(nb_frames))

        data = audioop.mul(data, Config.sample_width, volume_factor)

        return [data, nb_frames]

    def add_fade_effect(self, db_gain, fade_duration):
        if not self.is_in_use():
            return

        self.gain_effects.append(GainEffect(
            "fade",
            self.current_audio_segment,
            self.current_loop,
            self.sound_position,
            self.sound_position + fade_duration,
            gain=db_gain))

    def effects_next_gain(self, frame_count):
        db_gain = 0
        for gain_effect in self.gain_effects:
            [new_gain, last_gain] = gain_effect.get_next_gain(
                    self.current_frame,
                    self.current_loop,
                    frame_count)
            if last_gain:
                self.set_gain(new_gain)
                self.gain_effects.remove(gain_effect)
            else:
                db_gain += new_gain
        return db_gain


    def volume_factor(self, additional_gain=0):
        return 10 ** ( (self.db_gain + additional_gain) / 20)