BassMidi - Сorrect way to stop stream from within a synchronization procedure

Started by Alex_Prog,

Alex_Prog

Hi Ian,

First of all, please excuse my English - it's not my native language, so I've used Google Translate.

I have a couple of questions. Yes, it's me again - the "looper" guy. I'm writing a program that plays sections of a MIDI file in a loop. I want the playback to stop smoothly rather than abruptly (which causes an audible click); specifically, I want the notes that have already been triggered to continue sounding (their natural decay/release).

I've managed to get part of this working:
function IsStreamStopped(BASS_Stream: HStream): boolean;
begin
  result:= (GetStreamState(BASS_Stream) = BASS_ACTIVE_STOPPED);
end;

procedure PlayStop(ReleaseDelayMs: Cardinal = 1000); //Default delay 1000 Ms for notes release
var
  Ch: Integer;
begin
  if (MidiLoopStream = 0) or IsStreamStopped(MidiLoopStream) then Exit;

  // For canceling new notes events in MIDIFILTERPROC
  IsStoppingPlay:= true;

  //Stoppind all notes in all channels (Notes go to Release)
  for Ch := 0 to 15 do
    begin
      BASS_MIDI_StreamEvent(MidiLoopStream, Ch, MIDI_EVENT_NOTESOFF, 0);
    end;

  // Set system timer to release time
  PlayStopTimerID := SetTimer(0, 0, ReleaseDelayMs, @PlayStopTimerCallback);
end;


//System timer Callback
procedure PlayStopTimerCallback(HWND: HWND; Msg: UINT; IDEvent: DWORD; Time: DWORD);
var
  Ch: Integer;
begin

  KillTimer(0, PlayStopTimerID);
  PlayStopTimerID := 0;

 
   //Reset channels
  for Ch := 0 to 15 do
    begin
      BASS_MIDI_StreamEvent(MidiLoopStream, Ch, MIDI_EVENT_NOTESOFF, 0);
      BASS_MIDI_StreamEvent(MidiLoopStream, Ch, MIDI_EVENT_SOUNDOFF, 0);
      BASS_MIDI_StreamEvent(MidiLoopStream, Ch, MIDI_EVENT_RESET, 0);
    end;
 
  //Stop Midi Stream
  BASS_ChannelStop(MidiLoopStream);
 
  //Remove Canceling for notes events in MIDIFILTERPROC
  IsStoppingPlay:= false;
end;

function MIDIFILTERPROC(handle: HSTREAM; track: Integer; event: PBASS_MIDI_EVENT;
          seeking: BOOL; user: Pointer): BOOL; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
begin
  ...
 
  //Canceling new notes events
  if IsStoppingPlay then
    begin
      result:= false;
      exit;
    end;
 
  ...
end;

Calling the `PlayStop()` procedure does exactly what I need: the MIDI file playback stops, but the notes already triggered continue to sound. However, this only works when I call the procedure from, for example, a button-click event handler. If I call `PlayStop()` from `SyncProc` (the synchronization procedure), the program freezes—specifically at this line:

`BASS_MIDI_StreamEvent(MidiLoopStream, Ch, MIDI_EVENT_NOTESOFF, 0);`

As I understand it, there's an issue involving playback threads.

My question is: what is the correct way to stop the playback stream from within a synchronization procedure while achieving that same effect - stopping playback while allowing the already-triggered notes to decay naturally? Is this even possible?

And a second question: If I want to restart playback after the stream has been stopped, artifacts from the previous session (stuck notes) persist. What is the proper way to clear these artifacts from the playback stream—essentially resetting the stream to the state it was in immediately after it was created and the MIDI file was first loaded?

Thanks in advance, Alex.

Ian @ un4seen

Quote from: Alex_ProgMy question is: what is the correct way to stop the playback stream from within a synchronization procedure while achieving that same effect - stopping playback while allowing the already-triggered notes to decay naturally? Is this even possible?

A MIDI stream can be ended at its current position by sending it a MIDI_EVENT_END event:

BASS_MIDI_StreamEvent(MidiLoopStream, 0, MIDI_EVENT_END, 0);

If you also have the BASS_MIDI_DECAYEND flag set on the MIDI stream, then it should decay naturally before ending. MIDI_EVENT_NOTESOFF shouldn't be necessary.

Quote from: Alex_ProgAnd a second question: If I want to restart playback after the stream has been stopped, artifacts from the previous session (stuck notes) persist. What is the proper way to clear these artifacts from the playback stream—essentially resetting the stream to the state it was in immediately after it was created and the MIDI file was first loaded?

BASS_ChannelPlay(restart=true) or BASS_ChannelSetPosition(pos=0) should reset it. If it doesn't, please confirm how you're creating the stream.

Alex_Prog

Quote from: Ian @ un4seenA MIDI stream can be ended at its current position by sending it a MIDI_EVENT_END event:

BASS_MIDI_StreamEvent(MidiLoopStream, 0, MIDI_EVENT_END, 0);

I tried this approach. The sound does indeed stop, but it does so with an audible click rather than a natural decay. Furthermore, the stream itself does not stop; events continue to arrive at the `MIDIFILTERPROC` callback. In my program, this procedure calculates the current position (measure/beat) based on event ticks, and this counter keeps advancing—meaning the stream itself has not actually stopped.

Alex_Prog

I just checked and found an error in my code: after calling `BASS_MIDI_StreamEvent(handle, 0, MIDI_EVENT_END, 0);`, I was also calling `BASS_ChannelStop(MidiLoopStream);`. Once I removed `BASS_ChannelStop`, the sound stopped correctly, but events continue to arrive—the stream itself does not stop.

Quote from: Ian @ un4seenBASS_ChannelPlay(restart=true) or BASS_ChannelSetPosition(pos=0) should reset it. If it doesn't, please confirm how you're creating the stream.

Stream created from MemoryStream. File loaded, prepared for any other streams (Midi,Casm,OTS,MDB), and Midi Stream loaded to BASS_MIDI_StreamCreateFile:
MidiLoopStream := BASS_MIDI_StreamCreateFile( true,
                                                          aStream.Memory,
                                                          0,
                                                          aStream.Size,
                                                          BASS_SAMPLE_LOOP or BASS_MIDI_DECAYEND
                                                          {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF}
                                                          , 1);

Ian @ un4seen

Try removing the BASS_SAMPLE_LOOP flag. With that set, the stream will loop (instead of stopping) when it ends.

Alex_Prog

Quote from: Ian @ un4seenTry removing the BASS_SAMPLE_LOOP flag. With that set, the stream will loop (instead of stopping) when it ends.
I removed this flag, but no changes, stream do not stop. Sound stopped correctly, with notes release.
MidiLoopStream := BASS_MIDI_StreamCreateFile( true,
                                                          aStream.Memory,
                                                          0,
                                                          aStream.Size,
                                                          BASS_MIDI_DECAYEND
                                                          {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF}
                                                          , 1);

Ian @ un4seen

That's strange. Where are you calling BASS_MIDI_StreamEvent(MIDI_EVENT_END) from and is it reporting success in its return value? Do you hear notes decaying from it?

Alex_Prog

I'm so sorry! its my mistake. It called in SyncProc, and i use wrong handle for stream.

Mistake: Handle vs MidiLoopStream
procedure SyncProcPartChain(Handle : HSYNC; Stream : HSTREAM; data : DWORD; user : Pointer); stdcall;

  res:= BASS_MIDI_StreamEvent([b]Handle[/b], 0, MIDI_EVENT_END, 0);
end;

The sound stopped correctly, stream not stopped.

Must be:
  res:= BASS_MIDI_StreamEvent([b]MidiLoopStream[/b], 0, MIDI_EVENT_END, 0);

Now, sound and stream stopped correctly.

MANY THANKS for you help!

Ian @ un4seen

Good to see you've got it working now. You could also use the SYNCPROC's "Stream" parameter in the BASS_MIDI_StreamEvent call (assuming you want to the end the stream that the sync was set on).