Fix test breakage from a855119bdd
[ardour.git] / libs / evoral / test / SMFTest.cc
1 #include "SMFTest.h"
2
3 #include <glibmm/fileutils.h>
4 #include <glibmm/miscutils.h>
5
6 #include "pbd/file_utils.h"
7
8 using namespace std;
9
10 CPPUNIT_TEST_SUITE_REGISTRATION( SMFTest );
11
12 void
13 SMFTest::createNewFileTest ()
14 {
15         TestSMF smf;
16
17         string output_dir_path = PBD::tmp_writable_directory (PACKAGE, "createNewFileTest");
18         string new_file_path = Glib::build_filename (output_dir_path, "NewFile.mid");
19         smf.create(new_file_path);
20         smf.close();
21         CPPUNIT_ASSERT(Glib::file_test (new_file_path, Glib::FILE_TEST_IS_REGULAR));
22 }
23
24 PBD::Searchpath
25 test_search_path ()
26 {
27 #ifdef PLATFORM_WINDOWS
28         if (!getenv("EVORAL_TEST_PATH")) {
29                 string wsp(g_win32_get_package_installation_directory_of_module(NULL));
30                 return Glib::build_filename (wsp,  "evoral_testdata");
31         }
32 #endif
33         return Glib::getenv("EVORAL_TEST_PATH");
34 }
35
36 void
37 SMFTest::takeFiveTest ()
38 {
39         TestSMF smf;
40         string testdata_path;
41         CPPUNIT_ASSERT (find_file (test_search_path (), "TakeFive.mid", testdata_path));
42         CPPUNIT_ASSERT (SMF::test(testdata_path));
43
44         smf.open(testdata_path);
45         CPPUNIT_ASSERT(!smf.is_empty());
46
47         CPPUNIT_ASSERT_EQUAL((uint16_t)1, smf.num_tracks());
48         CPPUNIT_ASSERT_EQUAL(0, smf.seek_to_track(1));
49
50         seq->start_write();
51         smf.seek_to_start();
52
53         uint64_t time = 0; /* in SMF ticks */
54         Evoral::Event<Time> ev;
55
56         uint32_t delta_t = 0;
57         uint32_t size    = 0;
58         uint8_t* buf     = NULL;
59         int ret;
60         while ((ret = smf.read_event(&delta_t, &size, &buf)) >= 0) {
61                 ev.set(buf, size, Time());
62                 time += delta_t;
63
64                 if (ret > 0) { // didn't skip (meta) event
65                         //cerr << "read smf event type " << hex << int(buf[0]) << endl;
66                         ev.set_time(Temporal::Beats::ticks_at_rate(time, smf.ppqn()));
67                         ev.set_event_type(Evoral::MIDI_EVENT);
68                         seq->append(ev, next_event_id ());
69                 }
70         }
71
72         seq->end_write (Sequence<Time>::Relax,
73                         Temporal::Beats::ticks_at_rate(time, smf.ppqn()));
74         CPPUNIT_ASSERT(!seq->empty());
75
76         // Iterate over all notes
77         bool   on          = true;
78         size_t num_notes   = 0;
79         size_t num_sysexes = 0;
80         for (Sequence<Time>::const_iterator i = seq->begin(Time()); i != seq->end(); ++i) {
81                 if (i->is_note_on()) {
82                         ++num_notes;
83                 } else if (i->is_sysex()) {
84                         ++num_sysexes;
85                 }
86         }
87         CPPUNIT_ASSERT_EQUAL(size_t(3833), seq->notes().size());
88         CPPUNIT_ASSERT_EQUAL(size_t(3833), num_notes);
89         CPPUNIT_ASSERT_EQUAL(size_t(232), seq->sysexes().size());
90         CPPUNIT_ASSERT_EQUAL(size_t(232), num_sysexes);
91 }
92
93 void
94 SMFTest::writeTest ()
95 {
96         TestSMF smf;
97         string  testdata_path;
98         CPPUNIT_ASSERT (find_file (test_search_path (), "TakeFive.mid", testdata_path));
99
100         smf.open(testdata_path);
101         CPPUNIT_ASSERT(!smf.is_empty());
102
103         TestSMF out;
104         const string output_dir_path = PBD::tmp_writable_directory (PACKAGE, "writeTest");
105         const string new_file_path   = Glib::build_filename (output_dir_path, "TakeFiveCopy.mid");
106         CPPUNIT_ASSERT_EQUAL (0, out.create(new_file_path, 1, 1920));
107         out.begin_write();
108
109         uint32_t delta_t = 0;
110         uint32_t size    = 0;
111         uint8_t* buf     = NULL;
112         while (smf.read_event(&delta_t, &size, &buf) >= 0) {
113                 out.append_event_delta(delta_t, size, buf, 0);
114         }
115
116         out.end_write(new_file_path);
117
118         // TODO: Check files are actually equivalent
119 }