Created
July 3, 2022 06:59
-
-
Save sonkm3/81a6234bb6269e7e60d29b3494edd995 to your computer and use it in GitHub Desktop.
mokupi #31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "d41291be", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pyaudio\n", | |
"import wave\n", | |
"import sys\n", | |
"import time\n", | |
"\n", | |
"\n", | |
"FORMAT = pyaudio.paInt24\n", | |
"CHANNELS = 2\n", | |
"RATE = 96000\n", | |
"CHUNK = 1024\n", | |
"RECORD_SECONDS = 60\n", | |
"DEVICE_INDEX = 1\n", | |
"\n", | |
"WAVE_OUTPUT_FILENAME = \"output.wav\"\n", | |
"\n", | |
"p = pyaudio.PyAudio()\n", | |
"frames = []\n", | |
"\n", | |
"def callback(in_data, frame_count, time_info, status):\n", | |
" frames.append(in_data)\n", | |
" return(in_data, pyaudio.paContinue)\n", | |
"\n", | |
"stream = p.open(\n", | |
" format=FORMAT,\n", | |
" channels=CHANNELS,\n", | |
" rate=RATE,\n", | |
" input=True,\n", | |
" input_device_index=DEVICE_INDEX,\n", | |
" frames_per_buffer=CHUNK,\n", | |
" start=False,\n", | |
" stream_callback=callback\n", | |
")\n", | |
"\n", | |
"def main():\n", | |
" stream.start_stream()\n", | |
"\n", | |
" time.sleep(RECORD_SECONDS)\n", | |
"\n", | |
" stream.stop_stream()\n", | |
"\n", | |
" stream.close()\n", | |
" p.terminate()\n", | |
"\n", | |
" wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')\n", | |
" wf.setnchannels(CHANNELS)\n", | |
" wf.setsampwidth(p.get_sample_size(FORMAT))\n", | |
" wf.setframerate(RATE)\n", | |
" wf.writeframes(b''.join(frames))\n", | |
" wf.close()\n", | |
"\n", | |
"if __name__ == '__main__':\n", | |
" main()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "dc5900e1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'index': 0, 'structVersion': 1, 'type': 8, 'name': 'ALSA', 'deviceCount': 5, 'defaultInputDevice': 1, 'defaultOutputDevice': 3}\n", | |
"{'index': 0, 'structVersion': 2, 'name': 'bcm2835 Headphones: - (hw:0,0)', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 8, 'defaultLowInputLatency': -1.0, 'defaultLowOutputLatency': 0.0016099773242630386, 'defaultHighInputLatency': -1.0, 'defaultHighOutputLatency': 0.034829931972789115, 'defaultSampleRate': 44100.0}\n", | |
"{'index': 1, 'structVersion': 2, 'name': 'U-22: USB Audio (hw:1,0)', 'hostApi': 0, 'maxInputChannels': 2, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.008684807256235827, 'defaultLowOutputLatency': 0.008707482993197279, 'defaultHighInputLatency': 0.034829931972789115, 'defaultHighOutputLatency': 0.034829931972789115, 'defaultSampleRate': 44100.0}\n", | |
"{'index': 2, 'structVersion': 2, 'name': 'sysdefault', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 128, 'defaultLowInputLatency': -1.0, 'defaultLowOutputLatency': 0.0016099773242630386, 'defaultHighInputLatency': -1.0, 'defaultHighOutputLatency': 0.034829931972789115, 'defaultSampleRate': 44100.0}\n", | |
"{'index': 3, 'structVersion': 2, 'name': 'default', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 128, 'defaultLowInputLatency': -1.0, 'defaultLowOutputLatency': 0.0016099773242630386, 'defaultHighInputLatency': -1.0, 'defaultHighOutputLatency': 0.034829931972789115, 'defaultSampleRate': 44100.0}\n", | |
"{'index': 4, 'structVersion': 2, 'name': 'dmix', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': -1.0, 'defaultLowOutputLatency': 0.021333333333333333, 'defaultHighInputLatency': -1.0, 'defaultHighOutputLatency': 0.021333333333333333, 'defaultSampleRate': 48000.0}\n", | |
"{'index': 1, 'structVersion': 1, 'type': 7, 'name': 'OSS', 'deviceCount': 0, 'defaultInputDevice': -1, 'defaultOutputDevice': -1}\n" | |
] | |
} | |
], | |
"source": [ | |
"import pyaudio\n", | |
"p = pyaudio.PyAudio()\n", | |
"\n", | |
"for host_index in range(0, p.get_host_api_count()):\n", | |
" print(p. get_host_api_info_by_index(host_index))\n", | |
" for device_index in range(0, p. get_host_api_info_by_index(host_index)['deviceCount']):\n", | |
" print(p.get_device_info_by_host_api_device_index(host_index, device_index))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4d1bca79", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment