2009-02-09 17:32:38 -05:00
|
|
|
#include "SMFTest.hpp"
|
2009-02-10 18:58:02 -05:00
|
|
|
|
2014-11-29 04:11:22 -05:00
|
|
|
#include <glibmm/fileutils.h>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
|
|
|
|
#include "pbd/file_utils.h"
|
2013-07-11 13:03:55 -04:00
|
|
|
|
2009-02-10 18:58:02 -05:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( SMFTest );
|
|
|
|
|
2009-02-12 18:28:51 -05:00
|
|
|
void
|
|
|
|
SMFTest::createNewFileTest ()
|
|
|
|
{
|
2009-02-14 20:32:41 -05:00
|
|
|
TestSMF smf;
|
2014-11-29 04:11:22 -05:00
|
|
|
|
|
|
|
string output_dir_path = PBD::tmp_writable_directory (PACKAGE, "createNewFileTest");
|
|
|
|
string new_file_path = Glib::build_filename (output_dir_path, "NewFile.mid");
|
|
|
|
smf.create(new_file_path);
|
2009-02-12 18:28:51 -05:00
|
|
|
smf.close();
|
2014-11-29 04:11:22 -05:00
|
|
|
CPPUNIT_ASSERT(Glib::file_test (new_file_path, Glib::FILE_TEST_IS_REGULAR));
|
2009-02-12 18:28:51 -05:00
|
|
|
}
|
|
|
|
|
2014-11-29 04:30:35 -05:00
|
|
|
PBD::Searchpath
|
|
|
|
test_search_path ()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
2015-09-13 09:30:00 -04:00
|
|
|
if (!getenv("EVORAL_TEST_PATH")) {
|
|
|
|
string wsp(g_win32_get_package_installation_directory_of_module(NULL));
|
|
|
|
return Glib::build_filename (wsp, "evoral_testdata");
|
|
|
|
}
|
2014-11-29 04:30:35 -05:00
|
|
|
#endif
|
2015-09-13 09:30:00 -04:00
|
|
|
return Glib::getenv("EVORAL_TEST_PATH");
|
2014-11-29 04:30:35 -05:00
|
|
|
}
|
|
|
|
|
2009-02-10 18:58:02 -05:00
|
|
|
void
|
|
|
|
SMFTest::takeFiveTest ()
|
|
|
|
{
|
2009-02-14 20:32:41 -05:00
|
|
|
TestSMF smf;
|
2014-11-29 04:30:35 -05:00
|
|
|
string testdata_path;
|
|
|
|
CPPUNIT_ASSERT (find_file (test_search_path (), "TakeFive.mid", testdata_path));
|
2016-12-04 15:40:21 -05:00
|
|
|
CPPUNIT_ASSERT (SMF::test(testdata_path));
|
|
|
|
|
2014-11-29 04:30:35 -05:00
|
|
|
smf.open(testdata_path);
|
2009-02-10 18:58:02 -05:00
|
|
|
CPPUNIT_ASSERT(!smf.is_empty());
|
2009-10-19 11:26:56 -04:00
|
|
|
|
2016-12-04 15:40:21 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)1, smf.num_tracks());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, smf.seek_to_track(1));
|
|
|
|
|
2009-02-10 18:58:02 -05:00
|
|
|
seq->start_write();
|
|
|
|
smf.seek_to_start();
|
|
|
|
|
|
|
|
uint64_t time = 0; /* in SMF ticks */
|
2015-01-07 00:12:07 -05:00
|
|
|
Evoral::Event<Evoral::Beats> ev;
|
2009-10-19 11:26:56 -04:00
|
|
|
|
2009-02-10 18:58:02 -05:00
|
|
|
uint32_t delta_t = 0;
|
|
|
|
uint32_t size = 0;
|
|
|
|
uint8_t* buf = NULL;
|
|
|
|
int ret;
|
|
|
|
while ((ret = smf.read_event(&delta_t, &size, &buf)) >= 0) {
|
2015-01-07 00:12:07 -05:00
|
|
|
ev.set(buf, size, Evoral::Beats());
|
2009-02-10 18:58:02 -05:00
|
|
|
time += delta_t;
|
2009-10-19 11:26:56 -04:00
|
|
|
|
2009-02-10 18:58:02 -05:00
|
|
|
if (ret > 0) { // didn't skip (meta) event
|
2009-02-11 15:20:41 -05:00
|
|
|
//cerr << "read smf event type " << hex << int(buf[0]) << endl;
|
2015-01-07 00:12:07 -05:00
|
|
|
ev.set_time(Evoral::Beats::ticks_at_rate(time, smf.ppqn()));
|
2016-11-07 05:14:55 -05:00
|
|
|
ev.set_event_type(Evoral::MIDI_EVENT);
|
2010-07-24 20:39:04 -04:00
|
|
|
seq->append(ev, next_event_id ());
|
2009-02-10 18:58:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-22 04:05:42 -05:00
|
|
|
seq->end_write (Sequence<Time>::Relax,
|
2015-01-07 00:12:07 -05:00
|
|
|
Evoral::Beats::ticks_at_rate(time, smf.ppqn()));
|
2009-02-10 18:58:02 -05:00
|
|
|
CPPUNIT_ASSERT(!seq->empty());
|
2016-12-04 18:49:56 -05:00
|
|
|
|
|
|
|
// Iterate over all notes
|
|
|
|
bool on = true;
|
|
|
|
size_t num_notes = 0;
|
|
|
|
size_t num_sysexes = 0;
|
|
|
|
for (Sequence<Time>::const_iterator i = seq->begin(Evoral::Beats()); i != seq->end(); ++i) {
|
|
|
|
if (i->is_note_on()) {
|
|
|
|
++num_notes;
|
|
|
|
} else if (i->is_sysex()) {
|
|
|
|
++num_sysexes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(3833), seq->notes().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(3833), num_notes);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(232), seq->sysexes().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(232), num_sysexes);
|
2009-02-10 18:58:02 -05:00
|
|
|
}
|
2016-12-04 15:40:21 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
SMFTest::writeTest ()
|
|
|
|
{
|
|
|
|
TestSMF smf;
|
|
|
|
string testdata_path;
|
|
|
|
CPPUNIT_ASSERT (find_file (test_search_path (), "TakeFive.mid", testdata_path));
|
|
|
|
|
|
|
|
smf.open(testdata_path);
|
|
|
|
CPPUNIT_ASSERT(!smf.is_empty());
|
|
|
|
|
|
|
|
TestSMF out;
|
|
|
|
const string output_dir_path = PBD::tmp_writable_directory (PACKAGE, "writeTest");
|
|
|
|
const string new_file_path = Glib::build_filename (output_dir_path, "TakeFiveCopy.mid");
|
|
|
|
CPPUNIT_ASSERT_EQUAL (0, out.create(new_file_path, 1, 1920));
|
|
|
|
out.begin_write();
|
|
|
|
|
|
|
|
uint32_t delta_t = 0;
|
|
|
|
uint32_t size = 0;
|
|
|
|
uint8_t* buf = NULL;
|
|
|
|
while (smf.read_event(&delta_t, &size, &buf) >= 0) {
|
|
|
|
out.append_event_delta(delta_t, size, buf, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
out.end_write(new_file_path);
|
|
|
|
|
|
|
|
// TODO: Check files are actually equivalent
|
|
|
|
}
|