Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to implement python-samplerate for live microphone #1

Open
saibharani opened this issue Apr 19, 2017 · 8 comments
Open

how to implement python-samplerate for live microphone #1

saibharani opened this issue Apr 19, 2017 · 8 comments

Comments

@saibharani
Copy link

I want to convert sample rate of live microphone from pyaudio at 44100hz to 16000hz and use it with pocketsphinx can anyone please help to convert samplerate of live recording chunk by chunk with python-samplerate.
Thank you

@tuxu
Copy link
Owner

tuxu commented Apr 20, 2017

There is an example with live resampling of synthetic data using the callback API here. The code would be quite similar using audio input and sounddevice.InputStream. If you already get the live audio with pyaudio, you could use the full API instead and call the process method repeatedly.

@saibharani
Copy link
Author

when i am trying to do it as you said using full api it says cannot convert from string to float it is expecting input to be numpy data but my audio is pyaudio which is in pyaudio.pyInt16 format can you please help i want to resample audio for pocketsphinx and i am new to python. so please help me in a little detail way.
Thank you.

@tuxu
Copy link
Owner

tuxu commented Apr 20, 2017

You need to convert the input data to a NumPy array first. This can be easily done with np.fromstring, see here for an example.

@saibharani
Copy link
Author

sorry for troubling you again I tried using np.fromstring but it is not working It doesn't detect anything. Can you please make a program to record audio using pyaudio in pyInt16 format and convert it from 44100hz to 16000hz using python-samplerate so that I can use it in my college project using pocketsphinx. Please help me solve this issue. Thank you.

@tuxu
Copy link
Owner

tuxu commented Apr 20, 2017

The code below should work as expected.

from __future__ import print_function, division
import numpy as np
import pyaudio
import samplerate as sr

input_rate = 44100
target_rate = 16000
chunk = 1024

audio = pyaudio.PyAudio()
stream = audio.open(format=pyaudio.paInt16, channels=1,
                    rate=input_rate, input=True,
                    frames_per_buffer=chunk)

resampler = sr.Resampler()
ratio = target_rate / input_rate

for i in range(5):
    raw_data = stream.read(chunk)
    data = np.fromstring(raw_data, dtype=np.int16)
    resampled_data = resampler.process(data, ratio)
    print('{} -> {}'.format(len(data), len(resampled_data)))
    # Do something with resampled_data

stream.stop_stream()
stream.close()
audio.terminate()

@saibharani
Copy link
Author

Thank you very much for the code i will try it and will tell you if it works for me. Thanks again.

@tuxu
Copy link
Owner

tuxu commented Apr 22, 2017 via email

@besimali
Copy link

The above script seems to return a numpy array of floats although the input np array contained int16.

It is not a bug, but something to keep in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants