annotate pytouhou/ui/music.py @ 325:cddfd3cb4797

Add music support for >PCB.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 21 Jun 2012 15:01:01 +0200
parents c412df42aa15
children 61caded6b4f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
16 from pyglet.media import AudioData, AudioFormat
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 from pyglet.media.riff import WaveSource
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 class InfiniteWaveSource(WaveSource):
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 def __init__(self, filename, start, end, file=None):
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 WaveSource.__init__(self, filename, file)
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 self._start = self.audio_format.bytes_per_sample * start
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 self._end = self.audio_format.bytes_per_sample * end
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 if self._end > self._max_offset:
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 raise Exception #TODO
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 self._duration = None
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 def _get_audio_data(self, bytes):
324
c412df42aa15 Fix music looping
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
34 bytes -= bytes % self.audio_format.bytes_per_sample
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
324
c412df42aa15 Fix music looping
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
36 data = b''
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 length = bytes
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 while True:
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 size = min(length, self._end - self._offset)
324
c412df42aa15 Fix music looping
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
40 data += self._file.read(size)
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 if size == length:
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 break
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 self._offset = self._start
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 self._file.seek(self._offset + self._start_offset)
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 length -= size
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 self._offset += length
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 timestamp = float(self._offset) / self.audio_format.bytes_per_second
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 duration = float(bytes) / self.audio_format.bytes_per_second
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 return AudioData(data, bytes, timestamp, duration)
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
54
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
55
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
56 def seek(self, timestamp):
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
57 raise NotImplementedError('irrelevant')
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
58
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
59
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
60 class ZwavSource(InfiniteWaveSource):
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
61 def __init__(self, filename, format, file=None):
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
62 if file is None:
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
63 file = open(filename, 'rb')
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
64
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
65 self._file = file
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
66
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
67 assert b'ZWAV' == self._file.read(4)
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
68
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
69 self.audio_format = AudioFormat(
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
70 channels=format.wChannels,
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
71 sample_size=format.wBitsPerSample,
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
72 sample_rate=format.dwSamplesPerSec)
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
73
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
74 self._start_offset = 0
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
75 self._offset = format.intro
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
76
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
77 self._file.seek(self._offset)
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
78 self._start = format.intro + format.start
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
79 self._end = format.intro + format.duration