Skip to content

Instantly share code, notes, and snippets.

@mabdrabo
Created January 28, 2014 23:05
Show Gist options
  • Save mabdrabo/8678538 to your computer and use it in GitHub Desktop.
Save mabdrabo/8678538 to your computer and use it in GitHub Desktop.
Simple script to record sound from the microphone, dependencies: easy_install pyaudio
import pyaudio
import wave
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print "recording..."
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print "finished recording"
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
@el07694
Copy link

el07694 commented Jun 24, 2022

You can use pydub module for this i think.
For example

from pydub import AudioSegment
...
...
...
    data = stream.read(CHUNK)
    slice = AudioSegment(data, sample_width=???, frame_rate=???, channels=???)
    slice_len = len(slice) #slice_len is the duration of data frame in milliseconds

@sujantkumarkv
Copy link

hey all, i know this is too old a gist, but i came here from nvidia forum. Any help is appreciated.

This script is assuming i physically have the jetson right and using its microphone? because i'm connected to it via ssh on my macbook & physically very far. So, i guess I somehow need to send the audio packets/bytes to it?

and does pyaudio help with that? anyone has clues how to proceed? thanks :)

cc @mabdrabo @el07694 @deluxerootit @sinanaybar @bg172 @TheEccentricDuck @shilan @kongkip @YveOms @creboy @jdelange @muthiyanbhushan @Raman99444 @soni30 @Shinoy-12 @salilsaxena @davekimble2 @ajfarkas @kanik9 @progware @ecambronero @ClaudiaMarchPiris @PiepsC @Skykiller664 @pomarec @s1st3r @morenol @sharpriy @riadibadulla @marijnruyts @Dinesh2903 @danidz @HarrisMx @maelfosso @xe1gyq @rdhara @MihailRis @Akshay0724 @nmnhut2010 @ashishmd @rachit1994 @kevinmgamboa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment