aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/SMFWriter.cpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2013-01-12 03:17:47 +0000
committerDavid Robillard <d@drobilla.net>2013-01-12 03:17:47 +0000
commite1a63732f796c8057751cb7f8ac98a595b91692e (patch)
tree764392d055901118ee285dd20f233ff33e0cd04a /src/engine/SMFWriter.cpp
parentbb62bf2c23ba84f547e37b70b3ef31b51ba987ab (diff)
downloadmachina-e1a63732f796c8057751cb7f8ac98a595b91692e.tar.gz
machina-e1a63732f796c8057751cb7f8ac98a595b91692e.tar.bz2
machina-e1a63732f796c8057751cb7f8ac98a595b91692e.zip
Bulk reformat.
git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4929 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/engine/SMFWriter.cpp')
-rw-r--r--src/engine/SMFWriter.cpp53
1 files changed, 31 insertions, 22 deletions
diff --git a/src/engine/SMFWriter.cpp b/src/engine/SMFWriter.cpp
index ad993bc..41abfe0 100644
--- a/src/engine/SMFWriter.cpp
+++ b/src/engine/SMFWriter.cpp
@@ -1,5 +1,5 @@
/*
- This file is part of Raul.
+ This file is part of Machina.
Copyright 2007-2012 David Robillard <http://drobilla.net>
Raul is free software: you can redistribute it and/or modify it under the
@@ -45,14 +45,16 @@ SMFWriter::SMFWriter(Raul::TimeUnit unit)
, _track_size(0)
, _header_size(0)
{
- if (unit.type() == Raul::TimeUnit::BEATS)
+ if (unit.type() == Raul::TimeUnit::BEATS) {
assert(unit.ppt() < std::numeric_limits<uint16_t>::max());
+ }
}
SMFWriter::~SMFWriter()
{
- if (_fd)
+ if (_fd) {
finish();
+ }
}
/** Start a write to an SMF file.
@@ -65,17 +67,19 @@ bool
SMFWriter::start(const std::string& filename,
Raul::TimeStamp start_time) throw (std::logic_error)
{
- if (_fd)
- throw std::logic_error("Attempt to start new write while write in progress.");
+ if (_fd) {
+ throw std::logic_error(
+ "Attempt to start new write while write in progress.");
+ }
std::cout << "Opening SMF file " << filename << " for writing." << endl;
_fd = fopen(filename.c_str(), "w+");
if (_fd) {
- _track_size = 0;
- _filename = filename;
- _start_time = start_time;
+ _track_size = 0;
+ _filename = filename;
+ _start_time = start_time;
_last_ev_time = 0;
// write a tentative header to pad file out so writing starts at the right offset
write_header();
@@ -95,19 +99,21 @@ SMFWriter::write_event(Raul::TimeStamp time,
size_t ev_size,
const unsigned char* ev) throw (std::logic_error)
{
- if (time < _start_time)
+ if (time < _start_time) {
throw std::logic_error("Event time is before file start time");
- else if (time < _last_ev_time)
+ } else if (time < _last_ev_time) {
throw std::logic_error("Event time not monotonically increasing");
- else if (time.unit() != _unit)
+ } else if (time.unit() != _unit) {
throw std::logic_error("Event has unexpected time unit");
+ }
Raul::TimeStamp delta_time = time;
delta_time -= _start_time;
fseek(_fd, 0, SEEK_END);
- uint64_t delta_ticks = delta_time.ticks() * _unit.ppt() + delta_time.subticks();
+ uint64_t delta_ticks = delta_time.ticks() * _unit.ppt()
+ + delta_time.subticks();
size_t stamp_size = 0;
/* If delta time is too long (i.e. overflows), write out empty
@@ -127,21 +133,24 @@ SMFWriter::write_event(Raul::TimeStamp time,
fwrite(ev, 1, ev_size, _fd);
_last_ev_time = time;
- _track_size += stamp_size + ev_size;
+ _track_size += stamp_size + ev_size;
}
void
SMFWriter::flush()
{
- if (_fd)
+ if (_fd) {
fflush(_fd);
+ }
}
void
SMFWriter::finish() throw (std::logic_error)
{
- if (!_fd)
- throw std::logic_error("Attempt to finish write with no write in progress.");
+ if (!_fd) {
+ throw std::logic_error(
+ "Attempt to finish write with no write in progress.");
+ }
write_footer();
fclose(_fd);
@@ -161,8 +170,8 @@ SMFWriter::write_header()
char data[6];
memcpy(data, &type, 2);
- memcpy(data+2, &ntracks, 2);
- memcpy(data+4, &division, 2);
+ memcpy(data + 2, &ntracks, 2);
+ memcpy(data + 4, &division, 2);
//data[4] = _ppqn & 0xF0;
//data[5] = _ppqn & 0x0F;
@@ -214,21 +223,21 @@ SMFWriter::write_var_len(uint32_t value)
while ( (value >>= 7) ) {
buffer <<= 8;
- buffer |= ((value & 0x7F) | 0x80);
+ buffer |= ((value & 0x7F) | 0x80);
}
while (true) {
//printf("Writing var len byte %X\n", (unsigned char)buffer);
++ret;
fputc(buffer, _fd);
- if (buffer & 0x80)
+ if (buffer & 0x80) {
buffer >>= 8;
- else
+ } else {
break;
+ }
}
return ret;
}
} // namespace Machina
-