Exemple #1
0
bool PianoPanel::MIDITrackContainsNotes(int track, MidiFile* midifile)
{
	for (int i = 0; i < midifile->getNumEvents(track); i++)
	{
		MidiEvent e = midifile->getEvent(track, i);
		if (e.isNote())
		{
			return true;
		}
	}

	return false;
}
void PCH_ChartManager::LoadMidiToDB(PCH_CString filePath,PCH_CString fileHash, bool update)
{
    std::ifstream in2(filePath, std::ios::in | std::ios::binary);
    MidiFile midifile(in2);
    if (!midifile.status())
    {
        printf("Error reading MIDI file %s\n",filePath);
        return;
    }

    printf("Loading midi file data...\n");
    if(update)
    {
        printf("Deleting existing data...\n");
        _db->DeleteChart(filePath);
    }

    midifile.absoluteTicks();
    midifile.linkNotePairs();

    midifile.doTimeAnalysis();

    printf("Analysis complete...\n");

    int totalTracks =midifile.size();

    MidiEvent* mev;

    int chartID = _db->AddChart(filePath, fileHash);
    int trackID = 0;
    int trackItemCount = 0;

#ifndef PCH_USE_HEAP
    PCH_ControllerEvent newEvent;
    PCH_ChartInstrument newInst;
    PCH_ChartTrackNote newNote;
    PCH_PitchBend newBend;
#endif

    for (int track=0; track < totalTracks; track++)
    {
        trackItemCount = midifile[track].size();

        if(trackItemCount == 0) continue;

        trackID = _db->AddTrack(chartID,track);
        printf("Track #%d added...\n",trackID);

        _db->BeginTransaction();

        for (int i=0; i<trackItemCount; i++)
        {
            mev = &midifile[track][i];

            if(mev->isTimbre())
            {
#ifdef PCH_USE_HEAP
                PCH_ChartInstrument* newInst = new PCH_ChartInstrument();
                newInst->Channel = mev->getChannel();
                newInst->Seconds = mev->seconds;
                newInst->InstrumentID = mev->getP1();

                _db->AddInstrument(trackID,newInst);

                delete newInst;
                newInst = NULL;
#else
                newInst.Channel = mev->getChannel();
                newInst.Seconds = mev->seconds;
                newInst.InstrumentID = mev->getP1();

                _db->AddInstrument(trackID,&newInst);
#endif
            }

            if(mev->getCommandByte() == 0xFF && mev->getP1() == 0x03)//Title
            {
                printf("Title found\n");
                unsigned char* title = &midifile[track][i][3];//CMD + p1 + p2 are omitted

                int titleSize = mev->getP2();
                if(titleSize > 0)
                {
                    char* strTitle = new char[titleSize+1];

                    memcpy(strTitle,title,titleSize);
                    strTitle[titleSize] = '\0';

                    _db->SetTrackTitle(trackID, strTitle);
                }
                printf("Title end\n");
            }

            if(mev->isNote()) //if(false)
            {
                if(mev->isNoteOn())
                {
#ifdef PCH_USE_HEAP
                    PCH_ChartTrackNote* newNote = new PCH_ChartTrackNote();
                    newNote->Seconds = mev->seconds;
                    newNote->SecondsDuration = mev->getDurationInSeconds();
                    newNote->Channel = mev->getChannel();
                    newNote->KeyNumber =  mev->getKeyNumber();
                    newNote->Velocity = mev->getVelocity();

                    _db->AddNote(trackID, newNote);

                    delete newNote;
                    newNote = NULL;
#else
                    newNote.Seconds = mev->seconds;
                    newNote.SecondsDuration = mev->getDurationInSeconds();
                    newNote.Channel = mev->getChannel();
                    newNote.KeyNumber =  mev->getKeyNumber();
                    newNote.Velocity = mev->getVelocity();

                    _db->AddNote(trackID, &newNote);
#endif
                }
            }

            if(mev->isController())
            {
#ifdef PCH_USE_HEAP
                PCH_ControllerEvent* newEvent = new PCH_ControllerEvent();
                newEvent->Seconds = mev->seconds;
                newEvent->Channel = mev->getChannel();
                newEvent->ControllerID = mev->getP1();
                newEvent->Value = mev->getP2();

                _db->AddEvent(trackID, newEvent);

                delete newEvent;
                newEvent = NULL;
#else
                newEvent.Seconds = mev->seconds;
                newEvent.Channel = mev->getChannel();
                newEvent.ControllerID = mev->getP1();
                newEvent.Value = mev->getP2();

                _db->AddEvent(trackID, &newEvent);
#endif
            }

            if(mev->isPitchbend())
            {
#ifdef PCH_USE_HEAP
                PCH_PitchBend* newBend = new PCH_PitchBend();
                newBend->Seconds = mev->seconds;
                newBend->Channel = mev->getChannel();
                newBend->PitchBend = mev->getP1() + (mev->getP2() << 7);

                _db->AddPitchBend(trackID, newBend);

                delete newBend;
                newBend = NULL;
#else
                newBend.Seconds = mev->seconds;
                newBend.Channel = mev->getChannel();
                newBend.PitchBend = mev->getP1() + (mev->getP2() << 7);
                _db->AddPitchBend(trackID, &newBend);
#endif
            }
        }

        _db->CommitTransaction();
    }
}