{"id":331952,"date":"2022-04-14T21:00:27","date_gmt":"2022-04-14T21:00:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=331952"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=331952","title":{"rendered":"<span>\u0417\u0432\u0443\u043a \u0432 DIY \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u0448\u0435 \u0445\u043e\u0431\u0431\u0438\/DIY, \u043a\u0430\u043a \u0438 <a href=\"https:\/\/habr.com\/ru\/post\/645491\/\" rel=\"noopener noreferrer nofollow\">\u043c\u043e\u0451<\/a>, \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u043c, \u0442\u043e \u043d\u0430 \u043a\u0430\u043a\u043e\u043c \u0442\u043e \u044d\u0442\u0430\u043f\u0435 \u0432\u0430\u043c \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0432\u0443\u043a. \u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0437\u0432\u0443\u043a\u0435 \u0438 \u043e\u0431\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u043e\u043f\u044b\u0442\u043e\u043c. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0431\u0443\u0434\u0435\u043c, \u043f\u0440\u043e \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0437\u0432\u0443\u043a\u0430 \u043d\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435.  \u0412\u043e\u0437\u044c\u043c\u0435\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u043f\u043e\u0434 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c Linux, \u043d\u043e \u0438 \u043f\u043e\u0434 Windows \u0434\u043e\u043b\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u042f\u0437\u044b\u043a \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e Python. \u041a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e \u0432 Linux \u0435\u0441\u0442\u044c ALSA \u0438 \u0434\u043b\u044f \u043d\u0435\u0435 \u0435\u0441\u0442\u044c python \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <a href=\"https:\/\/pypi.org\/project\/pyalsaaudio\/\" rel=\"noopener noreferrer nofollow\">pyalsaaudio<\/a>. \u0412 \u043a\u0430\u043a\u043e\u043c \u0442\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0430 \u044f \u0432\u0438\u0434\u0435\u043b \u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0434\u043b\u044f \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0438 \u0433\u0440\u043e\u043c\u043a\u043e\u0441\u0442\u0438. \u041f\u0440\u043e \u0433\u0440\u043e\u043c\u043a\u043e\u0441\u0442\u044c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u043e\u0442\u043e\u043c. \u0422.\u043a. \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0439 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0434\u0440\u0443\u0433\u0443\u044e \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0443\u044e &#8212; <a href=\"https:\/\/pypi.org\/project\/sounddevice\/\" rel=\"noopener noreferrer nofollow\">sounddevice<\/a>. \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043e \u043d\u0430 \u043a\u0440\u043e\u0441\u0441 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0439 C\/C++ \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 <a href=\"http:\/\/www.portaudio.com\/\" rel=\"noopener noreferrer nofollow\">portaudio<\/a>. <\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f sounddevice \u0435\u0441\u0442\u044c \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0438 \u043e\u043d \u0432\u0435\u0441\u044c\u043c\u0430 \u043f\u0440\u043e\u0441\u0442:<\/p>\n<pre><code class=\"python\">import sounddevice as sd sd.play(myarray, fs)<\/code><\/pre>\n<p>\u0437\u0434\u0435\u0441\u044c <em>myarray<\/em> \u2014 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e \u0437\u0432\u0443\u043a\u043e\u043c, \u043d\u0430\u0434\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e sounddevice \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u044b <a href=\"https:\/\/numpy.org\/\" rel=\"noopener noreferrer nofollow\">numpy<\/a>. <em>fs<\/em> \u2014 \u0431\u0438\u0442\u0440\u0435\u0439\u0442 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u0447\u0430\u0441\u0442\u043e\u0442\u0430 \u0434\u0438\u0441\u043a\u0440\u0435\u0442\u0438\u0437\u0430\u0446\u0438\u0438. \u041e\u0442\u043a\u0443\u0434\u0430 \u044d\u0442\u043e \u0432\u0441\u0435 \u0431\u0440\u0430\u0442\u044c? \u041d\u0443\u0436\u043d\u0430 \u0435\u0449\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"https:\/\/pypi.org\/project\/SoundFile\/\" rel=\"noopener noreferrer nofollow\">soundfile<\/a>.<\/p>\n<pre><code class=\"python\">import sounddevice as sd import soundfile as sf  myarray, fs = sf.read('my-file.wav') sd.play(myarray, fs) sd.wait() #  \u0436\u0434\u0435\u043c \u043a\u043e\u043d\u0446\u0430 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u0438\u043d\u0443\u0441, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445 \u0442\u043e \u043d\u0443\u0436\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043b\u044e\u0431\u043e\u0439 \u0437\u0432\u0443\u043a\u043e\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u043e\u0442\u044b: <\/p>\n<pre><code class=\"python\">duration=1 \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0441\u0435\u043a  frequency = 1000 # \u0447\u0430\u0441\u0442\u043e\u0442\u0430 \u0413\u0446  samplerate = 24000 # \u0431\u0438\u0442\u0440\u0435\u0439\u0442  amp = 10000 # \u0430\u043c\u043f\u043b\u0438\u0442\u0443\u0434\u0430  #############  #\u0431\u0435\u0440\u0435\u043c numpy  import numpy as np  t = np.arange(duration * samplerate) \/ samplerate  signal = amp*np.sin(2 * np.pi * frequency * t)  sd.play(signal) sd.wait()<\/code><\/pre>\n<p>\u0415\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u044b\u0431\u043e\u0440 \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d \u0441\u0438\u043d\u0443\u0441\u043e\u043c. \u041d\u0435 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<pre><code class=\"python\">import sounddevice as sd  import soundfile as sf  fs = 48000 # \u0431\u0438\u0442\u0440\u0435\u0439\u0442 \u0437\u0430\u043f\u0438\u0441\u0438  duration=3 # \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438  rec = sd.rec(int(duration * fs), samplerate=fs, channels=1, blocking=True)  sd.wait()  sf.write('my-file.wav', rec, fs)<\/code><\/pre>\n<p>\u0415\u0441\u0442\u044c \u0435\u0449\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u044f. \u041a\u0430\u043a\u0438\u0435 \u0442\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438. \u041d\u043e \u044d\u0442\u043e \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043c\u0430\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0442.\u043a. \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e  \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438\u2026 \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u043e\u0442\u043e\u043a\u0430\u043c. \u041f\u0440\u043e \u043f\u043e\u0442\u043e\u043a\u0438 \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442 \u0438 \u0434\u0430\u0436\u0435 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438,  \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0442\u043c\u0435\u0447\u0443, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a:<\/p>\n<pre><code class=\"python\">stream = sd.Stream(device=(dev_in, dev_out),             samplerate=41000,             blocksize=blocksize,             dtype=\"int16\",             channels=(1,2),             callback=callback_fun)<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u043f\u043e\u0442\u043e\u043a\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435. \u0412 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0435\u0441\u0442\u044c \u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435, \u043d\u043e \u044d\u0442\u043e\u0442 \u043c\u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c. \u0412 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0435 <em>device<\/em>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043d\u0430 \u043d\u0430 \u044d\u0442\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430. \u0413\u0434\u0435 \u0436\u0435 \u0438\u0445 \u0431\u0440\u0430\u0442\u044c? \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <em>sd.query_devices()<\/em>:<\/p>\n<pre><code class=\"python\">device_info = sd.query_devices()<\/code><\/pre>\n<p> \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c\u0438 \u0437\u0432\u0443\u043a\u043e\u0432\u044b\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u043e\u0439:<\/p>\n<pre><code class=\"xml\">0 sof-hda-dsp: - (hw:0,0), ALSA (2 in, 0 out)  1 sof-hda-dsp: - (hw:0,3), ALSA (0 in, 2 out) 2 sof-hda-dsp: - (hw:0,4), ALSA (0 in, 2 out) 3 sof-hda-dsp: - (hw:0,5), ALSA (0 in, 2 out) 4 sof-hda-dsp: - (hw:0,6), ALSA (4 in, 0 out) 5 sof-hda-dsp: - (hw:0,7), ALSA (4 in, 0 out) 6 sysdefault, ALSA (128 in, 0 out) 7 samplerate, ALSA (128 in, 0 out) 8 speexrate, ALSA (128 in, 0 out) 9 pulse, ALSA (32 in, 32 out) 10 upmix, ALSA (8 in, 0 out)  11 vdownmix, ALSA (6 in, 0 out) 12 default, ALSA (32 in, 32 out)<\/code><\/pre>\n<p>\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <em>device<\/em> \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043b\u0438 \u043d\u043e\u043c\u0435\u0440 \u0438\u043b\u0438 \u0438\u043c\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <em>device = (12,12)<\/em>. \u042f \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u043e\u0442 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430 USB \u043a\u0430\u043c\u0435\u0440\u044b, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437 \u2014 \u0432\u044b\u043b\u0435\u0442\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u043d\u0430 portaudio, \u043a\u0430\u043a \u044f \u043f\u043e\u043d\u044f\u043b \u0442\u0430\u043a\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u043c\u0435\u043d\u044f. C <em>default<\/em> \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043c\u044f \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430:<\/p>\n<pre><code class=\"python\">dev_in = sd.query_devices(kind=\"input\")[\"name\"] dev_out = sd.query_devices(kind=\"output\")[\"name\"]<\/code><\/pre>\n<p> \u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442, \u0438 \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>callback<\/em> \u0444\u0443\u043d\u043a\u0446\u0438\u044e. <\/p>\n<pre><code class=\"python\">def callback_fun(indata, outdata, frames, time, status):   if status:      print(status)    outdata[:] = indata<\/code><\/pre>\n<p>\u041e\u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043f\u043e\u0442\u043e\u043a\u0430. \u0417\u0434\u0435\u0441\u044c, \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b \u043f\u043e\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u0438\u0433\u043d\u0430\u043b \u0441 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0443\u0441\u043b\u044b\u0448\u0430\u0442\u044c. \u0418\u043b\u0438 \u0435\u0441\u043b\u0438 \u043a\u0442\u043e \u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u043e\u0433\u043e \u0430\u0441\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u0430, \u0442\u043e \u0441\u0438\u0433\u043d\u0430\u043b \u0441 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430 \u043d\u0430\u0434\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0441\u0430, \u0430 \u0441\u0438\u0433\u043d\u0430\u043b \u0441 \u0441\u0438\u043d\u0442\u0435\u0437\u0430\u0442\u043e\u0440\u0430 \u0433\u043e\u043b\u043e\u0441\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u2014 queue. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043a\u0430\u043a \u043f\u0440\u043e\u0438\u0433\u0440\u0430\u0442\u044c WAV \u0444\u0430\u0439 \u0441 \u0434\u0438\u0441\u043a\u0430? \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import queue  fifo = queue.Queue()  inp_fifo = queue.Queue()  ############# def play(file):     with sf.SoundFile(file,mode=\"r\") as f:         data = f.read(blocksize,dtype='int16')         while len(data):             if not fifo.full():                 data = f.read(blocksize,dtype='int16')                 fifo.put(data) ################# def callback_fun(indata, outdata, frames, time, status):     inp_fifo.put(bytes(indata.copy())) # \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0441 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430     if status:         print(status)     inp = np.zeros(len(outdata), dtype=np.int16)     if not fifo.empty() :         bf = np.frombuffer(fifo.get(), dtype=np.int16)         inp[:bf.shape[0]] = bf #     outdata[:] = inp.reshape(-1,1)<\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0447\u0443, \u0435\u0441\u043b\u0438 \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u0431\u044b\u043b \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <em>channels=(1,2)<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043d\u0430 \u0432\u0445\u043e\u0434 \u0443 \u043d\u0430\u0441 1 \u2014 \u043c\u043e\u043d\u043e \u0441\u0438\u0433\u043d\u0430\u043b, \u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 2 \u2014 \u0441\u0442\u0435\u0440\u0435\u043e. \u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u0434\u0430\u0435\u0442\u0435 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u043c\u043e\u043d\u043e \u0441\u0438\u0433\u043d\u0430\u043b \u0432 \u043f\u043e\u0442\u043e\u043a, \u0442\u043e \u043e\u043d \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0437\u0432\u0435\u0434\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043a\u0430\u043d\u0430\u043b\u0430. <\/p>\n<p>\u041f\u043e\u0439\u0434\u0435\u043c \u0434\u0430\u043b\u0435\u0435. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0447\u0442\u043e \u043a\u0442\u043e \u0442\u043e \u0443\u0436\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0441\u044f \u0443 \u043d\u0430\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u2014 \u0432\u043e\u0442 \u043e\u043d \u0448\u0430\u043d\u0441 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0443 \u0433\u0440\u043e\u043c\u043a\u043e\u0441\u0442\u0438, \u0442.\u00a0\u043a. \u0432 <em>sounddevice<\/em> \u0442\u0430\u043a\u0438\u0445  \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043d\u0435\u0442.<br \/>\u0417\u0430\u0432\u043e\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <em>vol = 1<\/em>, \u0430 \u0434\u0430\u043b\u0435\u0435 \u043c\u0430\u0433\u0438\u044f \u0446\u0438\u0444 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435:<\/p>\n<pre><code class=\"python\"> inp[:bf.shape[0]] = bf \/\/ vol<\/code><\/pre>\n<p> \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c <em>vol<\/em>. \u0415\u0441\u043b\u0438 \u0432\u0437\u044f\u0442\u044c <em>vol = 1000<\/em>, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438<br \/> \u0437\u0430\u043d\u0443\u043b\u0438\u0442\u044c \u0437\u0432\u0443\u043a. \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u0442\u0438 \u0434\u0430\u043b\u044c\u0448\u0435\u2026 \u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0432\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043c\u0435\u0448\u0430\u0442\u044c \u0437\u0432\u0443\u043a \u043e\u0442 \u0434\u0432\u0443\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432:<\/p>\n<pre><code class=\"python\"># \u0441\u043c. \u0412 callback_fun if not fifo1.empty() :         bf1 = np.frombuffer(fifo1.get(), dtype=np.int16)         inp1[:bf1.shape[0]] = bf1 # if not fifo2.empty() :         bf2= np.frombuffer(fifo2.get(), dtype=np.int16)         inp2[:bf2.shape[0]] = bf2 # inp = (inp1 + inp2)\/\/2 outdata[:] = inp.reshape(-1,1)<\/code><\/pre>\n<p>\u0418\u043b\u0438 \u043e\u0434\u0438\u043d \u0441\u0438\u0433\u043d\u0430\u043b \u0432 \u043b\u0435\u0432\u043e\u0435 \u0443\u0445\u0430, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0432 \u043f\u0440\u0430\u0432\u043e\u0435:<\/p>\n<pre><code class=\"python\"># \u0441\u043c. \u0412 callback_fun inp = np.column_stack((inp1,inp2))     outdata[:] = inp<\/code><\/pre>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0442\u0443\u0442 \u0448\u0438\u0440\u043e\u043a\u043e\u0435 \u043f\u043e\u043b\u0435 \u0434\u043b\u044f \u0442\u0432\u043e\u0440\u0447\u0435\u0441\u0442\u0432\u0430. \u0415\u0441\u043b\u0438 \u043a\u0442\u043e \u0442\u043e \u0437\u0430\u0445\u043e\u0447\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u043c\u043e\u0438 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u044f \u043d\u0435 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442, \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438. \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e <a href=\"https:\/\/python-sounddevice.readthedocs.io\/en\/0.4.4\/index.html\" rel=\"noopener noreferrer nofollow\">sounddevice<\/a>.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u0430.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/661037\/\"> https:\/\/habr.com\/ru\/post\/661037\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u0448\u0435 \u0445\u043e\u0431\u0431\u0438\/DIY, \u043a\u0430\u043a \u0438 <a href=\"https:\/\/habr.com\/ru\/post\/645491\/\" rel=\"noopener noreferrer nofollow\">\u043c\u043e\u0451<\/a>, \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u043c, \u0442\u043e \u043d\u0430 \u043a\u0430\u043a\u043e\u043c \u0442\u043e \u044d\u0442\u0430\u043f\u0435 \u0432\u0430\u043c \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0432\u0443\u043a. \u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0437\u0432\u0443\u043a\u0435 \u0438 \u043e\u0431\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u043e\u043f\u044b\u0442\u043e\u043c. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0431\u0443\u0434\u0435\u043c, \u043f\u0440\u043e \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0437\u0432\u0443\u043a\u0430 \u043d\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435.  \u0412\u043e\u0437\u044c\u043c\u0435\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u043f\u043e\u0434 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c Linux, \u043d\u043e \u0438 \u043f\u043e\u0434 Windows \u0434\u043e\u043b\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u042f\u0437\u044b\u043a \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e Python. \u041a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e \u0432 Linux \u0435\u0441\u0442\u044c ALSA \u0438 \u0434\u043b\u044f \u043d\u0435\u0435 \u0435\u0441\u0442\u044c python \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <a href=\"https:\/\/pypi.org\/project\/pyalsaaudio\/\" rel=\"noopener noreferrer nofollow\">pyalsaaudio<\/a>. \u0412 \u043a\u0430\u043a\u043e\u043c \u0442\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0430 \u044f \u0432\u0438\u0434\u0435\u043b \u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0434\u043b\u044f \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0438 \u0433\u0440\u043e\u043c\u043a\u043e\u0441\u0442\u0438. \u041f\u0440\u043e \u0433\u0440\u043e\u043c\u043a\u043e\u0441\u0442\u044c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u043e\u0442\u043e\u043c. \u0422.\u043a. \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0439 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0434\u0440\u0443\u0433\u0443\u044e \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0443\u044e &#8212; <a href=\"https:\/\/pypi.org\/project\/sounddevice\/\" rel=\"noopener noreferrer nofollow\">sounddevice<\/a>. \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043e \u043d\u0430 \u043a\u0440\u043e\u0441\u0441 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0439 C\/C++ \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 <a href=\"http:\/\/www.portaudio.com\/\" rel=\"noopener noreferrer nofollow\">portaudio<\/a>. <\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f sounddevice \u0435\u0441\u0442\u044c \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0438 \u043e\u043d \u0432\u0435\u0441\u044c\u043c\u0430 \u043f\u0440\u043e\u0441\u0442:<\/p>\n<pre><code class=\"python\">import sounddevice as sd sd.play(myarray, fs)<\/code><\/pre>\n<p>\u0437\u0434\u0435\u0441\u044c <em>myarray<\/em> \u2014 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e \u0437\u0432\u0443\u043a\u043e\u043c, \u043d\u0430\u0434\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e sounddevice \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u044b <a href=\"https:\/\/numpy.org\/\" rel=\"noopener noreferrer nofollow\">numpy<\/a>. <em>fs<\/em> \u2014 \u0431\u0438\u0442\u0440\u0435\u0439\u0442 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u0447\u0430\u0441\u0442\u043e\u0442\u0430 \u0434\u0438\u0441\u043a\u0440\u0435\u0442\u0438\u0437\u0430\u0446\u0438\u0438. \u041e\u0442\u043a\u0443\u0434\u0430 \u044d\u0442\u043e \u0432\u0441\u0435 \u0431\u0440\u0430\u0442\u044c? \u041d\u0443\u0436\u043d\u0430 \u0435\u0449\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <a href=\"https:\/\/pypi.org\/project\/SoundFile\/\" rel=\"noopener noreferrer nofollow\">soundfile<\/a>.<\/p>\n<pre><code class=\"python\">import sounddevice as sd import soundfile as sf  myarray, fs = sf.read('my-file.wav') sd.play(myarray, fs) sd.wait() #  \u0436\u0434\u0435\u043c \u043a\u043e\u043d\u0446\u0430 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f<\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u0438\u043d\u0443\u0441, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445 \u0442\u043e \u043d\u0443\u0436\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u043b\u044e\u0431\u043e\u0439 \u0437\u0432\u0443\u043a\u043e\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u043e\u0442\u044b: <\/p>\n<pre><code class=\"python\">duration=1 \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0441\u0435\u043a  frequency = 1000 # \u0447\u0430\u0441\u0442\u043e\u0442\u0430 \u0413\u0446  samplerate = 24000 # \u0431\u0438\u0442\u0440\u0435\u0439\u0442  amp = 10000 # \u0430\u043c\u043f\u043b\u0438\u0442\u0443\u0434\u0430  #############  #\u0431\u0435\u0440\u0435\u043c numpy  import numpy as np  t = np.arange(duration * samplerate) \/ samplerate  signal = amp*np.sin(2 * np.pi * frequency * t)  sd.play(signal) sd.wait()<\/code><\/pre>\n<p>\u0415\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u044b\u0431\u043e\u0440 \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d \u0441\u0438\u043d\u0443\u0441\u043e\u043c. \u041d\u0435 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0437\u0430\u043f\u0438\u0441\u044c:<\/p>\n<pre><code class=\"python\">import sounddevice as sd  import soundfile as sf  fs = 48000 # \u0431\u0438\u0442\u0440\u0435\u0439\u0442 \u0437\u0430\u043f\u0438\u0441\u0438  duration=3 # \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438  rec = sd.rec(int(duration * fs), samplerate=fs, channels=1, blocking=True)  sd.wait()  sf.write('my-file.wav', rec, fs)<\/code><\/pre>\n<p>\u0415\u0441\u0442\u044c \u0435\u0449\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u044f. \u041a\u0430\u043a\u0438\u0435 \u0442\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438. \u041d\u043e \u044d\u0442\u043e \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043c\u0430\u043b\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0442.\u043a. \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e  \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438\u2026 \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u043e\u0442\u043e\u043a\u0430\u043c. \u041f\u0440\u043e \u043f\u043e\u0442\u043e\u043a\u0438 \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442 \u0438 \u0434\u0430\u0436\u0435 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438,  \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0442\u043c\u0435\u0447\u0443, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a:<\/p>\n<pre><code class=\"python\">stream = sd.Stream(device=(dev_in, dev_out),             samplerate=41000,             blocksize=blocksize,             dtype=\"int16\",             channels=(1,2),             callback=callback_fun)<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f \u043f\u043e\u0442\u043e\u043a\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0438 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435. \u0412 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0435\u0441\u0442\u044c \u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u044b\u0435, \u043d\u043e \u044d\u0442\u043e\u0442 \u043c\u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c. \u0412 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0435 <em>device<\/em>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043d\u0430 \u043d\u0430 \u044d\u0442\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430. \u0413\u0434\u0435 \u0436\u0435 \u0438\u0445 \u0431\u0440\u0430\u0442\u044c? \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <em>sd.query_devices()<\/em>:<\/p>\n<pre><code class=\"python\">device_info = sd.query_devices()<\/code><\/pre>\n<p> \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c\u0438 \u0437\u0432\u0443\u043a\u043e\u0432\u044b\u043c\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u043e\u0439:<\/p>\n<pre><code class=\"xml\">0 sof-hda-dsp: - (hw:0,0), ALSA (2 in, 0 out)  1 sof-hda-dsp: - (hw:0,3), ALSA (0 in, 2 out) 2 sof-hda-dsp: - (hw:0,4), ALSA (0 in, 2 out) 3 sof-hda-dsp: - (hw:0,5), ALSA (0 in, 2 out) 4 sof-hda-dsp: - (hw:0,6), ALSA (4 in, 0 out) 5 sof-hda-dsp: - (hw:0,7), ALSA (4 in, 0 out) 6 sysdefault, ALSA (128 in, 0 out) 7 samplerate, ALSA (128 in, 0 out) 8 speexrate, ALSA (128 in, 0 out) 9 pulse, ALSA (32 in, 32 out) 10 upmix, ALSA (8 in, 0 out)  11 vdownmix, ALSA (6 in, 0 out) 12 default, ALSA (32 in, 32 out)<\/code><\/pre>\n<p>\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <em>device<\/em> \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043b\u0438 \u043d\u043e\u043c\u0435\u0440 \u0438\u043b\u0438 \u0438\u043c\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <em>device = (12,12)<\/em>. \u042f \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0432\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u043e\u0442 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430 USB \u043a\u0430\u043c\u0435\u0440\u044b, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437 \u2014 \u0432\u044b\u043b\u0435\u0442\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u043d\u0430 portaudio, \u043a\u0430\u043a \u044f \u043f\u043e\u043d\u044f\u043b \u0442\u0430\u043a\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0443 \u043c\u0435\u043d\u044f. C <em>default<\/em> \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043c\u044f \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430:<\/p>\n<pre><code class=\"python\">dev_in = sd.query_devices(kind=\"input\")[\"name\"] dev_out = sd.query_devices(kind=\"output\")[\"name\"]<\/code><\/pre>\n<p> \u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442, \u0438 \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>callback<\/em> \u0444\u0443\u043d\u043a\u0446\u0438\u044e. <\/p>\n<pre><code class=\"python\">def callback_fun(indata, outdata, frames, time, status):   if status:      print(status)    outdata[:] = indata<\/code><\/pre>\n<p>\u041e\u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043f\u043e\u0442\u043e\u043a\u0430. \u0417\u0434\u0435\u0441\u044c, \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b \u043f\u043e\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u0438\u0433\u043d\u0430\u043b \u0441 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0443\u0441\u043b\u044b\u0448\u0430\u0442\u044c. \u0418\u043b\u0438 \u0435\u0441\u043b\u0438 \u043a\u0442\u043e \u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u043e\u0433\u043e \u0430\u0441\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u0430, \u0442\u043e \u0441\u0438\u0433\u043d\u0430\u043b \u0441 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430 \u043d\u0430\u0434\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0441\u0430, \u0430 \u0441\u0438\u0433\u043d\u0430\u043b \u0441 \u0441\u0438\u043d\u0442\u0435\u0437\u0430\u0442\u043e\u0440\u0430 \u0433\u043e\u043b\u043e\u0441\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u2014 queue. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043a\u0430\u043a \u043f\u0440\u043e\u0438\u0433\u0440\u0430\u0442\u044c WAV \u0444\u0430\u0439 \u0441 \u0434\u0438\u0441\u043a\u0430? \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">import queue  fifo = queue.Queue()  inp_fifo = queue.Queue()  ############# def play(file):     with sf.SoundFile(file,mode=\"r\") as f:         data = f.read(blocksize,dtype='int16')         while len(data):             if not fifo.full():                 data = f.read(blocksize,dtype='int16')                 fifo.put(data) ################# def callback_fun(indata, outdata, frames, time, status):     inp_fifo.put(bytes(indata.copy())) # \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u0441 \u043c\u0438\u043a\u0440\u043e\u0444\u043e\u043d\u0430     if status:         print(status)     inp = np.zeros(len(outdata), dtype=np.int16)     if not fifo.empty() :         bf = np.frombuffer(fifo.get(), dtype=np.int16)         inp[:bf.shape[0]] = bf #     outdata[:] = inp.reshape(-1,1)<\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0447\u0443, \u0435\u0441\u043b\u0438 \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u0431\u044b\u043b \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <em>channels=(1,2)<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043d\u0430 \u0432\u0445\u043e\u0434 \u0443 \u043d\u0430\u0441 1 \u2014 \u043c\u043e\u043d\u043e \u0441\u0438\u0433\u043d\u0430\u043b, \u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 2 \u2014 \u0441\u0442\u0435\u0440\u0435\u043e. \u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u0434\u0430\u0435\u0442\u0435 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434 \u043c\u043e\u043d\u043e \u0441\u0438\u0433\u043d\u0430\u043b \u0432 \u043f\u043e\u0442\u043e\u043a, \u0442\u043e \u043e\u043d \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0437\u0432\u0435\u0434\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043a\u0430\u043d\u0430\u043b\u0430. <\/p>\n<p>\u041f\u043e\u0439\u0434\u0435\u043c \u0434\u0430\u043b\u0435\u0435. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0447\u0442\u043e \u043a\u0442\u043e \u0442\u043e \u0443\u0436\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0441\u044f \u0443 \u043d\u0430\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u2014 \u0432\u043e\u0442 \u043e\u043d \u0448\u0430\u043d\u0441 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0435\u0433\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0443 \u0433\u0440\u043e\u043c\u043a\u043e\u0441\u0442\u0438, \u0442.\u00a0\u043a. \u0432 <em>sounddevice<\/em> \u0442\u0430\u043a\u0438\u0445  \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043d\u0435\u0442.<br \/>\u0417\u0430\u0432\u043e\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <em>vol = 1<\/em>, \u0430 \u0434\u0430\u043b\u0435\u0435 \u043c\u0430\u0433\u0438\u044f \u0446\u0438\u0444 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435:<\/p>\n<pre><code class=\"python\"> inp[:bf.shape[0]] = bf \/\/ vol<\/code><\/pre>\n<p> \u043d\u0430\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c <em>vol<\/em>. \u0415\u0441\u043b\u0438 \u0432\u0437\u044f\u0442\u044c <em>vol = 1000<\/em>, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438<br \/> \u0437\u0430\u043d\u0443\u043b\u0438\u0442\u044c \u0437\u0432\u0443\u043a. \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u0442\u0438 \u0434\u0430\u043b\u044c\u0448\u0435\u2026 \u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0432\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043c\u0435\u0448\u0430\u0442\u044c \u0437\u0432\u0443\u043a \u043e\u0442 \u0434\u0432\u0443\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432:<\/p>\n<pre><code class=\"python\"># \u0441\u043c. \u0412 callback_fun if not fifo1.empty() :         bf1 = np.frombuffer(fifo1.get(), dtype=np.int16)         inp1[:bf1.shape[0]] = bf1 # if not fifo2.empty() :         bf2= np.frombuffer(fifo2.get(), dtype=np.int16)         inp2[:bf2.shape[0]] = bf2 # inp = (inp1 + inp2)\/\/2 outdata[:] = inp.reshape(-1,1)<\/code><\/pre>\n<p>\u0418\u043b\u0438 \u043e\u0434\u0438\u043d \u0441\u0438\u0433\u043d\u0430\u043b \u0432 \u043b\u0435\u0432\u043e\u0435 \u0443\u0445\u0430, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0432 \u043f\u0440\u0430\u0432\u043e\u0435:<\/p>\n<pre><code class=\"python\"># \u0441\u043c. \u0412 callback_fun inp = np.column_stack((inp1,inp2))     outdata[:] = inp<\/code><\/pre>\n<p>\u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0442\u0443\u0442 \u0448\u0438\u0440\u043e\u043a\u043e\u0435 \u043f\u043e\u043b\u0435 \u0434\u043b\u044f \u0442\u0432\u043e\u0440\u0447\u0435\u0441\u0442\u0432\u0430. \u0415\u0441\u043b\u0438 \u043a\u0442\u043e \u0442\u043e \u0437\u0430\u0445\u043e\u0447\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u043c\u043e\u0438 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u044f \u043d\u0435 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442, \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438. \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e <a href=\"https:\/\/python-sounddevice.readthedocs.io\/en\/0.4.4\/index.html\" rel=\"noopener noreferrer nofollow\">sounddevice<\/a>.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u0430.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/661037\/\"> https:\/\/habr.com\/ru\/post\/661037\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-331952","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331952"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331952\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}