Files
haiku-beta6/src/kits/midi/MidiStore.cpp
T

986 lines
17 KiB
C++
Raw Normal View History

2003-03-17 22:30:19 +00:00
/*
2006-06-19 13:24:04 +00:00
* Copyright 2002-2006, Haiku.
* Distributed under the terms of the MIT License.
2003-03-17 22:30:19 +00:00
*
2006-06-19 13:24:04 +00:00
* Authors:
*
* Matthijs Hollemans
* Jérôme Leveque
* Paul Stadler
2003-03-17 22:30:19 +00:00
*/
2002-11-21 19:23:22 +00:00
#include <File.h>
#include <List.h>
2006-06-19 13:24:04 +00:00
#include <MidiDefs.h>
#include <MidiStore.h>
2003-03-17 22:30:19 +00:00
#include <stdlib.h>
#include <string.h>
2003-03-17 22:30:19 +00:00
#include "debug.h"
2002-07-09 12:24:59 +00:00
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
struct BMidiEvent {
2003-03-17 22:30:19 +00:00
BMidiEvent()
{
byte1 = 0;
byte2 = 0;
byte3 = 0;
data = NULL;
length = 0;
}
~BMidiEvent()
{
free(data);
}
uint32 time; // either ticks or milliseconds
bool ticks; // event is from MIDI file
uchar byte1;
uchar byte2;
uchar byte3;
void* data; // sysex data
size_t length; // sysex data size
int32 tempo; // beats per minute
};
2005-07-07 12:42:57 +00:00
static int
compare_events(const void* event1, const void* event2)
2003-03-17 22:30:19 +00:00
{
BMidiEvent* e1 = *((BMidiEvent**) event1);
BMidiEvent* e2 = *((BMidiEvent**) event2);
return (e1->time - e2->time);
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
// #pragma mark -
BMidiStore::BMidiStore()
{
2006-06-19 13:24:04 +00:00
fEvents = new BList;
fCurrentEvent = 0;
fStartTime = 0;
fNeedsSorting = false;
fBeatsPerMinute = 60;
fTicksPerBeat = 240;
fFile = NULL;
fHookFunc = NULL;
fLooping = false;
fPaused = false;
fFinished = false;
fInstruments = new bool[128];
2002-07-09 12:24:59 +00:00
}
BMidiStore::~BMidiStore()
{
2006-06-19 13:24:04 +00:00
for (int32 t = 0; t < fEvents->CountItems(); ++t) {
2003-03-17 22:30:19 +00:00
delete EventAt(t);
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
2006-06-19 13:24:04 +00:00
delete fEvents;
delete[] fInstruments;
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::NoteOff(uchar channel, uchar note, uchar velocity,
uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_NOTE_OFF | (channel - 1);
event->byte2 = note;
event->byte3 = velocity;
AddEvent(event);
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::NoteOn(uchar channel, uchar note,
uchar velocity, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_NOTE_ON | (channel - 1);
event->byte2 = note;
event->byte3 = velocity;
AddEvent(event);
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::KeyPressure(uchar channel, uchar note,
uchar pressure, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_KEY_PRESSURE | (channel - 1);
event->byte2 = note;
event->byte3 = pressure;
AddEvent(event);
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::ControlChange(uchar channel, uchar controlNumber,
uchar controlValue, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_CONTROL_CHANGE | (channel - 1);
event->byte2 = controlNumber;
event->byte3 = controlValue;
AddEvent(event);
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::ProgramChange(uchar channel, uchar programNumber,
uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_PROGRAM_CHANGE | (channel - 1);
event->byte2 = programNumber;
AddEvent(event);
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::ChannelPressure(uchar channel, uchar pressure, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_CHANNEL_PRESSURE | (channel - 1);
event->byte2 = pressure;
AddEvent(event);
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::PitchBend(uchar channel, uchar lsb, uchar msb, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_PITCH_BEND | (channel - 1);
event->byte2 = lsb;
event->byte3 = msb;
AddEvent(event);
2002-07-09 12:24:59 +00:00
}
2003-03-17 22:30:19 +00:00
2005-07-07 12:42:57 +00:00
void
BMidiStore::SystemExclusive(void* data, size_t length, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = B_SYS_EX_START;
event->data = malloc(length);
event->length = length;
memcpy(event->data, data, length);
AddEvent(event);
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::SystemCommon(uchar status, uchar data1,
uchar data2, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = status;
event->byte2 = data1;
event->byte3 = data2;
AddEvent(event);
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::SystemRealTime(uchar status, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = status;
AddEvent(event);
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::TempoChange(int32 beatsPerMinute, uint32 time)
{
2003-03-17 22:30:19 +00:00
BMidiEvent* event = new BMidiEvent;
event->time = time;
event->ticks = false;
event->byte1 = 0xFF;
event->byte2 = 0x51;
event->byte3 = 0x03;
event->tempo = beatsPerMinute;
AddEvent(event);
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
status_t
BMidiStore::Import(const entry_ref* ref)
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
memset(fInstruments, 0, 128 * sizeof(bool));
2005-07-07 12:42:57 +00:00
try {
2006-06-19 13:24:04 +00:00
fFile = new BFile(ref, B_READ_ONLY);
if (fFile->InitCheck() != B_OK)
throw fFile->InitCheck();
2003-03-17 22:30:19 +00:00
char fourcc[4];
ReadFourCC(fourcc);
if (strncmp(fourcc, "MThd", 4) != 0)
throw (status_t) B_BAD_MIDI_DATA;
if (Read32Bit() != 6)
throw (status_t) B_BAD_MIDI_DATA;
2002-11-21 19:23:22 +00:00
2006-06-19 13:24:04 +00:00
fFormat = Read16Bit();
fNumTracks = Read16Bit();
fTicksPerBeat = Read16Bit();
2006-06-19 13:24:04 +00:00
if (fTicksPerBeat & 0x8000) {
2005-07-07 12:42:57 +00:00
// we don't support SMPTE time codes,
// only ticks per quarter note
2006-06-19 13:24:04 +00:00
fTicksPerBeat = 240;
2003-03-17 22:30:19 +00:00
}
2006-06-19 13:24:04 +00:00
fCurrTrack = 0;
while (fCurrTrack < fNumTracks) {
2003-03-17 22:30:19 +00:00
ReadChunk();
}
2005-07-07 12:42:57 +00:00
} catch (status_t e) {
2006-06-19 13:24:04 +00:00
delete fFile;
fFile = NULL;
2003-03-17 22:30:19 +00:00
return e;
}
2002-11-21 19:23:22 +00:00
SortEvents(true);
2002-07-09 12:24:59 +00:00
2006-06-19 13:24:04 +00:00
delete fFile;
fFile = NULL;
2003-03-17 22:30:19 +00:00
return B_OK;
}
2005-07-07 12:42:57 +00:00
status_t
BMidiStore::Export(const entry_ref* ref, int32 format)
2003-03-17 22:30:19 +00:00
{
2005-07-07 12:42:57 +00:00
try {
2006-06-19 13:24:04 +00:00
fFile = new BFile(ref, B_READ_WRITE);
if (fFile->InitCheck() != B_OK)
throw fFile->InitCheck();
2003-03-17 22:30:19 +00:00
SortEvents(true);
WriteFourCC('M', 'T', 'h', 'd');
Write32Bit(6);
Write16Bit(0); // we do only format 0
Write16Bit(1);
2006-06-19 13:24:04 +00:00
Write16Bit(fTicksPerBeat);
2003-03-17 22:30:19 +00:00
WriteTrack();
2005-07-07 12:42:57 +00:00
} catch (status_t e) {
2006-06-19 13:24:04 +00:00
delete fFile;
fFile = NULL;
2003-03-17 22:30:19 +00:00
return e;
2002-11-21 19:23:22 +00:00
}
2006-06-19 13:24:04 +00:00
delete fFile;
fFile = NULL;
2003-03-17 22:30:19 +00:00
return B_OK;
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::SortEvents(bool force)
{
2006-06-19 13:24:04 +00:00
if (force || fNeedsSorting) {
fEvents->SortItems(compare_events);
fNeedsSorting = false;
2003-03-17 22:30:19 +00:00
}
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::CountEvents() const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
return fEvents->CountItems();
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::CurrentEvent() const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
return fCurrentEvent;
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::SetCurrentEvent(uint32 eventNumber)
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
fCurrentEvent = eventNumber;
2002-07-09 12:24:59 +00:00
}
2003-03-17 22:30:19 +00:00
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::DeltaOfEvent(uint32 eventNumber) const
2003-03-17 22:30:19 +00:00
{
// Even though the BeBook says that the delta is the time span between
// an event and the first event in the list, this doesn't appear to be
// true for events that were captured from other BMidi objects such as
// BMidiPort. For those events, we return the absolute timestamp. The
// BeBook is correct for events from MIDI files, though.
2003-03-17 22:30:19 +00:00
BMidiEvent* event = EventAt(eventNumber);
if (event != NULL)
return GetEventTime(event);
return 0;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::EventAtDelta(uint32 time) const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
for (int32 t = 0; t < fEvents->CountItems(); ++t) {
2005-07-07 12:42:57 +00:00
if (GetEventTime(EventAt(t)) >= time)
return t;
2003-03-17 22:30:19 +00:00
}
return 0;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::BeginTime() const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
return fStartTime;
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::SetTempo(int32 beatsPerMinute_)
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
fBeatsPerMinute = beatsPerMinute_;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
int32
BMidiStore::Tempo() const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
return fBeatsPerMinute;
2002-11-21 19:23:22 +00:00
}
2003-03-17 22:30:19 +00:00
void BMidiStore::_ReservedMidiStore1() { }
void BMidiStore::_ReservedMidiStore2() { }
void BMidiStore::_ReservedMidiStore3() { }
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
void
BMidiStore::Run()
{
2004-05-13 19:54:00 +00:00
// This rather compilicated Run() loop is not only used by BMidiStore
// but also by BMidiSynthFile. The "paused", "finished", and "looping"
// flags, and the "stop hook" are especially provided for the latter.
2006-06-19 13:24:04 +00:00
fPaused = false;
fFinished = false;
2005-07-07 12:42:57 +00:00
int32 timeAdjust = 0;
uint32 baseTime = 0;
2003-03-17 22:30:19 +00:00
bool firstEvent = true;
2004-05-13 19:54:00 +00:00
bool resetTime = false;
2003-03-17 22:30:19 +00:00
2005-07-07 12:42:57 +00:00
while (KeepRunning()) {
2006-06-19 13:24:04 +00:00
if (fPaused) {
2004-05-13 19:54:00 +00:00
resetTime = true;
snooze(100000);
continue;
}
2005-07-07 12:42:57 +00:00
2006-06-19 13:24:04 +00:00
BMidiEvent* event = EventAt(fCurrentEvent);
2005-07-07 12:42:57 +00:00
if (event == NULL) {
// no more events
2006-06-19 13:24:04 +00:00
if (fLooping) {
2004-05-13 19:54:00 +00:00
resetTime = true;
2006-06-19 13:24:04 +00:00
fCurrentEvent = 0;
2005-07-07 12:42:57 +00:00
} else
2004-05-13 19:54:00 +00:00
break;
}
2003-03-17 22:30:19 +00:00
2005-07-07 12:42:57 +00:00
if (firstEvent) {
2006-06-19 13:24:04 +00:00
fStartTime = B_NOW;
baseTime = fStartTime;
2005-07-07 12:42:57 +00:00
} else if (resetTime)
2004-05-13 19:54:00 +00:00
baseTime = B_NOW;
2005-07-07 12:42:57 +00:00
if (firstEvent || resetTime) {
2004-05-13 19:54:00 +00:00
timeAdjust = baseTime - GetEventTime(event);
SprayEvent(event, baseTime);
2003-03-17 22:30:19 +00:00
firstEvent = false;
2004-05-13 19:54:00 +00:00
resetTime = false;
2005-07-07 12:42:57 +00:00
} else
2003-03-17 22:30:19 +00:00
SprayEvent(event, GetEventTime(event) + timeAdjust);
2006-06-19 13:24:04 +00:00
++fCurrentEvent;
2002-11-21 19:23:22 +00:00
}
2004-05-13 19:54:00 +00:00
2006-06-19 13:24:04 +00:00
fFinished = true;
fPaused = false;
2004-05-13 19:54:00 +00:00
2006-06-19 13:24:04 +00:00
if (fHookFunc != NULL)
(*fHookFunc)(fHookArg);
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::AddEvent(BMidiEvent* event)
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
fEvents->AddItem(event);
fNeedsSorting = true;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::SprayEvent(const BMidiEvent* event, uint32 time)
2003-03-17 22:30:19 +00:00
{
uchar byte1 = event->byte1;
uchar byte2 = event->byte2;
uchar byte3 = event->byte3;
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
switch (byte1 & 0xF0) {
2003-03-17 22:30:19 +00:00
case B_NOTE_OFF:
SprayNoteOff((byte1 & 0x0F) + 1, byte2, byte3, time);
return;
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
case B_NOTE_ON:
SprayNoteOn((byte1 & 0x0F) + 1, byte2, byte3, time);
return;
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
case B_KEY_PRESSURE:
SprayKeyPressure((byte1 & 0x0F) + 1, byte2, byte3, time);
return;
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
case B_CONTROL_CHANGE:
SprayControlChange((byte1 & 0x0F) + 1, byte2, byte3, time);
return;
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
case B_PROGRAM_CHANGE:
SprayProgramChange((byte1 & 0x0F) + 1, byte2, time);
return;
case B_CHANNEL_PRESSURE:
SprayChannelPressure((byte1 & 0x0F) + 1, byte2, time);
return;
case B_PITCH_BEND:
SprayPitchBend((byte1 & 0x0F) + 1, byte2, byte3, time);
return;
case 0xF0:
2005-07-07 12:42:57 +00:00
switch (byte1) {
2003-03-17 22:30:19 +00:00
case B_SYS_EX_START:
SpraySystemExclusive(event->data, event->length, time);
return;
case B_MIDI_TIME_CODE:
case B_SONG_POSITION:
case B_SONG_SELECT:
case B_CABLE_MESSAGE:
case B_TUNE_REQUEST:
case B_SYS_EX_END:
SpraySystemCommon(byte1, byte2, byte3, time);
return;
case B_TIMING_CLOCK:
case B_START:
case B_CONTINUE:
case B_STOP:
case B_ACTIVE_SENSING:
SpraySystemRealTime(byte1, time);
return;
case B_SYSTEM_RESET:
2005-07-07 12:42:57 +00:00
if (byte2 == 0x51 && byte3 == 0x03) {
2003-03-17 22:30:19 +00:00
SprayTempoChange(event->tempo, time);
2006-06-19 13:24:04 +00:00
fBeatsPerMinute = event->tempo;
2005-07-07 12:42:57 +00:00
} else
2003-03-17 22:30:19 +00:00
SpraySystemRealTime(byte1, time);
return;
}
return;
}
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
BMidiEvent*
BMidiStore::EventAt(int32 index) const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
return (BMidiEvent*)fEvents->ItemAt(index);
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::GetEventTime(const BMidiEvent* event) const
2003-03-17 22:30:19 +00:00
{
if (event->ticks)
return TicksToMilliseconds(event->time);
2005-07-07 12:42:57 +00:00
return event->time;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::TicksToMilliseconds(uint32 ticks) const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
return ((uint64)ticks * 60000) / (fBeatsPerMinute * fTicksPerBeat);
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::MillisecondsToTicks(uint32 ms) const
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
return ((uint64)ms * fBeatsPerMinute * fTicksPerBeat) / 60000;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::ReadFourCC(char* fourcc)
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
if (fFile->Read(fourcc, 4) != 4)
2003-03-17 22:30:19 +00:00
throw (status_t) B_BAD_MIDI_DATA;
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::WriteFourCC(char a, char b, char c, char d)
2003-03-17 22:30:19 +00:00
{
char fourcc[4] = { a, b, c, d };
2005-07-07 12:42:57 +00:00
2006-06-19 13:24:04 +00:00
if (fFile->Write(fourcc, 4) != 4)
2003-03-17 22:30:19 +00:00
throw (status_t) B_ERROR;
2002-07-09 12:24:59 +00:00
}
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::Read32Bit()
2003-03-17 22:30:19 +00:00
{
uint8 buf[4];
2006-06-19 13:24:04 +00:00
if (fFile->Read(buf, 4) != 4)
2003-03-17 22:30:19 +00:00
throw (status_t) B_BAD_MIDI_DATA;
2002-07-09 12:24:59 +00:00
2005-07-07 12:42:57 +00:00
return (buf[0] << 24L) | (buf[1] << 16L) | (buf[2] << 8L) | buf[3];
2002-07-09 12:24:59 +00:00
}
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
void
BMidiStore::Write32Bit(uint32 val)
2003-03-17 22:30:19 +00:00
{
uint8 buf[4];
buf[0] = (val >> 24) & 0xFF;
buf[1] = (val >> 16) & 0xFF;
buf[2] = (val >> 8) & 0xFF;
buf[3] = val & 0xFF;
2002-07-09 12:24:59 +00:00
2006-06-19 13:24:04 +00:00
if (fFile->Write(buf, 4) != 4)
2003-03-17 22:30:19 +00:00
throw (status_t) B_ERROR;
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
uint16
BMidiStore::Read16Bit()
2003-03-17 22:30:19 +00:00
{
uint8 buf[2];
2006-06-19 13:24:04 +00:00
if (fFile->Read(buf, 2) != 2)
2003-03-17 22:30:19 +00:00
throw (status_t) B_BAD_MIDI_DATA;
2002-07-09 12:24:59 +00:00
2003-03-17 22:30:19 +00:00
return (buf[0] << 8) | buf[1];
2002-11-21 19:23:22 +00:00
}
2002-07-09 12:24:59 +00:00
2005-07-07 12:42:57 +00:00
void
BMidiStore::Write16Bit(uint16 val)
2003-03-17 22:30:19 +00:00
{
uint8 buf[2];
2005-07-07 12:42:57 +00:00
buf[0] = (val >> 8) & 0xFF;
buf[1] = val & 0xFF;
2006-06-19 13:24:04 +00:00
if (fFile->Write(buf, 2) != 2)
2003-03-17 22:30:19 +00:00
throw (status_t) B_ERROR;
2002-11-21 19:23:22 +00:00
}
2003-03-17 22:30:19 +00:00
2005-07-07 12:42:57 +00:00
uint8
BMidiStore::PeekByte()
2003-03-17 22:30:19 +00:00
{
uint8 buf;
2006-06-19 13:24:04 +00:00
if (fFile->Read(&buf, 1) != 1)
2003-03-17 22:30:19 +00:00
throw (status_t) B_BAD_MIDI_DATA;
2002-07-09 12:24:59 +00:00
2006-06-19 13:24:04 +00:00
if (fFile->Seek(-1, SEEK_CUR) < 0)
2003-03-17 22:30:19 +00:00
throw (status_t) B_ERROR;
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
return buf;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
uint8
BMidiStore::NextByte()
2003-03-17 22:30:19 +00:00
{
uint8 buf;
2006-06-19 13:24:04 +00:00
if (fFile->Read(&buf, 1) != 1)
2003-03-17 22:30:19 +00:00
throw (status_t) B_BAD_MIDI_DATA;
2002-07-09 12:24:59 +00:00
2006-06-19 13:24:04 +00:00
--fByteCount;
2003-03-17 22:30:19 +00:00
return buf;
2002-07-09 12:24:59 +00:00
}
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
void
BMidiStore::WriteByte(uint8 val)
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
if (fFile->Write(&val, 1) != 1)
2003-03-17 22:30:19 +00:00
throw (status_t) B_ERROR;
2006-06-19 13:24:04 +00:00
++fByteCount;
2002-07-09 12:24:59 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::SkipBytes(uint32 length)
2003-03-17 22:30:19 +00:00
{
2006-06-19 13:24:04 +00:00
if (fFile->Seek(length, SEEK_CUR) < 0) {
2003-03-17 22:30:19 +00:00
throw (status_t) B_BAD_MIDI_DATA;
}
2006-06-19 13:24:04 +00:00
fByteCount -= length;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
uint32
BMidiStore::ReadVarLength()
2003-03-17 22:30:19 +00:00
{
uint32 val;
uint8 byte;
2005-07-07 12:42:57 +00:00
if ((val = NextByte()) & 0x80) {
2003-03-17 22:30:19 +00:00
val &= 0x7F;
2005-07-07 12:42:57 +00:00
do {
2003-03-17 22:30:19 +00:00
val = (val << 7) + ((byte = NextByte()) & 0x7F);
2005-07-07 12:42:57 +00:00
} while (byte & 0x80);
}
2003-03-17 22:30:19 +00:00
return val;
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::WriteVarLength(uint32 val)
2003-03-17 22:30:19 +00:00
{
uint32 buffer = val & 0x7F;
2005-07-07 12:42:57 +00:00
while ((val >>= 7) != 0) {
2003-03-17 22:30:19 +00:00
buffer <<= 8;
buffer |= ((val & 0x7F) | 0x80);
}
2005-07-07 12:42:57 +00:00
while (true) {
2003-03-17 22:30:19 +00:00
WriteByte(buffer);
if (buffer & 0x80)
buffer >>= 8;
else
break;
}
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::ReadChunk()
2003-03-17 22:30:19 +00:00
{
char fourcc[4];
ReadFourCC(fourcc);
2006-06-19 13:24:04 +00:00
fByteCount = Read32Bit();
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
if (strncmp(fourcc, "MTrk", 4) == 0)
ReadTrack();
2005-07-07 12:42:57 +00:00
else {
2003-03-17 22:30:19 +00:00
TRACE(("Skipping '%c%c%c%c' chunk (%lu bytes)",
fourcc[0], fourcc[1], fourcc[2], fourcc[3], byteCount))
2006-06-19 13:24:04 +00:00
SkipBytes(fByteCount);
2002-11-21 19:23:22 +00:00
}
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::ReadTrack()
2003-03-17 22:30:19 +00:00
{
uint8 status = 0;
uint8 data1;
uint8 data2;
BMidiEvent* event;
2006-06-19 13:24:04 +00:00
fTotalTicks = 0;
2003-03-17 22:30:19 +00:00
2006-06-19 13:24:04 +00:00
while (fByteCount > 0) {
2003-03-17 22:30:19 +00:00
uint32 ticks = ReadVarLength();
2006-06-19 13:24:04 +00:00
fTotalTicks += ticks;
2003-03-17 22:30:19 +00:00
if (PeekByte() & 0x80)
status = NextByte();
2005-07-07 12:42:57 +00:00
switch (status & 0xF0) {
2003-03-17 22:30:19 +00:00
case B_NOTE_OFF:
case B_NOTE_ON:
case B_KEY_PRESSURE:
case B_CONTROL_CHANGE:
case B_PITCH_BEND:
data1 = NextByte();
data2 = NextByte();
event = new BMidiEvent;
2006-06-19 13:24:04 +00:00
event->time = fTotalTicks;
2003-03-17 22:30:19 +00:00
event->ticks = true;
event->byte1 = status;
event->byte2 = data1;
event->byte3 = data2;
AddEvent(event);
break;
case B_PROGRAM_CHANGE:
case B_CHANNEL_PRESSURE:
data1 = NextByte();
event = new BMidiEvent;
2006-06-19 13:24:04 +00:00
event->time = fTotalTicks;
2003-03-17 22:30:19 +00:00
event->ticks = true;
event->byte1 = status;
event->byte2 = data1;
AddEvent(event);
if ((status & 0xF0) == B_PROGRAM_CHANGE)
2006-06-19 13:24:04 +00:00
fInstruments[data1] = true;
2003-03-17 22:30:19 +00:00
break;
case 0xF0:
2005-07-07 12:42:57 +00:00
switch (status) {
2003-03-17 22:30:19 +00:00
case B_SYS_EX_START:
ReadSystemExclusive();
break;
case B_TUNE_REQUEST:
case B_SYS_EX_END:
case B_TIMING_CLOCK:
case B_START:
case B_CONTINUE:
case B_STOP:
case B_ACTIVE_SENSING:
event = new BMidiEvent;
2006-06-19 13:24:04 +00:00
event->time = fTotalTicks;
2003-03-17 22:30:19 +00:00
event->ticks = true;
event->byte1 = status;
AddEvent(event);
break;
case B_MIDI_TIME_CODE:
case B_SONG_SELECT:
case B_CABLE_MESSAGE:
data1 = NextByte();
event = new BMidiEvent;
2006-06-19 13:24:04 +00:00
event->time = fTotalTicks;
2003-03-17 22:30:19 +00:00
event->ticks = true;
event->byte1 = status;
event->byte2 = data1;
AddEvent(event);
break;
case B_SONG_POSITION:
data1 = NextByte();
data2 = NextByte();
event = new BMidiEvent;
2006-06-19 13:24:04 +00:00
event->time = fTotalTicks;
2003-03-17 22:30:19 +00:00
event->ticks = true;
event->byte1 = status;
event->byte2 = data1;
event->byte3 = data2;
AddEvent(event);
break;
case B_SYSTEM_RESET:
ReadMetaEvent();
break;
}
break;
}
event = NULL;
2002-11-21 19:23:22 +00:00
}
2003-03-17 22:30:19 +00:00
2006-06-19 13:24:04 +00:00
++fCurrTrack;
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::ReadSystemExclusive()
2003-03-17 22:30:19 +00:00
{
// We do not import sysex's from MIDI files.
SkipBytes(ReadVarLength());
}
2003-03-17 22:30:19 +00:00
2005-07-07 12:42:57 +00:00
void
BMidiStore::ReadMetaEvent()
2003-03-17 22:30:19 +00:00
{
// We only import the Tempo Change meta event.
uint8 type = NextByte();
uint32 length = ReadVarLength();
2005-07-07 12:42:57 +00:00
if (type == 0x51 && length == 3) {
2003-03-17 22:30:19 +00:00
uchar data[3];
data[0] = NextByte();
data[1] = NextByte();
data[2] = NextByte();
uint32 val = (data[0] << 16) | (data[1] << 8) | data[2];
BMidiEvent* event = new BMidiEvent;
2006-06-19 13:24:04 +00:00
event->time = fTotalTicks;
2003-03-17 22:30:19 +00:00
event->ticks = true;
event->byte1 = 0xFF;
event->byte2 = 0x51;
event->byte3 = 0x03;
event->tempo = 60000000 / val;
AddEvent(event);
2005-07-07 12:42:57 +00:00
} else
2003-03-17 22:30:19 +00:00
SkipBytes(length);
2002-11-21 19:23:22 +00:00
}
2005-07-07 12:42:57 +00:00
void
BMidiStore::WriteTrack()
2003-03-17 22:30:19 +00:00
{
WriteFourCC('M', 'T', 'r', 'k');
2006-06-19 13:24:04 +00:00
off_t lengthPos = fFile->Position();
2003-03-17 22:30:19 +00:00
Write32Bit(0);
2006-06-19 13:24:04 +00:00
fByteCount = 0;
2005-07-07 12:42:57 +00:00
uint32 oldTime = 0;
uint32 newTime;
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
for (uint32 t = 0; t < CountEvents(); ++t) {
2003-03-17 22:30:19 +00:00
BMidiEvent* event = EventAt(t);
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
if (event->ticks)
newTime = event->time;
else
newTime = MillisecondsToTicks(event->time);
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
if (t == 0)
WriteVarLength(0);
else
WriteVarLength(newTime - oldTime);
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
oldTime = newTime;
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
switch (event->byte1 & 0xF0) {
2003-03-17 22:30:19 +00:00
case B_NOTE_OFF:
case B_NOTE_ON:
case B_KEY_PRESSURE:
case B_CONTROL_CHANGE:
case B_PITCH_BEND:
WriteByte(event->byte1);
WriteByte(event->byte2);
WriteByte(event->byte3);
break;
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
case B_PROGRAM_CHANGE:
case B_CHANNEL_PRESSURE:
WriteByte(event->byte1);
WriteByte(event->byte2);
break;
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
case 0xF0:
2005-07-07 12:42:57 +00:00
switch (event->byte1) {
2003-03-17 22:30:19 +00:00
case B_SYS_EX_START:
// We do not export sysex's.
break;
case B_TUNE_REQUEST:
case B_SYS_EX_END:
case B_TIMING_CLOCK:
case B_START:
case B_CONTINUE:
case B_STOP:
case B_ACTIVE_SENSING:
WriteByte(event->byte1);
break;
case B_MIDI_TIME_CODE:
case B_SONG_SELECT:
case B_CABLE_MESSAGE:
WriteByte(event->byte1);
WriteByte(event->byte2);
break;
case B_SONG_POSITION:
WriteByte(event->byte1);
WriteByte(event->byte2);
WriteByte(event->byte3);
break;
case B_SYSTEM_RESET:
WriteMetaEvent(event);
break;
}
break;
}
}
2002-11-21 19:23:22 +00:00
2003-03-17 22:30:19 +00:00
WriteVarLength(0);
WriteByte(0xFF); // the end-of-track
WriteByte(0x2F); // marker is required
WriteByte(0x00);
2002-11-21 19:23:22 +00:00
2006-06-19 13:24:04 +00:00
fFile->Seek(lengthPos, SEEK_SET);
Write32Bit(fByteCount);
fFile->Seek(0, SEEK_END);
2002-11-21 19:23:22 +00:00
}
2002-11-21 19:23:22 +00:00
2005-07-07 12:42:57 +00:00
void
BMidiStore::WriteMetaEvent(BMidiEvent* event)
2002-11-21 19:23:22 +00:00
{
2003-03-17 22:30:19 +00:00
// We only export the Tempo Change meta event.
2005-07-07 12:42:57 +00:00
if (event->byte2 == 0x51 && event->byte3 == 0x03) {
2003-03-17 22:30:19 +00:00
uint32 val = 60000000 / event->tempo;
WriteByte(0xFF);
WriteByte(0x51);
WriteByte(0x03);
WriteByte(val >> 16);
WriteByte(val >> 8);
WriteByte(val);
}
2002-11-21 19:23:22 +00:00
}