Jelajahi Sumber

implemented NoteOnMessage::toOffMessage()

Fabian Peter Hammerle 10 tahun lalu
induk
melakukan
2acee77f4d
2 mengubah file dengan 16 tambahan dan 6 penghapusan
  1. 6 1
      Message.cpp
  2. 10 5
      Message.h

+ 6 - 1
Message.cpp

@@ -26,7 +26,7 @@ Message* Message::parse(std::vector<unsigned char> &messageBytes)
 			case NoteOffMessage::messageType:
 			{
 				NoteMessage::Pitch pitch = messageBytes[1];
-				unsigned char velocity = messageBytes[2];
+                NoteMessage::Velocity velocity = messageBytes[2];
 				if(messageType == NoteOffMessage::messageType || velocity == 0)
 				{
 					return new NoteOffMessage(channel, pitch, velocity);
@@ -116,6 +116,11 @@ void NoteOnMessage::print(std::ostream& stream) const
 			<< "note " << (unsigned short)pitch << " with velocity " << (unsigned short)velocity << " on\n";
 }
 
+NoteOffMessage NoteOnMessage::toOffMessage(Velocity v) const
+{
+    return NoteOffMessage(channel, pitch, v);
+}
+
 void NoteOffMessage::print(std::ostream& stream) const
 {
 	stream	<< "channel #" << (unsigned short)(channel + 1) << ": "

+ 10 - 5
Message.h

@@ -1,6 +1,7 @@
 #pragma once
 #include <stdint.h>
 #include <vector>
+#include <ostream>
 
 namespace midi {
 
@@ -39,18 +40,21 @@ class NoteMessage : public ChannelMessage
 {
 public:
     typedef unsigned char Pitch;
+    typedef unsigned char Velocity;
 
 	Pitch pitch;
 	static const Pitch pitchMaximum = (1 << 7) - 1;
-	unsigned char velocity;
-	static const unsigned char velocityMaximum = (2 << 7) - 1;
+	Velocity velocity;
+	static const Velocity velocityMaximum = (2 << 7) - 1;
 
-	NoteMessage(Channel channel, Pitch pitch, unsigned char velocity)
+	NoteMessage(Channel channel, Pitch pitch, Velocity velocity)
 		: ChannelMessage(channel), pitch(pitch), velocity(velocity)
 	{
 	}
 };
 
+class NoteOffMessage;
+
 class NoteOnMessage : public NoteMessage
 {
 	friend class Message;
@@ -59,13 +63,14 @@ protected:
 	static const unsigned char messageType = 0x9;
 
 public:
-	NoteOnMessage(Channel channel, Pitch pitch, unsigned char velocity)
+	NoteOnMessage(Channel channel, Pitch pitch, Velocity velocity)
 		: NoteMessage(channel, pitch, velocity)
 	{
 	}
 
 	virtual void print(std::ostream& stream) const;
 	virtual std::vector<unsigned char> getBytes() const;
+    NoteOffMessage toOffMessage(Velocity velocity = 0) const;
 };
 
 class NoteOffMessage : public NoteMessage
@@ -76,7 +81,7 @@ protected:
 	static const unsigned char messageType = 0x8;
 
 public:
-	NoteOffMessage(Channel channel, Pitch pitch, unsigned char velocity)
+	NoteOffMessage(Channel channel, Pitch pitch, Velocity velocity)
 		: NoteMessage(channel, pitch, velocity)
 	{
 	}