#include <vlc_charset.h>
#include <limits.h>
+#define TEMPO_MIN 20
+#define TEMPO_MAX 250 /* Beats per minute */
+
static int Open (vlc_object_t *);
static void Close (vlc_object_t *);
p_demux->pf_control = Control;
p_demux->p_sys = p_sys;
- /* SMF expresses tempo in Beats-Per-Minute, default is 120 */
- date_Init (&p_sys->pts, ppqn * 120, 60);
+ /* Default SMF tempo is 120BPM, i.e. half a second per quarter note */
+ date_Init (&p_sys->pts, ppqn * 2, 1);
date_Set (&p_sys->pts, 1);
p_sys->pulse = 0;
p_sys->ppqn = ppqn;
int HandleMeta (demux_t *p_demux, mtrk_t *tr)
{
stream_t *s = p_demux->s;
+ demux_sys_t *p_sys = p_demux->p_sys;
uint8_t *payload;
uint8_t type;
int32_t length;
case 0x51: /* Tempo */
if (length == 3)
{
- uint32_t tempo = (payload[0] << 16)
+ uint32_t uspqn = (payload[0] << 16)
| (payload[1] << 8) | payload[2];
- /* FIXME: change date */
- msg_Dbg (p_demux, "new tempo: %u", (unsigned)tempo);
+ unsigned tempo = 60 * 1000000 / (uspqn ? uspqn : 1);
+ msg_Dbg (p_demux, "tempo: %uus/qn -> %u BPM",
+ (unsigned)uspqn, tempo);
+
+ if (tempo < TEMPO_MIN)
+ {
+ msg_Warn (p_demux, "tempo too slow -> %u BPM", TEMPO_MIN);
+ tempo = TEMPO_MIN;
+ }
+ else
+ if (tempo > TEMPO_MAX)
+ {
+ msg_Warn (p_demux, "tempo too fast -> %u BPM", TEMPO_MAX);
+ tempo = TEMPO_MAX;
+ }
+ date_Change (&p_sys->pts, p_sys->ppqn * tempo, 60);
}
else
ret = -1;
break;
case 0x54: /* SMPTE offset */
+ if (length == 5)
+ msg_Warn (p_demux, "SMPTE offset not implemented");
+ else
+ ret = -1;
+ break;
+
case 0x58: /* Time signature */
+ if (length == 4)
+ ;
+ else
+ ret = -1;
+ break;
+
case 0x59: /* Key signature */
- msg_Warn (p_demux, "unimplemented SMF Meta Event type 0x%02X (%d bytes)",
- type, length);
+ if (length == 2)
+ ;
+ else
+ ret = -1;
break;
case 0x7f: /* Proprietary event */
+ msg_Dbg (p_demux, "ignored proprietary SMF Meta Event (%d bytes)",
+ length);
break;
default:
next_pulse = track->next;
}
+ mtime_t cur_tick = (date_Get (&p_sys->pts) + 9999) / 10000, last_tick;
if (next_pulse != UINT64_MAX)
- date_Increment (&p_sys->pts, next_pulse - pulse);
+ last_tick = date_Increment (&p_sys->pts, next_pulse - pulse) / 10000;
+ else
+ last_tick = cur_tick + 1;
+
+ /* MIDI Tick emulation (ping the decider every 10ms) */
+ while (cur_tick < last_tick)
+ {
+ block_t *tick = block_New (p_demux, 1);
+ if (tick == NULL)
+ break;
+
+ tick->p_buffer[0] = 0xF9;
+ tick->i_dts = tick->i_pts = cur_tick++ * 10000;
+ es_out_Send (p_demux->out, p_sys->es, tick);
+ }
+
p_sys->pulse = next_pulse;
return 1;