Example #1
0
    def __init__(self, audio_library):
        self.sounds = {}
        folder = 'sounds'
        language = 'srb'
        path = os.path.join(folder, language)

        for root, dirs, files in os.walk(path):
            if len(dirs) == 0:
                values = os.path.split(root)
                letter = values[0][-1]
                t = values[1]
                for file in files:
                    filepath = os.path.join(root, file)
                    if file[-3:] != 'wav':
                        os.remove(filepath)
                        continue
                    if letter not in self.sounds:
                        self.sounds[letter] = {}
                    if t not in self.sounds[letter]:
                        self.sounds[letter][t] = []

                    if audio_library == PYDUB:
                        sound_object = AudioSegment.from_file(filepath,
                                                              format="wav")
                    elif audio_library == SIMPLEAUDIO:
                        sound_object = WaveObject(open(filepath, 'rb').read(),
                                                  num_channels=1,
                                                  bytes_per_sample=2,
                                                  sample_rate=44100)
                    else:
                        raise Exception('audio_library not configured')

                    self.sounds[letter][t].append(sound_object)
Example #2
0
    def _make_wave_object(self, pulses: int):
        """
        Generates a simpleaudio object for the number of pulses asked. Objects are cached for
        performance reasons.

        :param pulses: number of pulses to send 
        """

        if pulses not in self.waveforms:
            from simpleaudio import WaveObject
            amp = self._amp()
            data = b''.join(
                struct.pack('h', int(amp * y))
                for x in zip(*self.make_pulse(pulses)) for y in x)

            self.waveforms[pulses] = WaveObject(data, 2, self.CHANNEL_WIDTH,
                                                self.RATE)

        return self.waveforms[pulses]