# Path: pym2149/buf.py
# class BufType:
# 
#     def __init__(self, _, dtype):
#         self.dtype = dtype
# 
#     def __call__(self):
#         return MasterBuf(self.dtype)
# 
# Path: pym2149/nod.py
# class BufNode(Node):
# 
#     def __init__(self, buftype, channels = 1):
#         super().__init__()
#         self.masterbuf = buftype()
#         self.channels = channels
#         self.realcallimpl = self.callimpl
#         self.callimpl = self.makeresult
# 
#     def makeresult(self):
#         self.blockbuf = self.masterbuf.ensureandcrop(self.block.framecount * self.channels)
#         resultornone = self.realcallimpl()
#         if resultornone is not None:
#             return resultornone
#         return self.blockbuf
# 
# Path: pym2149/shapes.py
# def level5toamp(level):
# def _amptolevel4(amp):
# def level4to5(level4):
#     def level4to5(cls, data4, introlen = defaultintrolen):
#     def __init__(self, g, introlen = defaultintrolen):
#     def wavelength(self):
# def _meansin(x1, x2):
# def _sinsliceamp(i, n, skew):
# def _sinuslevel4(steps, maxlevel4, skew):
# def makesample5shape(data, signed, is4bit):
# class Shape:


from .buf import BufType
from .nod import BufNode
from .shapes import level4to5, level5toamp, level4tosinus5shape, level4totone5shape
from diapyr.util import singleton
import numpy as np
    'Interrupt setup/routine alternates fixed level between fixedreg and zero.'

    level4toshape = level4totone5shape

@singleton
class SinusEffect(FixedLevelEffect):
    'Interrupt setup/routine sets fixed level to sample value as scaled by fixedreg.'

    level4toshape = level4tosinus5shape

class DigiDrumEffect:
    'Like SinusEffect but in addition toneflagreg and noiseflagreg are virtual and we ignore them.'

    def __init__(self, sample5shape):
        self.sample5shape = sample5shape

    def getshape(self, fixedreg):
        return self.sample5shape

    def putlevel5(self, node):
        node.blockbuf.copybuf(node.chain(node.rtone))

class Dac(BufNode):

    def __init__(self, level, log2maxpeaktopeak, ampshare):
        buftype = BufType.float
        super().__init__(buftype)
        # We take off .5 so that the peak amplitude is about -3 dB:
        maxpeaktopeak = (2 ** (log2maxpeaktopeak - .5)) / ampshare
        # Lookup of ideal amplitudes:

# Path: pym2149/buf.py
# class BufType:
# 
#     def __init__(self, _, dtype):
#         self.dtype = dtype
# 
#     def __call__(self):
#         return MasterBuf(self.dtype)
# 
# Path: pym2149/nod.py
# class BufNode(Node):
# 
#     def __init__(self, buftype, channels = 1):
#         super().__init__()
#         self.masterbuf = buftype()
#         self.channels = channels
#         self.realcallimpl = self.callimpl
#         self.callimpl = self.makeresult
# 
#     def makeresult(self):
#         self.blockbuf = self.masterbuf.ensureandcrop(self.block.framecount * self.channels)
#         resultornone = self.realcallimpl()
#         if resultornone is not None:
#             return resultornone
#         return self.blockbuf
# 
# Path: pym2149/shapes.py
# def level5toamp(level):
# def _amptolevel4(amp):
# def level4to5(level4):
#     def level4to5(cls, data4, introlen = defaultintrolen):
#     def __init__(self, g, introlen = defaultintrolen):
#     def wavelength(self):
# def _meansin(x1, x2):
# def _sinsliceamp(i, n, skew):
# def _sinuslevel4(steps, maxlevel4, skew):
# def makesample5shape(data, signed, is4bit):
# class Shape:


# Path: pym2149/dac.py
from .buf import BufType
from .nod import BufNode
from .shapes import level4to5, level5toamp, level4tosinus5shape, level4totone5shape
from diapyr.util import singleton
import numpy as np

    'Interrupt setup/routine alternates fixed level between fixedreg and zero.'

    level4toshape = level4totone5shape

@singleton
class SinusEffect(FixedLevelEffect):
    'Interrupt setup/routine sets fixed level to sample value as scaled by fixedreg.'

    level4toshape = level4tosinus5shape

class DigiDrumEffect:
    'Like SinusEffect but in addition toneflagreg and noiseflagreg are virtual and we ignore them.'

    def __init__(self, sample5shape):
        self.sample5shape = sample5shape

    def getshape(self, fixedreg):
        return self.sample5shape

    def putlevel5(self, node):
        node.blockbuf.copybuf(node.chain(node.rtone))

class Dac(BufNode):

    def __init__(self, level, log2maxpeaktopeak, ampshare):
        buftype = BufType.float
        super().__init__(buftype)
        # We take off .5 so that the peak amplitude is about -3 dB:
        maxpeaktopeak = (2 ** (log2maxpeaktopeak - .5)) / ampshare
        # Lookup of ideal amplitudes:

        self.leveltopeaktopeak = np.fromiter((level5toamp(v) * maxpeaktopeak for v in range(32)), buftype.dtype)