2014-03-25 16 views
6

찾을 수있는 모든 예제는 CHANNELS = 1 인 모노입니다. PyAudio의 콜백 메소드를 사용하여 스테레오 또는 멀티 채널 입력을 어떻게 읽고 2D NumPy 배열 또는 여러 1D 배열로 변환합니까? '다중 채널 PyAudio를 NumPy 배열로 변환

def callback(in_data, frame_count, time_info, status): 
    global result 
    global result_waiting 

    if in_data: 
     result = np.fromstring(in_data, dtype=np.float32) 
     result_waiting = True 
    else: 
     print('no input') 

    return None, pyaudio.paContinue 

stream = p.open(format=pyaudio.paFloat32, 
       channels=1, 
       rate=fs, 
       output=False, 
       input=True, 
       frames_per_buffer=fs, 
       stream_callback=callback) 

을하지만 스테레오 입력이 작동하지 않습니다는 result 배열, 두 배 긴 그래서 채널 인터리브 또는 뭔가하는 생각,하지만 난 할 수 있습니다

는 모노 입력의 경우,이 같은 작품 이에 대한 문서를 찾으십시오.

+0

저는 배열을 작성하고 PyAudio로 재생하려고합니다. 이것에 대한 어떤 생각? – SolessChong

+0

@SolessChong 내 대답에 함수를 추가했습니다. – endolith

답변

9

왼쪽 채널부터 샘플로 인터리브 된 것처럼 보입니다. 오른쪽 채널에서 왼쪽 채널 입력과 침묵에 대한 신호로, 내가 얻을 : 이제

result = np.fromstring(in_data, dtype=np.float32) 
result = np.reshape(result, (frames_per_buffer, 2)) 

왼쪽에 액세스 할 수 :

그래서
result = [0.2776, -0.0002, 0.2732, -0.0002, 0.2688, -0.0001, 0.2643, -0.0003, 0.2599, ... 

는 2 차원 배열로 바꿀, 스테레오 스트림으로 그것을 밖으로 분리 채널의 경우 result[:, 0]을 사용하고 오른쪽 채널의 경우 result[:, 1]을 사용합니다.

def decode(in_data, channels): 
    """ 
    Convert a byte stream into a 2D numpy array with 
    shape (chunk_size, channels) 

    Samples are interleaved, so for a stereo stream with left channel 
    of [L0, L1, L2, ...] and right channel of [R0, R1, R2, ...], the output 
    is ordered as [L0, R0, L1, R1, ...] 
    """ 
    # TODO: handle data type as parameter, convert between pyaudio/numpy types 
    result = np.fromstring(in_data, dtype=np.float32) 

    chunk_length = len(result)/channels 
    assert chunk_length == int(chunk_length) 

    result = np.reshape(result, (chunk_length, channels)) 
    return result 


def encode(signal): 
    """ 
    Convert a 2D numpy array into a byte stream for PyAudio 

    Signal should be a numpy array with shape (chunk_size, channels) 
    """ 
    interleaved = signal.flatten() 

    # TODO: handle data type as parameter, convert between pyaudio/numpy types 
    out_data = interleaved.astype(np.float32).tostring() 
    return out_data 
+0

매우 유용합니다. 부분적으로 [이 질문과 관련] (http://stackoverflow.com/questions/22927096/how-to-print-values-of-a-string-full-of-chaos-question-marks/22927836?noredirect=1# comment35005843_22927836) – SolessChong

+0

[오디오 인코딩에 다른 데이터 형식을 사용하려면 (https://stackoverflow.com/a/24985016/3002273) (예 :'np.int16') –