all repos — mgba @ fd6948da4d5c066ecbb298081ed5a8cb661c9c86

mGBA Game Boy Advance Emulator

src/platform/python/mgba/audio.py (view raw)

 1# Copyright (c) 2013-2018 Jeffrey Pfau
 2#
 3# This Source Code Form is subject to the terms of the Mozilla Public
 4# License, v. 2.0. If a copy of the MPL was not distributed with this
 5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6from ._pylib import ffi, lib  # pylint: disable=no-name-in-module
 7
 8
 9class Buffer(object):
10    def __init__(self, native, internal_rate):
11        self._native = native
12        self._internal_rate = internal_rate
13
14    @property
15    def available(self):
16        return lib.blip_samples_avail(self._native)
17
18    def set_rate(self, rate):
19        lib.blip_set_rates(self._native, self._internal_rate, rate)
20
21    def read(self, samples):
22        buffer = ffi.new("short[%i]" % samples)
23        count = self.read_into(buffer, samples, 1, 0)
24        return buffer[:count]
25
26    def read_into(self, buffer, samples, channels=1, interleave=0):
27        return lib.blip_read_samples(self._native, ffi.addressof(buffer, interleave), samples, channels == 2)
28
29    def clear(self):
30        lib.blip_clear(self._native)
31
32
33class StereoBuffer(object):
34    def __init__(self, left, right):
35        self._left = left
36        self._right = right
37
38    @property
39    def available(self):
40        return min(self._left.available, self._right.available)
41
42    def set_rate(self, rate):
43        self._left.set_rate(rate)
44        self._right.set_rate(rate)
45
46    def read(self, samples):
47        buffer = ffi.new("short[%i]" % (2 * samples))
48        count = self.read_into(buffer, samples)
49        return buffer[0:2 * count]
50
51    def read_into(self, buffer, samples):
52        samples = self._left.read_into(buffer, samples, 2, 0)
53        return self._right.read_into(buffer, samples, 2, 1)
54
55    def clear(self):
56        self._left.clear()
57        self._right.clear()