想要录制电脑正在播放的输出音频,
运行了以下代码:
clc,clear;
recObj = audiorecorder(44100,16,2,3);%采样率44100,位数16,通道2,声卡序列
disp('Start speaking.')
recordblocking(recObj, 5);
disp('End of Recording.');
% 回放录音数据
play(recObj);
% 获取录音数据
myRecording = getaudiodata(recObj);
% 绘制录音数据波形
plot(myRecording);
命令窗口警告:
错误使用 audiorecorder/set.DeviceID (line 290)
找不到指定的设备。
出错 audiorecorder (line 190)
obj.DeviceID = deviceID;
出错 Untitled (line 2)
recObj = audiorecorder(44100,16,2,3);%采样率44100,位数16,通道2,声卡序列
但输入daq.getDevices明明可以检测到所有音频媒体设备:
ans =
Data acquisition devices:
index Vendor Device ID Description
----- ----------- --------- -------------------------------------------------
1 directsound Audio0 DirectSound 主声音捕获驱动程序
2 directsound Audio1 DirectSound 麦克风阵列 (Realtek High Definition Audio)
3 directsound Audio2 DirectSound 主声音驱动程序
4 directsound Audio3 DirectSound 扬声器 (Realtek High Definition Audio)
输出设备ID=2或3都找不到,0和1还有默认的-1都可以(输入设备:麦克风阵列),如何解决?
额...后来发现,改用Python调用下PyAudio就行,轮子都造好了,没必要在这个功能上死磕MATLAB,
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.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("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
参考来源:https://www.cnpython.com/qa/1424428
PS:实在不行也可以在MATLAB里调用.py文件。