head	1.3;
access;
symbols;
locks
	baron:1.3; strict;
comment	@// @;


1.3
date	99.03.30.17.12.37;	author baron;	state Exp;
branches;
next	1.2;

1.2
date	99.03.18.15.06.17;	author baron;	state Exp;
branches;
next	1.1;

1.1
date	99.03.16.12.21.34;	author baron;	state Exp;
branches;
next	;


desc
@Implementation of classes to hold/edit midi data
@


1.3
log
@now print time & length in a more human form
@
text
@#include "MidiData.h"
#include "midi_parse.h"	//kind declarations
/*
 * $Id: MidiData.cpp,v 1.2 1999/03/18 15:06:17 baron Exp baron $
 *
 * Copyright 1999 Kelvin W Sherlock
 */

MidiData::MidiData(int event_kind):
	BListItem(),
	time(-1),
	channel(-1),
	value(-1),
	kind(event_kind)
{
}

MidiData::~MidiData()
{
}

float MidiData::GetTime(void) const
{
	return time;
}
int MidiData::GetChannel(void) const
{
	return channel;
}
long MidiData::GetValue(void) const
{
	return value;
}
void MidiData::SetTime(float t)
{
	time = t;
}
void MidiData::SetChannel(int ch)
{
	channel = ch;
}
void MidiData::SetValue(long v)
{
	value = v; 
}
void MidiData::SetKind(int event_kind)
{
	kind = event_kind;
}
int MidiData::GetKind(void) const
{
	return kind;
}
bool MidiData::Edit(void)
{
	return 0;
}

void MidiData::DrawItem(BView *owner, BRect frame, bool complete)
{
BString S("");
char buffer[64];

	if (complete || IsSelected())
	{
		if (IsSelected())
			owner->SetHighColor(216,216,216);
		else
			owner->SetHighColor(owner->ViewColor());
		
		owner->FillRect(frame);
	}

	owner->SetHighColor(0, 0, 0);		//black
	
		//Draw the time
	if (time != -1)
	{
		owner->MovePenTo(frame.left + 4, frame.bottom - 2);
		/*
		 * time is a float == # of quarter notes since start of the song
		 *
		 */
		//TODO - check the time signature ...
		int bar = (int)(time / 4) + 1;	//assume 4/4
		int qn = ((int)(time) % 4) + 1;
		int tmp = (int)(time * 1000) % 1000; 
		
		sprintf(buffer, "%4d:%02d.%03d%c", bar, qn, tmp, 0);
		owner->DrawString(buffer);
		
	}
	if (channel != -1)
	{
		//Draw the channel
		S = "";
	
		owner->MovePenTo(frame.left + 74, frame.bottom - 2);
		S << (uint32) (channel+1);
		owner->DrawString(S.String());
	}
	
}

Note::Note():
	MidiData(NOTE_ON),
	OnVelocity(-1),
	OffVelocity(-1),
	note(-1),
	length(-1)
{
}

Note::~Note()
{
}

void Note::SetOnVelocity(int v)
{
	OnVelocity = v;
}
void Note::SetOffVelocity(int v)
{
	OffVelocity = v;
}
void Note::SetNote(int n)
{
	note = n;
}
void Note::SetNoteLength(float len)
{
	length = len;
}

int  Note::GetOnVelocity(void) const
{
	return OnVelocity;
}
int  Note::GetOffVelocity(void) const
{
	return OffVelocity;
}
int  Note::GetNote(void) const
{
	return note;
}
float  Note::GetNoteLength(void) const
{
	return length;
}

/*
 * This is called to draw the item in a BList
 *
 */
//TODO - implement
void Note::DrawItem(BView *owner, BRect frame, bool complete)
{
BString S("");

static char *notes[] =
{
	"C",
	"C#",
	"D",
	"D#",
	"E",
	"F",
	"F#",
	"G",
	"G#",
	"A",
	"A#",
	"B"
};

		MidiData::DrawItem(owner, frame, complete);


	//type of event
	owner->MovePenTo(frame.left + 104, frame.bottom - 2);
	owner->DrawString("Note On");

	//Draw The note
	S = "";
	
	owner->MovePenTo(frame.left + 204, frame.bottom - 2);
	S += notes[GetNote() % 12];
	S += " ";
	S <<(uint32)(GetNote() / 12);
	owner->DrawString(S.String());

	//Draw the Velocity
	S = "";
	owner->MovePenTo(frame.left + 254, frame.bottom - 2);
	S << (uint32) OnVelocity;
	owner->DrawString(S.String());

	//Draw the length
	if (length != -1)	//sanity check
	{
		S = "";
		S << length;
		owner->MovePenTo(frame.left + 304, frame.bottom - 2);
		owner->DrawString(S.String());
	}

}
//TODO - implement
bool Note::Edit(void)
{
	return false;
}

Text::Text():
	MidiData(TEXT_EVENT),
	type(-1),
	txt("")
{
}
Text::~Text()
{
}


void Text::SetText(const BString& b)
{
	txt = b;
}

const char *Text::GetText(void) const
{
	return txt.String();
}

void Text::SetType(int t)
{
	type = t;
}

int Text::GetType(void) const
{
	return type;
}

void Text::DrawItem(BView *owner, BRect frame, bool complete)
{
static char *tt[] =
{
	NULL,
	"Text",	// #1
	"Copyright",
	"Sequence Name",
	"Instrument Name",
	"Lyric",
	"Marker",
	"Cue Point"
};
BString S;



	MidiData::DrawItem(owner, frame, complete);	//do the hilighting thing
	owner->SetHighColor(255,0,0);	//red

	//draw the type of text
	if (type > 0 && type < 8)
	{
		owner->MovePenTo(frame.left + 104, frame.bottom - 2);
		S = tt[type];
		owner->DrawString(S.String());
	}
	
	//draw the first 40 chars (or so)
	if (txt.Length())
	{
		S.SetTo(txt, MIN(txt.Length(), 40));
		owner->MovePenTo(frame.left + 204, frame.bottom - 2);
		owner->DrawString(S.String());
	}

}

bool Text::Edit(void)
{
return false;
}

Tempo::Tempo():
	MidiData(SET_TEMPO)
{
}
Tempo::~Tempo()
{
}

void Tempo::DrawItem(BView *owner, BRect frame, bool complete)
{
char buffer[64];
float f;
int t = GetValue();

	MidiData::DrawItem(owner, frame, complete);
	
	owner->MovePenTo(frame.left + 104, frame.bottom - 2);
	owner->DrawString("Tempo");
	
	// draw the tempo
	f = t ? (float)60000000.0/t : 0;

	sprintf(buffer, "%2.2f%c", f, 0);

	owner->MovePenTo(frame.left + 204, frame.bottom - 2);
	owner->DrawString(buffer);

}

ProgramChange::ProgramChange():
	MidiData(PROGRAM_CHANGE)
{}
ProgramChange::~ProgramChange()
{}

extern char *Instruments[];
void ProgramChange::DrawItem(BView *owner, BRect frame, bool complete)
{
	MidiData::DrawItem(owner, frame, complete);
	
	owner->SetHighColor(0,0,255);	//blue
	owner->MovePenTo(frame.left + 104, frame.bottom -2 );
	owner->DrawString("Program Change");
	
	if (GetValue() != -1 && GetValue() < 128 && GetChannel() != 9)
	{
		owner->MovePenTo(frame.left + 204, frame.bottom - 2);
	
		owner->DrawString(Instruments[GetValue()]);
	}
}


PitchBend::PitchBend():
	MidiData(PITCH_BEND)
{}
PitchBend::~PitchBend()
{}
void PitchBend::DrawItem(BView *owner, BRect frame, bool complete)
{
char buffer[64];

	MidiData::DrawItem(owner, frame, complete);
	
	owner->SetHighColor(0, 100, 0);	//ranger green
	owner->MovePenTo(frame.left + 104, frame.bottom - 2);
	owner->DrawString("Pitch Bend");
	
	sprintf(buffer, "%d%c", GetValue(), 0); 
	owner->MovePenTo(frame.left + 204, frame.bottom - 2);
	owner->DrawString(buffer);

}



KeySignature::KeySignature():
	MidiData(KEY_SIGNATURE),
	minor(false),			// C Major
	key(0)
{}
KeySignature::~KeySignature()
{}

bool KeySignature::GetMinor(void) const
{
	return minor;
}
int KeySignature::GetKey(void) const
{
	return key;
}
void KeySignature::SetKey(int k)
{
	key = k;
}
void KeySignature::SetMinor(bool b)
{
	minor = b;
}

void KeySignature::DrawItem(BView *owner, BRect frame, bool complete)
{

BString S("");

static char *kSharp[] =
{
	"C  Am ",
	"G  Em ",
	"D  Bm ",
	"A  F#m",
	"E  C#m",
	"B  G#m",
	"F# D#m",
	"C# A#m",
	"G# Fm ",
	"D# Cm ",		//TODO - are these right???
	"A# Gm ",
	"F  Dm "
};

static char *kFlat[] =
{
	"C  Am ",
	"F  Dm ",
	"Bb Gm ",
	"Eb Cm ",
	"Ab Fm ",
	"Db Bbm",
	"Gb Ebm",
	"B  Abm",		//TODO - see above
	"E  Dbm",
	"A  Gbm",
	"D  Bm ",
	"G  Em "
};

	MidiData::DrawItem(owner, frame, complete);
	
	owner->MovePenTo(frame.left +  104, frame.bottom - 2);
	owner->DrawString("Key");
	
	S = "";
	
	if (key >= 0 && key < 12)
	{
		char *cp = kSharp[key];
		if (minor) cp += 3;
		S.SetTo(cp, 3);
	}
	else
	{
		register int k = 256 - key;
		if (k >= 0 && k < 12)
		{
		char *cp = kFlat[k];
			if (minor) cp += 3;
			S.SetTo(cp, 3);
		}
	}
	
	owner->MovePenTo(frame.left + 204, frame.bottom - 2);
	owner->DrawString(S.String());
}

ControlChange::ControlChange():
	MidiData(CONTROL_CHANGE),
	controller(-1)
{}

ControlChange::~ControlChange()
{}

void ControlChange::SetController(int c)
{
	controller = c;
}

int ControlChange::GetController(void) const
{
	return controller;
}

void ControlChange::DrawItem(BView *owner, BRect frame, bool complete)
{
static char *names[] =
{
	"Bank Select",
	"Modulation Controller",
	"Breath Controller",
	NULL,
	"Foot Controller",
	"Portamento Time",
	"Data Entry",
	"Volume",
	"Balance Controller",
	NULL,
	"PanPot",
	"Expression"
};

BString S("");


	MidiData::DrawItem(owner, frame, complete);
	
	owner->SetHighColor(255,0,255);
	owner->MovePenTo(frame.left +  104, frame.bottom - 2);
	owner->DrawString("Control Change");
	
	if (controller != -1)
	{			
		if (controller < 12 && names[controller])
			S = names[controller]; 
		else
		{
			S = "Controller #";
			S << (uint32) controller;
		}
		owner->MovePenTo(frame.left + 204, frame.bottom - 2);
		owner->DrawString(S.String());
		
		if (GetValue() != -1)
		{
			S = "";
			S << (uint32) GetValue();
			owner->MovePenTo(frame.left + 304, frame.bottom - 2);
			owner->DrawString(S.String());
		}
	}
}

TimeSig::TimeSig():
	MidiData(TIME_SIGNATURE),
	num(-1),
	den(-1),
	clock(-1),
	click(-1)
{}

TimeSig::~TimeSig()
{}

void TimeSig::SetNumerator(int n)
{
	num = n;
}

void TimeSig::SetDenominator(int d)
{
	den = d;
}

void TimeSig::SetClock(int c)
{
	clock = c;
}

void TimeSig::SetClick(int c)
{
	click = c;
}

int TimeSig::GetNumerator(void) const
{
	return num;
}
int TimeSig::GetDenominator(void) const
{
	return den;
}
int TimeSig::GetClick(void) const
{
	return click;
}
int TimeSig::GetClock(void) const
{
	return clock;
}

void TimeSig::DrawItem(BView *owner, BRect frame, bool complete)
{
BString S("");

	MidiData::DrawItem(owner, frame, complete);
	
	owner->MovePenTo(frame.left + 104, frame.bottom - 2);
	owner->DrawString("Time Signature");
	
	if (num != -1 && den != -1)
	{
		S <<(uint32) num;
		S += "/";
		S <<(uint32) den;
		owner->MovePenTo(frame.left + 204, frame.bottom - 2);
		owner->DrawString(S.String());
	}
}
@


1.2
log
@Added more data classes
@
text
@d4 1
a4 1
 * $Id: MidiData.cpp,v 1.1 1999/03/16 12:21:34 baron Exp baron $
d13 1
d22 1
a22 1
long MidiData::GetTime(void) const
d34 1
a34 1
void MidiData::SetTime(long t)
d59 46
d130 1
a130 1
void Note::SetNoteLength(long len)
d147 1
a147 1
int  Note::GetNoteLength(void) const
d159 1
d161 46
a211 6
#if 0
	Show();		// show the window

	read_port()

#endif
d248 34
d290 1
a290 2
	MidiData(SET_TEMPO),
	tempo(-1)
d296 90
a385 1
void Tempo::SetTempo(long t)
d387 1
a387 1
	tempo = t;
d389 7
a395 1
long Tempo::GetTempo(void) const
d397 123
a519 1
	return tempo;
d522 12
a533 3
ProgramChange::ProgramChange():
	MidiData(PROGRAM_CHANGE),
	instrument(-1)
d535 1
d537 2
a538 1
ProgramChange::~ProgramChange()
d540 1
d542 2
a543 1
void ProgramChange::SetInstrument(int i)
d545 1
a545 1
	instrument = i;
d547 2
a548 1
int ProgramChange::GetInstrument(void) const
d550 1
a550 1
	return instrument;
d553 13
a565 2
PitchBend::PitchBend():
	MidiData(PITCH_BEND)
d567 1
d569 2
a570 1
PitchBend::~PitchBend()
d572 16
a587 1
}@


1.1
log
@Initial revision
@
text
@d1 207
a207 94
#include "MidiData.h"

/*
 * $Id$
 *
 * Copyright 1999 Kelvin W Sherlock
 */

MidiData::MidiData():
	time(-1),
	channel(-1)
{
}

MidiData::~MidiData()
{
}

long MidiData::GetTime(void) const
{
	return time;
}

int MidiData::GetChannel(void) const
{
	return channel;
}

void MidiData::SetTime(long t)
{
	time = t;
}

void MidiData::SetChannel(int ch)
{
	channel = ch;
}

int MidiData::Edit(void)
{
	return 0;
}

Note::Note():
	MidiData(),
	OnVelocity(-1),
	OffVelocity(-1),
	note(-1),
	length(-1)
{
}

Note::~Note()
{
}

void Note::SetOnVelocity(int v)
{
	OnVelocity = v;
}
void Note::SetOffVelocity(int v)
{
	OffVelocity = v;
}
void Note::SetNote(int n)
{
	note = n;
}
void Note::SetNoteLength(long l)
{
	length = l;
}

int  Note::GetOnVelocity(void) const
{
	return OnVelocity;
}
int  Note::GetOffVelocity(void) const
{
	return OffVelocity;
}
int  Note::GetNote(void) const
{
	return note;
}
int  Note::GetNoteLength(void) const
{
	return length;
}

int Note::Edit(void)
{
	return 0;
}
@
