smflite

diff src/smf_tempo.c @ 0:4264abea8b06

smf-lite initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Jan 2012 11:25:11 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/smf_tempo.c	Thu Jan 26 11:25:11 2012 +0200
     1.3 @@ -0,0 +1,448 @@
     1.4 +/*-
     1.5 + * Copyright (c) 2007, 2008 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + *
    1.17 + * ALTHOUGH THIS SOFTWARE IS MADE OF WIN AND SCIENCE, IT IS PROVIDED BY THE
    1.18 + * AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    1.19 + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    1.20 + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    1.21 + * THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.22 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    1.23 + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    1.24 + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    1.25 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.26 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.27 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +/**
    1.32 + * \file
    1.33 + *
    1.34 + * Tempo map related part.
    1.35 + *
    1.36 + */
    1.37 +
    1.38 +#include <stdlib.h>
    1.39 +#include <assert.h>
    1.40 +#include <math.h>
    1.41 +#include <string.h>
    1.42 +#include "smf.h"
    1.43 +#include "smf_private.h"
    1.44 +
    1.45 +static double seconds_from_pulses(const smf_t *smf, int pulses);
    1.46 +
    1.47 +/**
    1.48 + * If there is tempo starting at "pulses" already, return it.  Otherwise,
    1.49 + * allocate new one, fill it with values from previous one (or default ones,
    1.50 + * if there is no previous one) and attach it to "smf".
    1.51 + */
    1.52 +static smf_tempo_t *
    1.53 +new_tempo(smf_t *smf, int pulses)
    1.54 +{
    1.55 +	smf_tempo_t *tempo, *previous_tempo = NULL;
    1.56 +
    1.57 +	if (smf->tempo_array->len > 0) {
    1.58 +		previous_tempo = smf_get_last_tempo(smf);
    1.59 +
    1.60 +		/* If previous tempo starts at the same time as new one, reuse it, updating in place. */
    1.61 +		if (previous_tempo->time_pulses == pulses)
    1.62 +			return (previous_tempo);
    1.63 +	}
    1.64 +
    1.65 +	tempo = malloc(sizeof(smf_tempo_t));
    1.66 +	if (tempo == NULL) {
    1.67 +		fg_critical("Cannot allocate smf_tempo_t.");
    1.68 +		return (NULL);
    1.69 +	}
    1.70 +
    1.71 +	tempo->time_pulses = pulses;
    1.72 +
    1.73 +	if (previous_tempo != NULL) {
    1.74 +		tempo->microseconds_per_quarter_note = previous_tempo->microseconds_per_quarter_note;
    1.75 +		tempo->numerator = previous_tempo->numerator;
    1.76 +		tempo->denominator = previous_tempo->denominator;
    1.77 +		tempo->clocks_per_click = previous_tempo->clocks_per_click;
    1.78 +		tempo->notes_per_note = previous_tempo->notes_per_note;
    1.79 +	} else {
    1.80 +		tempo->microseconds_per_quarter_note = 500000; /* Initial tempo is 120 BPM. */
    1.81 +		tempo->numerator = 4;
    1.82 +		tempo->denominator = 4;
    1.83 +		tempo->clocks_per_click = -1;
    1.84 +		tempo->notes_per_note = -1;
    1.85 +	}
    1.86 +
    1.87 +	fg_ptr_array_add(smf->tempo_array, tempo);
    1.88 +
    1.89 +	if (pulses == 0)
    1.90 +		tempo->time_seconds = 0.0;
    1.91 +	else
    1.92 +		tempo->time_seconds = seconds_from_pulses(smf, pulses);
    1.93 +
    1.94 +	return (tempo);
    1.95 +}
    1.96 +
    1.97 +static int
    1.98 +add_tempo(smf_t *smf, int pulses, int tempo)
    1.99 +{
   1.100 +	smf_tempo_t *smf_tempo = new_tempo(smf, pulses);
   1.101 +	if (smf_tempo == NULL)
   1.102 +		return (-1);
   1.103 +
   1.104 +	smf_tempo->microseconds_per_quarter_note = tempo;
   1.105 +
   1.106 +	return (0);
   1.107 +}
   1.108 +
   1.109 +static int
   1.110 +add_time_signature(smf_t *smf, int pulses, int numerator, int denominator, int clocks_per_click, int notes_per_note)
   1.111 +{
   1.112 +	smf_tempo_t *smf_tempo = new_tempo(smf, pulses);
   1.113 +	if (smf_tempo == NULL)
   1.114 +		return (-1);
   1.115 +
   1.116 +	smf_tempo->numerator = numerator;
   1.117 +	smf_tempo->denominator = denominator;
   1.118 +	smf_tempo->clocks_per_click = clocks_per_click;
   1.119 +	smf_tempo->notes_per_note = notes_per_note;
   1.120 +
   1.121 +	return (0);
   1.122 +}
   1.123 +
   1.124 +/**
   1.125 + * \internal
   1.126 + */
   1.127 +void
   1.128 +maybe_add_to_tempo_map(smf_event_t *event)
   1.129 +{
   1.130 +	if (!smf_event_is_metadata(event))
   1.131 +		return;
   1.132 +
   1.133 +	assert(event->track != NULL);
   1.134 +	assert(event->track->smf != NULL);
   1.135 +	assert(event->midi_buffer_length >= 1);
   1.136 +
   1.137 +	/* Tempo Change? */
   1.138 +	if (event->midi_buffer[1] == 0x51) {
   1.139 +		int new_tempo = (event->midi_buffer[3] << 16) + (event->midi_buffer[4] << 8) + event->midi_buffer[5];
   1.140 +		if (new_tempo <= 0) {
   1.141 +			fg_critical("Ignoring invalid tempo change.");
   1.142 +			return;
   1.143 +		}
   1.144 +
   1.145 +		add_tempo(event->track->smf, event->time_pulses, new_tempo);
   1.146 +	}
   1.147 +
   1.148 +	/* Time Signature? */
   1.149 +	if (event->midi_buffer[1] == 0x58) {
   1.150 +		int numerator, denominator, clocks_per_click, notes_per_note;
   1.151 +
   1.152 +		if (event->midi_buffer_length < 7) {
   1.153 +			fg_critical("Time Signature event seems truncated.");
   1.154 +			return;
   1.155 +		}
   1.156 +
   1.157 +		numerator = event->midi_buffer[3];
   1.158 +		denominator = (int)pow(2, event->midi_buffer[4]);
   1.159 +		clocks_per_click = event->midi_buffer[5];
   1.160 +		notes_per_note = event->midi_buffer[6];
   1.161 +
   1.162 +		add_time_signature(event->track->smf, event->time_pulses, numerator, denominator, clocks_per_click, notes_per_note);
   1.163 +	}
   1.164 +
   1.165 +	return;
   1.166 +}
   1.167 +
   1.168 +/**
   1.169 + * \internal
   1.170 + *
   1.171 + * This is an internal function, called from smf_track_remove_event when tempo-related
   1.172 + * event being removed does not require recreation of tempo map, i.e. there are no events
   1.173 + * after that one.
   1.174 + */
   1.175 +void
   1.176 +remove_last_tempo_with_pulses(smf_t *smf, int pulses)
   1.177 +{
   1.178 +	smf_tempo_t *tempo;
   1.179 +
   1.180 +	/* XXX: This is a partial workaround for the following problem: we have two tempo-related
   1.181 +	   events, A and B, that occur at the same time.  We remove B, then try to remove
   1.182 +	   A.  However, both tempo changes got coalesced in new_tempo(), so it is impossible
   1.183 +	   to remove B. */
   1.184 +	if (smf->tempo_array->len == 0)
   1.185 +		return;
   1.186 +
   1.187 +	tempo = smf_get_last_tempo(smf);
   1.188 +
   1.189 +	/* Workaround part two. */
   1.190 +	if (tempo->time_pulses != pulses)
   1.191 +		return;
   1.192 +
   1.193 +	memset(tempo, 0, sizeof(smf_tempo_t));
   1.194 +	free(tempo);
   1.195 +
   1.196 +	fg_ptr_array_remove_index(smf->tempo_array, smf->tempo_array->len - 1);
   1.197 +}
   1.198 +
   1.199 +static double
   1.200 +seconds_from_pulses(const smf_t *smf, int pulses)
   1.201 +{
   1.202 +	double seconds;
   1.203 +	smf_tempo_t *tempo;
   1.204 +
   1.205 +	tempo = smf_get_tempo_by_pulses(smf, pulses);
   1.206 +	assert(tempo);
   1.207 +	assert(tempo->time_pulses <= pulses);
   1.208 +
   1.209 +	seconds = tempo->time_seconds + (double)(pulses - tempo->time_pulses) *
   1.210 +		(tempo->microseconds_per_quarter_note / ((double)smf->ppqn * 1000000.0));
   1.211 +
   1.212 +	return (seconds);
   1.213 +}
   1.214 +
   1.215 +static int
   1.216 +pulses_from_seconds(const smf_t *smf, double seconds)
   1.217 +{
   1.218 +	int pulses = 0;
   1.219 +	smf_tempo_t *tempo;
   1.220 +
   1.221 +	tempo = smf_get_tempo_by_seconds(smf, seconds);
   1.222 +	assert(tempo);
   1.223 +	assert(tempo->time_seconds <= seconds);
   1.224 +
   1.225 +	pulses = tempo->time_pulses + (seconds - tempo->time_seconds) *
   1.226 +		((double)smf->ppqn * 1000000.0 / tempo->microseconds_per_quarter_note);
   1.227 +
   1.228 +	return (pulses);
   1.229 +}
   1.230 +
   1.231 +/**
   1.232 + * \internal
   1.233 + *
   1.234 + * Computes value of event->time_seconds for all events in smf.
   1.235 + * Warning: rewinds the smf.
   1.236 + */
   1.237 +void
   1.238 +smf_create_tempo_map_and_compute_seconds(smf_t *smf)
   1.239 +{
   1.240 +	smf_event_t *event;
   1.241 +
   1.242 +	smf_rewind(smf);
   1.243 +	smf_init_tempo(smf);
   1.244 +
   1.245 +	for (;;) {
   1.246 +		event = smf_get_next_event(smf);
   1.247 +
   1.248 +		if (event == NULL)
   1.249 +			return;
   1.250 +
   1.251 +		maybe_add_to_tempo_map(event);
   1.252 +
   1.253 +		event->time_seconds = seconds_from_pulses(smf, event->time_pulses);
   1.254 +	}
   1.255 +
   1.256 +	/* Not reached. */
   1.257 +}
   1.258 +
   1.259 +smf_tempo_t *
   1.260 +smf_get_tempo_by_number(const smf_t *smf, int number)
   1.261 +{
   1.262 +	assert(number >= 0);
   1.263 +
   1.264 +	if (number >= smf->tempo_array->len)
   1.265 +		return (NULL);
   1.266 +
   1.267 +	return (fg_ptr_array_index(smf->tempo_array, number));
   1.268 +}
   1.269 +
   1.270 +/**
   1.271 + * Return last tempo (i.e. tempo with greatest time_pulses) that happens before "pulses".
   1.272 + */
   1.273 +smf_tempo_t *
   1.274 +smf_get_tempo_by_pulses(const smf_t *smf, int pulses)
   1.275 +{
   1.276 +	int i;
   1.277 +	smf_tempo_t *tempo;
   1.278 +
   1.279 +	assert(pulses >= 0);
   1.280 +
   1.281 +	if (pulses == 0)
   1.282 +		return (smf_get_tempo_by_number(smf, 0));
   1.283 +
   1.284 +	assert(smf->tempo_array != NULL);
   1.285 +
   1.286 +	for (i = smf->tempo_array->len - 1; i >= 0; i--) {
   1.287 +		tempo = smf_get_tempo_by_number(smf, i);
   1.288 +
   1.289 +		assert(tempo);
   1.290 +		if (tempo->time_pulses < pulses)
   1.291 +			return (tempo);
   1.292 +	}
   1.293 +
   1.294 +	return (NULL);
   1.295 +}
   1.296 +
   1.297 +/**
   1.298 + * Return last tempo (i.e. tempo with greatest time_seconds) that happens before "seconds".
   1.299 + */
   1.300 +smf_tempo_t *
   1.301 +smf_get_tempo_by_seconds(const smf_t *smf, double seconds)
   1.302 +{
   1.303 +	int i;
   1.304 +	smf_tempo_t *tempo;
   1.305 +
   1.306 +	assert(seconds >= 0.0);
   1.307 +
   1.308 +	if (seconds == 0.0)
   1.309 +		return (smf_get_tempo_by_number(smf, 0));
   1.310 +
   1.311 +	assert(smf->tempo_array != NULL);
   1.312 +
   1.313 +	for (i = smf->tempo_array->len - 1; i >= 0; i--) {
   1.314 +		tempo = smf_get_tempo_by_number(smf, i);
   1.315 +
   1.316 +		assert(tempo);
   1.317 +		if (tempo->time_seconds < seconds)
   1.318 +			return (tempo);
   1.319 +	}
   1.320 +
   1.321 +	return (NULL);
   1.322 +}
   1.323 +
   1.324 +
   1.325 +/**
   1.326 + * Return last tempo.
   1.327 + */
   1.328 +smf_tempo_t *
   1.329 +smf_get_last_tempo(const smf_t *smf)
   1.330 +{
   1.331 +	smf_tempo_t *tempo;
   1.332 +
   1.333 +	tempo = smf_get_tempo_by_number(smf, smf->tempo_array->len - 1);
   1.334 +	assert(tempo);
   1.335 +
   1.336 +	return (tempo);
   1.337 +}
   1.338 +
   1.339 +/**
   1.340 + * \internal 
   1.341 + *
   1.342 + * Remove all smf_tempo_t structures from SMF.
   1.343 + */
   1.344 +void
   1.345 +smf_fini_tempo(smf_t *smf)
   1.346 +{
   1.347 +	smf_tempo_t *tempo;
   1.348 +
   1.349 +	while (smf->tempo_array->len > 0) {
   1.350 +		tempo = fg_ptr_array_index(smf->tempo_array, smf->tempo_array->len - 1);
   1.351 +		assert(tempo);
   1.352 +
   1.353 +		memset(tempo, 0, sizeof(smf_tempo_t));
   1.354 +		free(tempo);
   1.355 +
   1.356 +		fg_ptr_array_remove_index(smf->tempo_array, smf->tempo_array->len - 1);
   1.357 +	}
   1.358 +
   1.359 +	assert(smf->tempo_array->len == 0);
   1.360 +}
   1.361 +
   1.362 +/**
   1.363 + * \internal
   1.364 + *
   1.365 + * Remove any existing tempos and add default one.
   1.366 + *
   1.367 + * \bug This will abort (by calling fg_error) if new_tempo() (memory allocation there) fails.
   1.368 + */
   1.369 +void
   1.370 +smf_init_tempo(smf_t *smf)
   1.371 +{
   1.372 +	smf_tempo_t *tempo;
   1.373 +
   1.374 +	smf_fini_tempo(smf);
   1.375 +
   1.376 +	tempo = new_tempo(smf, 0);
   1.377 +	if (tempo == NULL)
   1.378 +		fg_error("tempo_init failed, sorry.");
   1.379 +}
   1.380 +
   1.381 +/**
   1.382 + * Returns ->time_pulses of last event on the given track, or 0, if track is empty.
   1.383 + */
   1.384 +static int
   1.385 +last_event_pulses(const smf_track_t *track)
   1.386 +{
   1.387 +	/* Get time of last event on this track. */
   1.388 +	if (track->number_of_events > 0) {
   1.389 +		smf_event_t *previous_event = smf_track_get_last_event(track);
   1.390 +		assert(previous_event);
   1.391 +		assert(previous_event->time_pulses >= 0);
   1.392 +
   1.393 +		return (previous_event->time_pulses);
   1.394 +	}
   1.395 +
   1.396 +	return (0);
   1.397 +}
   1.398 +
   1.399 +/**
   1.400 + * Adds event to the track at the time "pulses" clocks from the previous event in this track.
   1.401 + * The remaining two time fields will be computed automatically based on the third argument
   1.402 + * and current tempo map.  Note that ->delta_pulses is computed by smf.c:smf_track_add_event,
   1.403 + * not here.
   1.404 + */
   1.405 +void
   1.406 +smf_track_add_event_delta_pulses(smf_track_t *track, smf_event_t *event, int delta)
   1.407 +{
   1.408 +	assert(delta >= 0);
   1.409 +	assert(event->time_pulses == -1);
   1.410 +	assert(event->time_seconds == -1.0);
   1.411 +	assert(track->smf != NULL);
   1.412 +
   1.413 +	smf_track_add_event_pulses(track, event, last_event_pulses(track) + delta);
   1.414 +}
   1.415 +
   1.416 +/**
   1.417 + * Adds event to the track at the time "pulses" clocks from the start of song.
   1.418 + * The remaining two time fields will be computed automatically based on the third argument
   1.419 + * and current tempo map.
   1.420 + */
   1.421 +void
   1.422 +smf_track_add_event_pulses(smf_track_t *track, smf_event_t *event, int pulses)
   1.423 +{
   1.424 +	assert(pulses >= 0);
   1.425 +	assert(event->time_pulses == -1);
   1.426 +	assert(event->time_seconds == -1.0);
   1.427 +	assert(track->smf != NULL);
   1.428 +
   1.429 +	event->time_pulses = pulses;
   1.430 +	event->time_seconds = seconds_from_pulses(track->smf, pulses);
   1.431 +	smf_track_add_event(track, event);
   1.432 +}
   1.433 +
   1.434 +/**
   1.435 + * Adds event to the track at the time "seconds" seconds from the start of song.
   1.436 + * The remaining two time fields will be computed automatically based on the third argument
   1.437 + * and current tempo map.
   1.438 + */
   1.439 +void
   1.440 +smf_track_add_event_seconds(smf_track_t *track, smf_event_t *event, double seconds)
   1.441 +{
   1.442 +	assert(seconds >= 0.0);
   1.443 +	assert(event->time_pulses == -1);
   1.444 +	assert(event->time_seconds == -1.0);
   1.445 +	assert(track->smf != NULL);
   1.446 +
   1.447 +	event->time_seconds = seconds;
   1.448 +	event->time_pulses = pulses_from_seconds(track->smf, seconds);
   1.449 +	smf_track_add_event(track, event);
   1.450 +}
   1.451 +