2

I'm working on a python class which will just play a sound when called. Here is the class

import pyaudio
import numpy as np

class Buzzer(object):

    def __init__(self, seconds = 3.0):

        self.volume = 0.5     # range [0.0, 1.0]
        self.fs = 44100       # sampling rate, Hz, must be integer
        self.duration = seconds   # in seconds, may be float
        self.f = 440.0        # sine frequency, Hz, may be float
        self.samples = (np.sin(2*np.pi*np.arange(self.fs*self.duration)*self.f/self.fs)).astype(np.float32)

        self.stero_signals = np.c_[self.samples, self.samples]
        self.stero_signals[:,0] = 0

    def _play(self):

        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paFloat32,
                channels=2,
                rate=self.fs,
                output=True)

        # play. May repeat with different volume values (if done interactively) 
        stream.write(self.volume*self.samples)

        stream.stop_stream()
        stream.close()

        p.terminate()

    def __call__(self):
        self._play()

I'd like to be able to specify in the __init__ call if the sound plays out of the left of right earbud. How can I alter the _play function to allow for playing the sound out of a single earbud?

I've seen this question, but the answers are not detailed. OP says he found a solution, but his solution involves functions which he has not included.

martineau
  • 119,623
  • 25
  • 170
  • 301
Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73

0 Answers0