Beispiel #1
0
    def generate(self, sample_rate):
        audio = Audio(sample_rate)

        for signal in self.signals:
            audio += signal.realise(sample_rate)

        return audio
Beispiel #2
0
 def WAV_to_Audio(filename): # filename can also be file-like object (used in _IO_ffmpeg)
     import wave
     
     with wave.open(filename, "rb") as file:
         buffer = np.frombuffer(file.readframes(file.getnframes()),
                                [np.uint8,np.int16,None,np.int32][file.getsampwidth()-1])
         buffer = buffer.astype(np.float64)
         
         buffer = np.reshape(buffer,
                             newshape=(file.getnchannels(),
                                       int(len(buffer)/file.getnchannels()))[::-1]).T.copy(order='C')
         buffer /= 2**(8*file.getsampwidth()-1)
         
         from gensound.audio import Audio
         audio = Audio(file.getframerate())
         audio.from_array(buffer)
     
     return audio
Beispiel #3
0
 def AIFF_to_Audio(filename):
     # when making changes, consider WAV_to_Audio as well
     import aifc
     
     with aifc.open(filename, "rb") as file:
         buffer = np.frombuffer(file.readframes(file.getnframes()),
                                [np.uint8,np.int16,None,np.int32][file.getsampwidth()-1])
         buffer = buffer.byteswap().astype(np.float64) # byte swap for AIFF only, not WAV
         
         buffer = np.reshape(buffer,
                             newshape=(file.getnchannels(),
                                       int(len(buffer)/file.getnchannels()))[::-1]).T.copy(order='C')
         buffer /= 2**(8*file.getsampwidth()-1)
         
         from gensound.audio import Audio
         audio = Audio(file.getframerate())
         audio.from_array(buffer)
         
     return audio
Beispiel #4
0
    def generate(self, sample_rate):
        audio = Audio(sample_rate)

        #### Phase Inference: TODO should this be here?

        phase = 0  # phase inference

        for signal in self.sequence:
            if isinstance(signal, Oscillator) and signal.phase is None:
                signal._phase = phase
                phase = (phase + signal.end_phase) % (2 * np.pi
                                                      )  # phase inference
            else:
                phase = 0

            audio.concat(signal.realise(sample_rate))
            # TODO assymetric with Mix since we don't overload audio.concat

        return audio
Beispiel #5
0
    def __init__(self, filename):
        # TODO consider adding "new_copy"/"force_copy"/"unique"/"new" boolean arg
        # would be implemented either by appending random nonce to key,
        # or adding counter for number of copies of base key
        self.key = f"WAV_{filename}"

        audio = None

        if self._key() not in Raw.cache:
            audio = Audio.from_file(filename)

        # TODO copy again? so the cache will be eternally independent?
        super().__init__(audio)
Beispiel #6
0
    def realise(self, sample_rate):
        """ returns Audio instance.
        parses the entire signal tree recursively
        """
        audio = self.generate(sample_rate)

        if not isinstance(audio, Audio):
            audio = Audio(sample_rate).from_array(audio)

        for transform in self.transforms:
            transform.realise(audio=audio)

        return audio
Beispiel #7
0
def iDFT(freqs, sample_rate):
    """Freqs is list of [Xreal, Ximag]
    """

    N = len(freqs)

    xs = []

    for n in range(N):
        f = 2 * np.pi * n / N
        x = sum([
            real * np.cos(f * m) + imag * np.sin(f * m)
            for m, (real, imag) in enumerate(freqs)
        ])
        # TODO make sure that imag*cos and real*sin really do cancel each other

        xs.append(x / N)

    audio = Audio(sample_rate)
    audio.from_array(xs)

    return audio