Play audio stream (from microphone) using python vlc

-5

The module i chose to use for playing audio is python-vlc (bindings).

The module i prefer to use for capturing microphone input is pyaudio.

I just want to ask: If there is any way to do it with python.

I don't found any documentation for that.

https://people.csail.mit.edu/hubert/pyaudio/docs/

http://www.olivieraubert.net/vlc/python-ctypes/doc/

https://wiki.videolan.org/python_bindings

The following code works.

import pyaudio
import time

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100

def callback(in_data,frame_count,time_info,status_flags):
    return (in_data, pyaudio.paContinue)
    

def record():
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,output=True,frames_per_buffer=CHUNK,stream_callback=callback)
    input1 = input("Press any key to stop proccess.")
    stream.stop_stream()
    stream.close()
    p.terminate()

if __name__ == '__main__':
    record()

but i want to do it with python-vlc.

I try this:

import pyaudio
import time
import vlc

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100

import ctypes
import io
import sys

MediaOpenCb = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_uint64))
MediaReadCb = ctypes.CFUNCTYPE(ctypes.c_ssize_t, ctypes.c_void_p, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t)
MediaSeekCb = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_uint64)
MediaCloseCb = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p)

def media_open_cb(opaque, data_pointer, size_pointer):
    data_pointer.contents.value = opaque
    size_pointer.contents.value = sys.maxsize
    return 0


def media_read_cb(opaque, buffer, length):
    stream=ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
    new_data = stream.read(length)

    for i in range(len(new_data)):
        buffer[i]=new_data[i]

    return len(new_data)


def media_seek_cb(opaque, offset):
    stream=ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
    stream.seek(offset)
    return 0


def media_close_cb(opaque):
    stream=ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
    stream.close()

callbacks = {
    'open': MediaOpenCb(media_open_cb),
    'read': MediaReadCb(media_read_cb),
    'seek': MediaSeekCb(media_seek_cb),
    'close': MediaCloseCb(media_close_cb)
}

new_frames = []

def callback(in_data,frame_count,time_info,status_flags):
    global new_frames
    new_frames.append(in_data)
    return (in_data, pyaudio.paContinue)
    

def record():
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,output=True,frames_per_buffer=CHUNK,stream_callback=callback)

    instance = vlc.Instance()
    player = instance.media_player_new()
    media = instance.media_new_callbacks(callbacks['open'], callbacks['read'], callbacks['seek'], callbacks['close'], ctypes.cast(ctypes.pointer(ctypes.py_object(stream)), ctypes.c_void_p))
    player.set_media(media)
    player.play()

    input1 = input("Press any key to stop proccess.")
    player.stop()
    stream.stop_stream()
    stream.close()
    p.terminate()

if __name__ == '__main__':
    record()

Output:

[00000224a5aec9a0] mmdevice audio output error: cannot initialize COM (error 0x80010106)
[00000224a8415440] mmdevice audio output error: cannot initialize COM (error 0x80010106)
Press any key to stop proccess.[00000224a842b0c0] imem demux error: Invalid get/release function pointers
[00000224a8446b20] imem stream error: Invalid get/release function pointers
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 232, in 'calling callback function'
  File "record.py", line 27, in media_read_cb
    new_data = stream.read(length)
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python37\lib\site-packages\pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
OSError: [Errno -9977] Can't read from a callback stream
python
windows
vlc
microphone
pyaudio
asked on Stack Overflow Aug 20, 2020 by Chris P • edited Aug 23, 2020 by Chris P

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0