How to loop and store a recordblocking?
조회 수: 7 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!