Use Matlab for audio cutting in time domain

When learning cryptography, the final project selected is digital audio information hiding technology.
The first is to choose a wav format audio file. The wav file found on the Internet takes too long, so it needs to be segmented in the time domain and a short section is intercepted for experimentation.

1. Code

The MATLAB code is as follows:

[x,fs] = audioread('music.wav');
start_time = 0;
end_time = 33;
Y_new = x((fs * start_time + 1) : fs * end_time, 1);
audiowrite('music_new.wav', Y_new, fs); 

2. Code Explanation

  • The first line: the audioread function reads a piece of audio. Among them, the returned parameter x represents audio data, which is actually a matrix, and here is a matrix of 15016334*2. fs represents the sampling rate, the unit is Hz, that is, how many points are collected per second.

Here, if the audio is too large, there will be an out of memory problem, that is, it exceeds the memory of MATLAB. The solution is to reduce the audio sampling rate. You can use the FFmpeg tool to change the audio sampling rate with a single audio processing command. ffmpeg -i input -ar 16000 -ac 1 output
Explanation: ffmpeg is the tool name, -i means input, input is the file name, -ar means the following 16000 is the sampling rate, -ac 1 means mono, and output means the output file name. In this case, this problem can be solved.

  • The second and third lines: start_time and end_time are the start and end time, in seconds.

  • Fourth line: The audio data Y_new of the output file is also a matrix similar to x. So just select a section of the original matrix and write a new file. So what this line of code means is: the sampling rate multiplied by the number of seconds at the beginning, and the number of seconds until the sampling rate is multiplied by the end. The unit of sampling rate is sampling point/s

  • The fifth line: audiowrite means to write a file, Y_new is audio data, fs means the sampling rate. Then an audio file to be intercepted will be generated in your directory.

Note: The original wavread() and wavwrite() have been replaced by audioread() and audiowrite(). The function also changed from the original only wav format file to .wav .mp4 .m4a .flac .ogg .oga and other formats.

Guess you like

Origin blog.csdn.net/mahoon411/article/details/110729591