How do I interrupt a sound file that is currently being played?
    조회 수: 14 (최근 30일)
  
       이전 댓글 표시
    
I'm trying to write a program in appDesigner using Audio Toolbox that reads in midi keyboard input and plays an audio recording on repeat while a key is pressed. I've managed to get it to play the correct note, however it plays the sound file to completion and doesn't read anything in while it is playing. Here is my code for reading in messages and playing the notes:
            % Read midi messages from keyboards and play the corresponding
            % notes
             while app.PowerSwitch.Value == "On"
                % Example edited from https://uk.mathworks.com/help/audio/ug/midi-synthesizer.html
                    msgs = midireceive(app.device1);
                    for i = 1:numel(msgs)                        
                        msg = msgs(i);
                        if isNoteOn(msg) == true && msg.Velocity > 0
                            % select note file
                            if msg.Note == 48
                                app.fileReader = dsp.AudioFileReader('OrganArchive\Processed Audio\Swell\Oboe\Audio\Close\C1\S.wav');
                            else
                                app.fileReader = dsp.AudioFileReader('OrganArchive\Processed Audio\Swell\Oboe\Audio\Close\A2\S.wav');
                            end
                            % Play sound
                            while ~isDone(app.fileReader) && msg.Velocity > 0
                                    audioData = app.fileReader();
                                    app.deviceWriter(audioData);
                                drawnow;
                                if msg.Velocity == 0 || isNoteOff(msg) == true 
                                    break;
                                end
                            end
                        elseif isNoteOff(msg) == true
                            % end note
                            % Doesn't work but this is what I've tried:
                            release(app.fileReader);
                            release(app.deviceWriter);
                            break
                        end %end if
                    end % end for loop
                drawnow;
                if app.PowerSwitch.Value == "Off"
                    break
                end
            end % end of loop
             function yes = isNoteOn(msg)
                    if msg.Type == midimsgtype.NoteOn && msg.Velocity > 0 && isNoteOff(msg) == false
                        fprintf('NoteOn \n');
                        yes = true;
                    else
                        yes = false;
                    end         
                end
                function yes = isNoteOff(msg)
                    if msg.Type == midimsgtype.NoteOff || msg.Velocity == 0
                        fprintf('NoteOff \n')
                        yes = true;
                    else
                        yes = false;
                    end
                end     
I've also tried using stop(app.deviceWriter) to stop the note from playing but I get an error.
How can I get it to interrupt/stop the devicewriter so that it plays in real-time? Thanks in advance and if there are any questions please don't hesitate to ask.
댓글 수: 0
답변 (1개)
  Jimmy Lapierre
    
 2021년 5월 3일
        You should write your audio samples by smaller frames (such as 1024 samples per frame). That way, you can stop writing at any time to stop playback. Then, you might notice a delay, so if you want to reduce the latency, you can look at audioPlayerRecorder (even if you don't need the recording, that is setup in a way that latency is much smaller).
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!