Mercurial > touhou
comparison pytouhou/ui/music.py @ 321:61adb5453e46
Implement music playback.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 13 Jun 2012 15:29:43 +0200 |
parents | |
children | c412df42aa15 |
comparison
equal
deleted
inserted
replaced
320:1a4ffdda8735 | 321:61adb5453e46 |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 | |
16 from pyglet.media import AudioData | |
17 from pyglet.media.riff import WaveSource | |
18 | |
19 | |
20 class InfiniteWaveSource(WaveSource): | |
21 def __init__(self, filename, start, end, file=None): | |
22 WaveSource.__init__(self, filename, file) | |
23 | |
24 self._start = self.audio_format.bytes_per_sample * start | |
25 self._end = self.audio_format.bytes_per_sample * end | |
26 | |
27 if self._end > self._max_offset: | |
28 raise Exception #TODO | |
29 | |
30 self._duration = None | |
31 | |
32 | |
33 def _get_audio_data(self, bytes): | |
34 if bytes % self.audio_format.bytes_per_sample != 0: | |
35 bytes -= bytes % self.audio_format.bytes_per_sample | |
36 | |
37 length = bytes | |
38 while True: | |
39 size = min(length, self._end - self._offset) | |
40 data = self._file.read(size) | |
41 if size == length: | |
42 break | |
43 | |
44 self._offset = self._start | |
45 self._file.seek(self._offset + self._start_offset) | |
46 length -= size | |
47 | |
48 self._offset += length | |
49 | |
50 timestamp = float(self._offset) / self.audio_format.bytes_per_second | |
51 duration = float(bytes) / self.audio_format.bytes_per_second | |
52 | |
53 return AudioData(data, bytes, timestamp, duration) |