1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_MidiStreamParser.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 14068 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_SND_MIDI_STREAM_PARSER_H_
17 #define NW_SND_MIDI_STREAM_PARSER_H_
18 
19 namespace nw {
20 namespace snd {
21 namespace internal {
22 
23 /* ========================================================================
24         class definition
25    ======================================================================== */
26 
27 class MidiStreamParser
28 {
29   public:
30     typedef void (*MidiCallback)( u8 status, u8 data1, u8 data2, void* arg );
31 
32     MidiStreamParser();
33     void SetCallback( MidiCallback callback, void* arg );
34     void Parse( const void* buffer, unsigned long size );
35     void Reset();
36 
37   private:
38 	struct MidiMsgBuffer
39     {
40         const u8* ptr;
41         u32 len;
42         u32 readPos;
43         u32 donePos;
44     };
45 
46     bool ReadByte( u8* data );
47     bool PeakByte( u8* data );
48     void BackByte( u8 byte );
49     void EatByte();
50     void SetMsgBuffer( const void* buffer, u32 len );
51     void ParseBuffer();
52     void RestBuffer();
53     bool SeekStatusByte();
54 
IsDataByte(u8 b)55     static bool IsDataByte( u8 b ) { return ( b & 0x80 ) ? false : true; }
IsStatusByte(u8 b)56     static bool IsStatusByte( u8 b ) { return ( b & 0x80 ) ? true : false; }
IsRealtimeMesg(u8 b)57     static bool IsRealtimeMesg( u8 b ) { return ( b >= 0xf8 ) ? true : false; }
IsSystemResetMesg(u8 b)58     static bool IsSystemResetMesg( u8 b ) { return ( b == 0xff ) ? true : false; }
59 
60     MidiCallback m_CallbackFunc;
61     void* m_pCallbackArg;
62 
63     MidiMsgBuffer m_MsgBuffer;
64     MidiMsgBuffer m_RestBuffer;
65 
66     u8 m_RestBuf[256];
67     bool m_IsReset;
68     bool m_IsSysEx;
69     u8 m_RunningStatus;
70     u8 m_BackByte;
71     bool m_IsBackByteAvailable;
72 };
73 
74 } // namespace nw::snd::internal
75 } // namespace nw::snd
76 } // namespace nw
77 
78 
79 #endif /* NW_SND_MIDI_STREAM_PARSER_H_ */
80 
81