aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/midi/CircBuf.pas
diff options
context:
space:
mode:
authork-m_schindler <k-m_schindler@b956fd51-792f-4845-bead-9b4dfca2ff2c>2013-12-01 00:31:54 +0000
committerk-m_schindler <k-m_schindler@b956fd51-792f-4845-bead-9b4dfca2ff2c>2013-12-01 00:31:54 +0000
commit9cd462b34d5d839f8addfdf6a9f4469c99a8e653 (patch)
tree6924e73d97578431f2047dec0794761e6f7d07d4 /src/lib/midi/CircBuf.pas
parent38d08e1ba24c550540edbda85992c995553648da (diff)
downloadusdx-9cd462b34d5d839f8addfdf6a9f4469c99a8e653.tar.gz
usdx-9cd462b34d5d839f8addfdf6a9f4469c99a8e653.tar.xz
usdx-9cd462b34d5d839f8addfdf6a9f4469c99a8e653.zip
formating and beautifying. no actual code change, but needs checks.
git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@3011 b956fd51-792f-4845-bead-9b4dfca2ff2c
Diffstat (limited to 'src/lib/midi/CircBuf.pas')
-rw-r--r--src/lib/midi/CircBuf.pas248
1 files changed, 124 insertions, 124 deletions
diff --git a/src/lib/midi/CircBuf.pas b/src/lib/midi/CircBuf.pas
index 571d1ee4..995b82ac 100644
--- a/src/lib/midi/CircBuf.pas
+++ b/src/lib/midi/CircBuf.pas
@@ -5,19 +5,19 @@
{ A First-In First-Out circular buffer.
- Port of circbuf.c from Microsoft's Windows MIDI monitor example.
+ Port of circbuf.c from Microsoft's Windows Midi monitor example.
I did do a version of this as an object (see Rev 1.1) but it was getting too
complicated and I couldn't see any real benefits to it so I dumped it
for an ordinary memory buffer with pointers.
This unit is a bit C-like, everything is done with pointers and extensive
- use is made of the undocumented feature of the Inc() function that
+ use is made of the undocumented feature of the inc() function that
increments pointers by the size of the object pointed to.
All of this could probably be done using Pascal array notation with
range-checking turned off, but I'm not sure it's worth it.
}
-Unit CircBuf;
+unit CircBuf;
interface
@@ -26,158 +26,158 @@ interface
{$H+} // use long strings
{$ENDIF}
-Uses
+uses
Windows,
MMSystem;
type
- { MIDI input event }
- TMidiBufferItem = record
- timestamp: DWORD; { Timestamp in milliseconds after midiInStart }
- data: DWORD; { MIDI message received }
- sysex: PMidiHdr; { Pointer to sysex MIDIHDR, nil if not sysex }
- end;
- PMidiBufferItem = ^TMidiBufferItem;
-
- { MIDI input buffer }
- TCircularBuffer = record
- RecordHandle: HGLOBAL; { Windows memory handle for this record }
- BufferHandle: HGLOBAL; { Windows memory handle for the buffer }
- pStart: PMidiBufferItem; { ptr to start of buffer }
- pEnd: PMidiBufferItem; { ptr to end of buffer }
- pNextPut: PMidiBufferItem; { next location to fill }
- pNextGet: PMidiBufferItem; { next location to empty }
- Error: Word; { error code from MMSYSTEM functions }
- Capacity: Word; { buffer size (in TMidiBufferItems) }
- EventCount: Word; { Number of events in buffer }
- end;
-
- PCircularBuffer = ^TCircularBuffer;
-
-function GlobalSharedLockedAlloc( Capacity: Word; var hMem: HGLOBAL ): Pointer;
-procedure GlobalSharedLockedFree( hMem: HGLOBAL; ptr: Pointer );
-
-function CircbufAlloc( Capacity: Word ): PCircularBuffer;
-procedure CircbufFree( PBuffer: PCircularBuffer );
-function CircbufRemoveEvent( PBuffer: PCircularBuffer ): Boolean;
-function CircbufReadEvent( PBuffer: PCircularBuffer; PEvent: PMidiBufferItem ): Boolean;
+ { Midi input event }
+ TMidiBufferItem = record
+ timestamp: dword; { Timestamp in milliseconds after midiInStart }
+ data: dword; { Midi message received }
+ sysex: PMidiHdr; { Pointer to sysex MIDIHDR, nil if not sysex }
+ end;
+ PMidiBufferItem = ^TMidiBufferItem;
+
+ { Midi input buffer }
+ TCircularBuffer = record
+ RecordHandle: HGLOBAL; { Windows memory handle for this record }
+ BufferHandle: HGLOBAL; { Windows memory handle for the buffer }
+ pStart: PMidiBufferItem; { ptr to start of buffer }
+ pEnd: PMidiBufferItem; { ptr to end of buffer }
+ pNextPut: PMidiBufferItem; { next location to fill }
+ pNextGet: PMidiBufferItem; { next location to empty }
+ Error: word; { error code from MMSYSTEM functions }
+ Capacity: word; { buffer size (in TMidiBufferItems) }
+ EventCount: word; { Number of events in buffer }
+ end;
+
+ PCircularBuffer = ^TCircularBuffer;
+
+function GlobalSharedLockedAlloc(Capacity: word; var hMem: HGLOBAL): pointer;
+procedure GlobalSharedLockedFree(hMem: HGLOBAL; ptr: pointer);
+
+function CircbufAlloc(Capacity: word): PCircularBuffer;
+procedure CircbufFree(PBuffer: PCircularBuffer);
+function CircbufRemoveEvent(PBuffer: PCircularBuffer): boolean;
+function CircbufReadEvent(PBuffer: PCircularBuffer; PEvent: PMidiBufferItem): boolean;
{ Note: The PutEvent function is in the DLL }
implementation
{ Allocates in global shared memory, returns pointer and handle }
-function GlobalSharedLockedAlloc( Capacity: Word; var hMem: HGLOBAL ): Pointer;
+function GlobalSharedLockedAlloc(Capacity: word; var hMem: HGLOBAL): pointer;
var
- ptr: Pointer;
+ ptr: pointer;
begin
- { Allocate the buffer memory }
- hMem := GlobalAlloc(GMEM_SHARE Or GMEM_MOVEABLE Or GMEM_ZEROINIT, Capacity );
-
- if (hMem = 0) then
- ptr := Nil
- else
- begin
- ptr := GlobalLock(hMem);
- if (ptr = Nil) then
- GlobalFree(hMem);
- end;
-
- GlobalSharedLockedAlloc := Ptr;
+ { Allocate the buffer memory }
+ hMem := GlobalAlloc(GMEM_SHARE Or GMEM_MOVEABLE Or GMEM_ZEROINIT, Capacity);
+
+ if hMem = 0 then
+ ptr := nil
+ else
+ begin
+ ptr := GlobalLock(hMem);
+ if ptr = nil then
+ GlobalFree(hMem);
+ end;
+
+ GlobalSharedLockedAlloc := Ptr;
end;
-procedure GlobalSharedLockedFree( hMem: HGLOBAL; ptr: Pointer );
+procedure GlobalSharedLockedFree(hMem: HGLOBAL; ptr: pointer);
begin
- if (hMem <> 0) then
- begin
- GlobalUnlock(hMem);
- GlobalFree(hMem);
- end;
+ if hMem <> 0 then
+ begin
+ GlobalUnlock(hMem);
+ GlobalFree(hMem);
+ end;
end;
-function CircbufAlloc( Capacity: Word ): PCircularBuffer;
+function CircbufAlloc(Capacity: word): PCircularBuffer;
var
- NewCircularBuffer: PCircularBuffer;
- NewMIDIBuffer: PMidiBufferItem;
- hMem: HGLOBAL;
+ NewCircularBuffer: PCircularBuffer;
+ NewMidiBuffer: PMidiBufferItem;
+ hMem: HGLOBAL;
begin
- { TODO: Validate circbuf size, <64K }
- NewCircularBuffer :=
- GlobalSharedLockedAlloc( Sizeof(TCircularBuffer), hMem );
- if (NewCircularBuffer <> Nil) then
- begin
- NewCircularBuffer^.RecordHandle := hMem;
- NewMIDIBuffer :=
- GlobalSharedLockedAlloc( Capacity * Sizeof(TMidiBufferItem), hMem );
- if (NewMIDIBuffer = Nil) then
- begin
- { TODO: Exception here? }
- GlobalSharedLockedFree( NewCircularBuffer^.RecordHandle,
- NewCircularBuffer );
- NewCircularBuffer := Nil;
- end
- else
- begin
- NewCircularBuffer^.pStart := NewMidiBuffer;
- { Point to item at end of buffer }
- NewCircularBuffer^.pEnd := NewMidiBuffer;
- Inc(NewCircularBuffer^.pEnd, Capacity);
- { Start off the get and put pointers in the same position. These
- will get out of sync as the interrupts start rolling in }
- NewCircularBuffer^.pNextPut := NewMidiBuffer;
- NewCircularBuffer^.pNextGet := NewMidiBuffer;
- NewCircularBuffer^.Error := 0;
- NewCircularBuffer^.Capacity := Capacity;
- NewCircularBuffer^.EventCount := 0;
- end;
- end;
- CircbufAlloc := NewCircularBuffer;
+ { TODO: Validate circbuf size, <64K }
+ NewCircularBuffer :=
+ GlobalSharedLockedAlloc(Sizeof(TCircularBuffer), hMem);
+ if NewCircularBuffer <> nil then
+ begin
+ NewCircularBuffer^.RecordHandle := hMem;
+ NewMidiBuffer :=
+ GlobalSharedLockedAlloc(Capacity * Sizeof(TMidiBufferItem), hMem);
+ if NewMidiBuffer = nil then
+ begin
+ { TODO: Exception here? }
+ GlobalSharedLockedFree(NewCircularBuffer^.RecordHandle,
+ NewCircularBuffer);
+ NewCircularBuffer := nil;
+ end
+ else
+ begin
+ NewCircularBuffer^.pStart := NewMidiBuffer;
+ { Point to item at end of buffer }
+ NewCircularBuffer^.pEnd := NewMidiBuffer;
+ inc(NewCircularBuffer^.pEnd, Capacity);
+ { Start off the get and put pointers in the same position. These
+ will get out of sync as the interrupts start rolling in }
+ NewCircularBuffer^.pNextPut := NewMidiBuffer;
+ NewCircularBuffer^.pNextGet := NewMidiBuffer;
+ NewCircularBuffer^.Error := 0;
+ NewCircularBuffer^.Capacity := Capacity;
+ NewCircularBuffer^.EventCount := 0;
+ end;
+ end;
+ CircbufAlloc := NewCircularBuffer;
end;
-procedure CircbufFree( pBuffer: PCircularBuffer );
+procedure CircbufFree(pBuffer: PCircularBuffer);
begin
- if (pBuffer <> Nil) then
- begin
- GlobalSharedLockedFree(pBuffer^.BufferHandle, pBuffer^.pStart);
- GlobalSharedLockedFree(pBuffer^.RecordHandle, pBuffer);
- end;
+ if pBuffer <> nil then
+ begin
+ GlobalSharedLockedFree(pBuffer^.BufferHandle, pBuffer^.pStart);
+ GlobalSharedLockedFree(pBuffer^.RecordHandle, pBuffer);
+ end;
end;
{ Reads first event in queue without removing it.
Returns true if successful, False if no events in queue }
-function CircbufReadEvent( PBuffer: PCircularBuffer; PEvent: PMidiBufferItem ): Boolean;
+function CircbufReadEvent(PBuffer: PCircularBuffer; PEvent: PMidiBufferItem): boolean;
var
- PCurrentEvent: PMidiBufferItem;
+ PCurrentEvent: PMidiBufferItem;
begin
- if (PBuffer^.EventCount <= 0) then
- CircbufReadEvent := False
- else
- begin
- PCurrentEvent := PBuffer^.PNextget;
-
- { Copy the object from the "tail" of the buffer to the caller's object }
- PEvent^.Timestamp := PCurrentEvent^.Timestamp;
- PEvent^.Data := PCurrentEvent^.Data;
- PEvent^.Sysex := PCurrentEvent^.Sysex;
- CircbufReadEvent := True;
- end;
+ if PBuffer^.EventCount <= 0 then
+ CircbufReadEvent := false
+ else
+ begin
+ PCurrentEvent := PBuffer^.PNextget;
+
+ { Copy the object from the "tail" of the buffer to the caller's object }
+ PEvent^.Timestamp := PCurrentEvent^.Timestamp;
+ PEvent^.Data := PCurrentEvent^.Data;
+ PEvent^.Sysex := PCurrentEvent^.Sysex;
+ CircbufReadEvent := true;
+ end;
end;
{ Remove current event from the queue }
-function CircbufRemoveEvent(PBuffer: PCircularBuffer): Boolean;
+function CircbufRemoveEvent(PBuffer: PCircularBuffer): boolean;
begin
- if (PBuffer^.EventCount > 0) then
- begin
- Dec( Pbuffer^.EventCount);
-
- { Advance the buffer pointer, with wrap }
- Inc( Pbuffer^.PNextGet );
- If (PBuffer^.PNextGet = PBuffer^.PEnd) then
- PBuffer^.PNextGet := PBuffer^.PStart;
-
- CircbufRemoveEvent := True;
- end
- else
- CircbufRemoveEvent := False;
+ if PBuffer^.EventCount > 0 then
+ begin
+ dec(Pbuffer^.EventCount);
+
+ { Advance the buffer pointer, with wrap }
+ inc(Pbuffer^.PNextGet);
+ if PBuffer^.PNextGet = PBuffer^.PEnd then
+ PBuffer^.PNextGet := PBuffer^.PStart;
+
+ CircbufRemoveEvent := true;
+ end
+ else
+ CircbufRemoveEvent := false;
end;
end.