How to loop and store a recordblocking?

7 ビュー (過去 30 日間)
Andrés Liu
Andrés Liu 2015 年 2 月 20 日
回答済み: Samayochita 2025 年 2 月 28 日
Im trying to create 5 segments of audio recorded during 10 seconds each. The problem is I don't know how to keep track of and save each one withouth the next loop to overwrite the same variable. I have it like this. Hope you can help me, thank you!
recording = audiorecorder;
for i = 1:5
disp('Recording...')
recordblocking(recording , 10);
disp('End of recording')
audio = getaudiodata(recording);
sound(audio);
end

回答 (1 件)

Samayochita
Samayochita 2025 年 2 月 28 日
Hi Andrés Liu,
I understand that you are trying to create 5 audio segments of 10 seconds each without overwriting previous recordings and you are trying to store each recorded segment separately.
I think the issue you are currently facing is because of your code overwriting the ‘audio’ variable in each iteration of the loop, so only the last recorded segment is retained. To store all five audio segments separately, you can use a cell array
audioSegments = cell(1, 5); % Preallocate a cell array
% Record and save each segment
for i = 1:5
disp(['Recording segment ' num2str(i) '...']);
recordblocking(recording, 10);
disp('End of recording.');
% Retrieve and store the audio segments
audioSegments{i} = getaudiodata(recording);
% Save the audio segments
fileNames{i} = ['segment_' num2str(i) '.wav'];
audiowrite(fileNames{i}, audioSegments{i}, Fs); % Fs is the Sampling rate
disp(['Saved: ' fileNames{i}]);
end
After this you can load and play the saved .wav files using:
for i = 1:5
[loadedSegments{i}, Fs] = audioread(['segment_' num2str(i) '.wav']);
disp(['Playing saved segment ' num2str(i) '...']);
sound(loadedSegments{i}, Fs);
pause(12); % Wait for playback to finish before playing the next segment
end
I hope this answer was helpful.

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by