Home get audio signal from inbuilt mic
Reply: 1

get audio signal from inbuilt mic

Richard Abbott
1#
Richard Abbott Published in 2018-02-13 11:20:26Z

I want to send the audio signal to Pycharm so I can experiment and see what I can do with it.

Is there a specific function I can type to retrieve raw audio signal from the device I'm working on?

Scott Stensland
2#
Scott Stensland Reply to 2018-02-13 14:18:40Z

this should get your started ... its is some python which records microphone audio and parks it into an output wav file

import pyaudio
import wave
# import urllib.request
import urllib
import struct
import numpy as np
import sounddevice as sd
import matplotlib.pyplot as plt

# Callback function---------------------------------
def callback(indata, outdata, frames, time, status):
#    if status:
#        print(status)
    outdata[:] = indata
#---------------------------------------------------

# Parameters ----------------------------------------------
# Window_Size = 22050 # Point
# Window_Size = 1024 # Point
Window_Size = 2048 # Point
FORMAT_D = pyaudio.paFloat32; FORMAT_W = pyaudio.paInt32
CHANNELS = 1 # Mono
Sample_Rate = 22050 # Hz
dT = 1/Sample_Rate
RECORD_SECONDS = 20 # s
NOFFRAMES = int(Sample_Rate/Window_Size * RECORD_SECONDS)
WAVE_OUTPUT_FILENAME = "/tmp/python_audio_output.wav"
#-----------------------------------------------------------

p = pyaudio.PyAudio()

stream_D = p.open(format=FORMAT_D,
                channels=CHANNELS,
                rate=Sample_Rate,
                input=True,
                frames_per_buffer=Window_Size)

stream_W = p.open(format=FORMAT_W,
            channels=CHANNELS,
            rate=Sample_Rate,
            input=True,
            frames_per_buffer=Window_Size)

print("* recording")

frames = []


for i in range(0, int(Sample_Rate/Window_Size * RECORD_SECONDS)):
    data_D = stream_D.read(Window_Size)
#    data_W = stream_W.read(Window_Size)
    decoded = np.fromstring(data_D, 'Float32')
#    np.savetxt(str(i)+'ttt.txt',transform)
    sd.play(decoded,22050)
#    frames.append(data_W)
#-------------------------------------------------------

print("* done recording")

stream_D.stop_stream()
stream_D.close()
p.terminate()

#plt.plot(transform)
#plt.show()

# Save as a wave file---------------------------
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT_W))
wf.setframerate(Sample_Rate)
wf.writeframes(b''.join(frames))
wf.close()
#-------------------------------------------
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301952 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO