2017-12-01 30 views
0

필자는 wav 파일을 재생하는 아주 기본적인 pyaudio 코드를 가지고 있습니다.PyAudio와 Decibel 변경 및 패닝

open_wave = wave.open("tone_silence/l0r1d0_500.wavc",'rb') 

    pyAudio_session = pyaudio.PyAudio() 

    def callback(in_data, frame_count, time_info, status): 
     data = open_wave.readframes(frame_count) 
     return (data, pyaudio.paContinue) 
    pyAudio_stream = pyAudio_session.open(
     format = pyAudio_session.get_format_from_width(open_wave.getsampwidth()), 
     channels = open_wave.getnchannels(), 
     rate = open_wave.getframerate(), 
     output = True, 
     stream_callback=callback) 

    while pyAudio_stream.is_active(): 
     time.sleep(0.1) 

    pyAudio_stream.stop_stream() 
    pyAudio_stream.close() 
    print("Stopped") 
    pyAudio_session.terminate() 

는 내가 스트림의 데시벨 수준을 변경하고 (왼쪽 스피커 만/오른쪽 스피커 만)에 따라 특정 채널에 스테레오 출력을 이동할 수있는 방법을 찾기 위해 인터넷의 구석 구석을 검색 한 필요한 것. 그러나 나는 어떤 방법을 찾을 수 없었다.

언제든지 스트림을 닫을 수 없기 때문에 pydub (실제로이 기능이 있음)로 이동할 수 없습니다. 그것은 전체 오디오를 재생하고 갑작스럽게 닫을 수 없습니다.

답변

0

pydubaudioSegment을 직접 중지 할 수 없지만 this documentation에 따르면 키보드 입력을 쉽게하기 위해 오디오를 1/2 초 청크로 분할합니다. 따라서

우리가 try-except 블록 내에서 while loop을 통해 오디오를 가지고 실행하는 경우, 우리가 사용하여 재생을 중단 할 수 있어야한다 "Ctrl + C"여기

왼쪽에 오디오 또는 오른쪽으로 패닝 작업 예제 코드 다음 here에서 문서에 따라 채널이에 재생을 중지하는 논리 위에 사용 "Ctrl + C"

from pydub import AudioSegment 
from pydub.playback import play 

song = AudioSegment.from_wav("music.wav") 

# pan the sound 15% to the right 
panned_right = song.pan(+0.15) 

# pan the sound 50% to the left 
panned_left = song.pan(-0.50) 

#Play panned left audio 
while True: 
    try: 
     play(panned_left) 
    except KeyboardInterrupt: 
     print "Stopping playing" 
     break 

우리는 플레이 루프에서 깨는에 다시 IDLE 세션 프롬프트를 얻을. 당신은 당신의 필요에 맞출 수 있습니다.

Output: 
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
Stopping playing 
>>> 

이는 플레이 백 중반 스트림을 중단 내장도 패닝 pydub 내 방법과 해킹을 사용의 용이성 제공합니다. 희망이 당신의 문제에 도움이됩니다.

EDIT-1 루프 무기한 재생됩니다 동안 그 문제에 실행할 수 있습니다 솔루션 위
(당신이 그것을 파괴하지 않는 경우.). 그것은 테스트를 위해서만 좋을지도 모릅니다.

또 다른 옵션은 pythonmultiprocess 모듈을 사용하여 오디오 세그먼트를 재생하는 프로세스를 생성하고 필요하지 않은 경우 terminate을 사용하는 것입니다. 이렇게하면 재생 중에 더 많은 제어가 가능해집니다.

다음은 예입니다. 당신이 중지 오디오를 필요로 할 때

from multiprocessing import Process 

def play_audio(seg): 
    play(seg) 

if __name__ == '__main__': 
    p = Process(target=play_audio, args=(panned_left,)) 
    p.start() 

, 단순히 IDE 또는 프로그램 중 하나 p.terminate()를 호출합니다.

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
>>> p.terminate() 
>>>