class Sound(object): def __init__(self, filename, volume): super(Sound, self).__init__() file = os.path.join(BASEDIR, 'sounds', filename) self.volume = volume or 0 self.effect = QSoundEffect() self.effect.setSource(QUrl.fromLocalFile(file)) def play(self, volume=None, loop=True): volume = self.volume if volume is None else volume self.effect.setLoopCount(0) self.effect.setVolume(int(volume) / 100) if loop: self.effect.setLoopCount(QSoundEffect.Infinite) else: self.effect.setLoopCount(1) if not self.effect.isPlaying(): self.effect.play() def stop(self): self.effect.stop() def setVolume(self, volume): self.volume = volume
class noiseGenerator(QThread): def __init__(self, playnoise, volume): super().__init__() pathname = os.path.dirname(os.path.realpath('whitenoise.wav')) self.sound = QSoundEffect() self.sound.setSource(QUrl.fromLocalFile(pathname + '/whitenoise.wav')) self.sound.setLoopCount(QSoundEffect.Infinite) self.settings(playnoise, volume) def settings(self, playnoise, volume): self.playnoise = playnoise self.sound.setVolume(volume) def run(self): if self.playnoise: self.sound.play() def stop(self): self.sound.stop()
class QAVButton(QPushButton): def __init__(self, label): """ builds a custom button and displays it""" # calls super constuctor super(QAVButton, self).__init__(label) self.sound = QSoundEffect() self.volume = 1. self.color = QColor(Qt.gray) self.count = 0 self.duration = 1000 self.rate = 20 self.timer = QTimer() self.timer.timeout.connect(self.update_anim) self.mode = "sin" self.pressed.connect(self.start) self.released.connect(self.stop) self.is_accelerating = False self.update_color_with_alha(1) def setSound(self, sound_file): self.sound.setSource(QUrl.fromLocalFile(sound_file)) self.sound.setLoopCount(QSoundEffect.Infinite) def start(self): self.count = 0 self.sound.play() self.timer.start(self.rate) def stop(self): self.timer.stop() self.sound.stop() def set_color(self, col): self.color = col self.update_color_with_alha(1) def update_color_with_alha(self, alpha): red = self.color.red() green = self.color.green() blue = self.color.blue() bg_style = f"background-color:rgba({red},{green},{blue}, {alpha})" self.setStyleSheet(bg_style) def update_anim(self): #logarithmic (check with perception of luminosity/brighness) #val = math.log(count + 1) #linear if self.mode == "sin": val = math.sin(self.count * 2 * math.pi) / 2 + 0.5 elif self.mode == "lin": val = 1 - self.count else: val = math.log(self.count + 1) alpha = round(val * 100) + 155 self.update_color_with_alha(alpha) amplitude = val * 0.8 + 0.2 self.sound.setVolume(amplitude) self.count = self.count + self.rate / self.duration # print(count) if self.count >= 1 - self.rate / self.duration: self.count = 0 if self.is_accelerating: self.duration = max(200, self.duration * 0.95)