13
0

SMF: API change: report format (0,1 or2) and total channels used

This commit is contained in:
Ben Loftis 2021-11-13 15:49:12 -06:00
parent 4c472ca1c5
commit 35312dc85c
2 changed files with 46 additions and 32 deletions

View File

@ -46,7 +46,6 @@ SMF::SMF()
: _smf (0) : _smf (0)
, _smf_track (0) , _smf_track (0)
, _empty (true) , _empty (true)
, _type0 (false)
{}; {};
SMF::~SMF() SMF::~SMF()
@ -54,6 +53,12 @@ SMF::~SMF()
close (); close ();
} }
int
SMF::smf_format () const
{
return _smf ? _smf->format : 0;
}
uint16_t uint16_t
SMF::num_tracks() const SMF::num_tracks() const
{ {
@ -120,8 +125,7 @@ SMF::open(const std::string& path, int track)
{ {
Glib::Threads::Mutex::Lock lm (_smf_lock); Glib::Threads::Mutex::Lock lm (_smf_lock);
_type0 = false; _num_channels = 0;
_type0channels.clear ();
assert(track >= 1); assert(track >= 1);
if (_smf) { if (_smf) {
@ -150,31 +154,44 @@ SMF::open(const std::string& path, int track)
fclose(f); fclose(f);
bool type0 = _smf->format==0;
lm.release (); lm.release ();
if (_smf->format == 0 && _smf->number_of_tracks == 1 && !_empty) { if (!_empty) {
// type-0 file: scan file for # of used channels.
int ret; for (int i = 1; i <= _smf->number_of_tracks; ++i) {
uint32_t delta_t = 0;
uint32_t size = 0; // scan file for used channels.
uint8_t* buf = NULL; int ret;
event_id_t event_id = 0; uint32_t delta_t = 0;
seek_to_start(); uint32_t size = 0;
while ((ret = read_event (&delta_t, &size, &buf, &event_id)) >= 0) { uint8_t* buf = NULL;
if (ret == 0) { event_id_t event_id = 0;
continue;
if (type0) {
seek_to_start (); //type0 files have no 'track' concept, just seek_to_start
} else {
seek_to_track (i);
} }
if (size == 0) {
break; std::set<uint8_t> used;
while ((ret = read_event (&delta_t, &size, &buf, &event_id)) >= 0) {
if (ret == 0) {
continue;
}
if (size == 0) {
break;
}
uint8_t type = buf[0] & 0xf0;
uint8_t chan = buf[0] & 0x0f;
if (type >= 0x80 && type <= 0xE0) {
used.insert(chan);
}
} }
uint8_t type = buf[0] & 0xf0; _num_channels += used.size();
uint8_t chan = buf[0] & 0x0f; free (buf);
if (type < 0x80 || type > 0xE0) {
continue;
}
_type0channels.insert(chan);
} }
free (buf);
_type0 = true;
seek_to_start(); seek_to_start();
} }
return 0; return 0;
@ -237,8 +254,7 @@ SMF::create(const std::string& path, int track, uint16_t ppqn)
} }
_empty = true; _empty = true;
_type0 = false; _num_channels = 0;
_type0channels.clear ();
return 0; return 0;
} }
@ -252,8 +268,7 @@ SMF::close()
smf_delete(_smf); smf_delete(_smf);
_smf = 0; _smf = 0;
_smf_track = 0; _smf_track = 0;
_type0 = false; _num_channels = 0;
_type0channels.clear ();
} }
} }

View File

@ -86,8 +86,8 @@ public:
double round_to_file_precision (double val) const; double round_to_file_precision (double val) const;
bool is_type0 () const { return _type0; } int smf_format () const;
std::set<uint8_t> channels () const { return _type0channels; } int num_channels () const { return _num_channels; }
void track_names (std::vector<std::string>&) const; void track_names (std::vector<std::string>&) const;
void instrument_names (std::vector<std::string>&) const; void instrument_names (std::vector<std::string>&) const;
@ -135,8 +135,7 @@ public:
bool _empty; ///< true iff file contains(non-empty) events bool _empty; ///< true iff file contains(non-empty) events
mutable Glib::Threads::Mutex _smf_lock; mutable Glib::Threads::Mutex _smf_lock;
bool _type0; int _num_channels;
std::set<uint8_t> _type0channels;
mutable Markers _markers; mutable Markers _markers;
}; };