Pyaudio How to get sound on only one speaker

后端 未结 4 907
误落风尘
误落风尘 2020-12-21 13:13

I\'m using pyaudio in a school project and I\'m trying to get the sound to play on only one speaker at a time. My code is like this:

import pyaudio

p = pyau         


        
相关标签:
4条回答
  • 2020-12-21 13:26

    I also found a method myself. I only changed the play_sound function to

    def play_sound(freq, t, A=0.2):
        wave, A = wavefunc(t, freq, A=A)
        S = sigmoid(t)
        signal = wave*S
        stereo_signal = zeros([len(signal), 2])   #these two lines are new
        stereo_signal[:, 1] = signal[:]     #1 for right speaker, 0 for  left
        stream = p.open(channels=2, 
                    rate=44100, 
                    format=pyaudio.paFloat32, 
                    output=True)
        play_wave(stream,stereo_signal)
        stream.close()
    
    0 讨论(0)
  • 2020-12-21 13:27

    Here is a sample code for another simple and easy to use library for stereo playing.

    import numpy as np
    import simpleaudio as sa
    import matplotlib.pyplot as plt
    # calculate note frequencies Hertz
    A_freq = 120
    Csh_freq = A_freq * 2 ** (4 / 12)
    
    E_freq = A_freq * 2 ** (7 / 12)
    print(Csh_freq)
    print(E_freq)
    # get timesteps for each sample, T is note duration in seconds
    sample_rate = 44100
    T = 0.25
    t = np.linspace(0, T, T * sample_rate, False)
    #print(t)
    # generate sine wave notes
    A_note = np.sin(A_freq * t * 2 * np.pi)
    Csh_note = np.sin(Csh_freq * t * 2 * np.pi)
    E_note = np.sin(E_freq * t * 2 * np.pi)
    
    plt.subplot(211)
    plt.plot(A_note)
    # concatenate notes
    audio = np.hstack((A_note, Csh_note,E_note))
    #print(audio)
    # normalize to 16-bit range
    audio *= 32767 / 1 * np.max(np.abs(audio))
    plt.subplot(212)
    #print(audio)
    # convert to 16-bit data
    audio = audio.astype(np.int16)
    
    # create stereo signal with zeros
    stereo_signal = np.zeros([int(sample_rate*T*3),2],dtype=np.int16)   
    stereo_signal[:,1] = audio[:]
    print(stereo_signal)
    
    #print(audio2)
    plt.plot(audio)
    # start playback
    play_obj = sa.play_buffer(stereo_signal, 2, 2, sample_rate)
    
    #plt.show()
    # wait for playback to finish before exiting
    play_obj.wait_done()
    
    0 讨论(0)
  • 2020-12-21 13:30

    Instead of PyAudio, you could use http://python-sounddevice.rtfd.org/.

    The play() function accepts a mapping argument to select the channel(s) to play back on.

    You could try this to play a mono NumPy array on the right channel:

    import sounddevice as sd
    sd.play(signal, samplerate=44100, mapping=[2])
    
    0 讨论(0)
  • 2020-12-21 13:40

    Right now you are using channels=1, i.e., a mono audio stream. You need to use two channels for stereo and generate the data for the left and right channel separately.

    Here's a short tutorial on how to create stereo data.

    0 讨论(0)
提交回复
热议问题