How to loop and store a recordblocking?

16 vues (au cours des 30 derniers jours)
Andrés Liu
Andrés Liu le 20 Fév 2015
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

Réponses (1)

Samayochita
Samayochita le 28 Fév 2025
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.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by