语音处理/语音识别基础(四)- 语音文件读取与播放

本文介绍了在Matlab中如何读取wav文件,调整音频属性如采样率和音量,播放不同速度和反转波形,以及录制和保存音频文件的过程。同时展示了使用内置函数如`audioread`, `audioPlay`, 和 `audiorecorder`进行音频处理的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文分享如何在 matlab 里面读取 wav 文件,播放 wav 文件,以及如何录制语音文件,保存语音文件。

代码中演示了如何改变音频文件的采样率(影响到播放速度),音量(影响到听到的声音大小)。

以及录制的数据如何保存到文件中。 如果使用其他的编程语言,也能通过相关的函数类库达到类似的效果,比如 Python :Playing and Recording Sound in Python – Real Python

Matlab 2021 中读取 wav 文件

文件来自:  http://mirlab.org/jang/books/audiosignalprocessing/example/sunday.wav 

[y, fs] = audioread('sunday.wav');
sound(y, fs);		% Playback of the sound data (播放此音讯)
time=(1:length(y))/fs;	% Time vector on x-axis (时间轴的向量)
plot(time, y);		% Plot the waveform w.r.t. time (画出时间轴上的波形)

画出的图形如下:

打印出音频文件的参数信息:

myAudioRead 库函数来自: http://mirlab.org/jang/books/audiosignalprocessing/example.rar

fileName = 'welcome.wav';

au = myAudioRead(fileName);
y=au.signal; fs=au.fs; nbits=au.nbits;
fprintf('Information of the sound file "%s":\n', fileName);
fprintf('Duration = %g seconds\n', length(y)/fs);
fprintf('Sampling rate = %g samples/second\n', fs);
fprintf('Bit resolution = %g bits/sample\n', nbits);

打印出来音频的总时长,采样率,采样位深(采样位分辨率)后,就可以进一步进行分析。

Matlab 中播放音频文件,播放3倍,15倍音量大小(震幅)的音频

au = myAudioRead('welcome.wav');
y=au.signal; fs=au.fs; nbits=au.nbits;

% 播放音频:Playback with original amplitude (播放 1 倍震幅的音訊)
audioPlay(au);

% Playback with 3 times the original amplitude (播放 3 倍震幅的音訊)
au.signal = 3*y;
audioPlay(au);	 


au.signal = 15*y;
audioPlay(au);	% Playback with 15 times the original amplitude (播放 15 倍震幅的音訊)

以不同采用率播放(不同速度播放)

% Playback at the original speed (播放 1.0 倍速度的音訊)

au.signal = y; % 恢复震幅
audioPlay(au);


% Playback at 0.8 times the original speed (播放 0.8 倍速度的音訊)
au.fs = 0.8*fs;
audioPlay(au);

% 播放 0.5 倍速度的音訊,像牛叫的声音
au.fs = 0.5*fs;
audioPlay(au);



% Playback at 1.2 times the original speed (播放 1.2 倍速度的音訊)

au.fs = 1.2*fs;
audioPlay(au);



% Playback at 2 times the original speed (播放 2 倍速度的音訊) 

au.fs = 2*fs
audioPlay(au);

改变波形,比如把信号沿着x轴反转(y轴 * -1),再听声音, 听到的没有变化,说明声音的相位并不影响人的感知。

% Playback of the original signal (播放正常的音訊波形)
au.fs = fs;
au.y = y;
audioPlay(au);

% Playback of the up-down flipped signal (播放上下顛倒的音訊波形)
au.y = - y;
audioPlay(au);

% Playback of the left-right flipped signal (播放前後顛倒的音訊波形)
au.y = flipud(y);
audioPlay(au);

 使用 audioplayer, play 来播放音频文件


apObj=audioplayer(y, fs);
apObj.SampleRate=16000;		% Change the sample rate to 16000
play(apObj);

% 可以使用 doc play, doc audioplayer 来查看在线帮助,也可以使用 help 看命令行帮助。

录制音频文件

% The commands wavplay and wavrecord are only supported in Microsoft Windows platform.
% audiorecorder(Fs, NBITS, NCHANS) creates an audiorecorder object 
fs=16000;		% Sampling rate (取樣頻率)
% duration=2;		% Recording duration (錄音時間)
fprintf('Press any key to start %g seconds of recording...', duration); pause
fprintf('Recording...');
% y=wavrecord(duration*fs, fs);	% duration*fs is the total number of sample points

% record audio, sample rate of fs, and 16 bit, 1 channel
r = audiorecorder(fs, 16, 1)
record(r)
% 等待一定时间
stop(r)

fprintf('Finished recording.\n');
fprintf('Press any key to play the recording...'); pause; 
fprintf('\n');
p = play(r);

前面的 record() 函数是异步录制, 执行之后代码继续运行,知道 stop()。 也可以同步录制,示例如下。

同步指定时长录制音频文件(录制5s)

fs=16000; % Sampling rate 
r = audiorecorder(fs, 16, 1) % sample rate of fs, and 16 bit, 1 channel

recordblocking(r, 5); % speak into microphone...

fprintf('Finished recording.\n');
fprintf('Press any key to play the recording...'); pause; fprintf('\n');

p = play(r);

保存录制的音频文件 

可以通过如下代码,将前面录制的数据保存到 wav 文件。

% 保存录制的音频到文件
% nbits=16;	   	% Bit resolution (每个采样点的信号量, 量化值的位数为 16-bit)
waveFile='test.wav';	% Wav file to be saved

% get sample data from recorder, get data as int16 array:
y = getaudiodata(r, 'int16'); 

% writes data Y to an audio file with name FILENAME, with a sample rate of FS Hz.
% AUDIOWRITE(FILENAME,Y,FS)  
audiowrite(waveFile, y, fs);
fprintf('Finished writing %s\n', waveFile);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值