| 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net] |
|---|
| 4 | * |
|---|
| 5 | * http://www.hydrogen-music.org |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY, without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <transport/SimpleTransportMaster.h> |
|---|
| 24 | #include <transport/songhelpers.h> |
|---|
| 25 | #include <hydrogen/Song.h> |
|---|
| 26 | #include <hydrogen/TransportPosition.h> |
|---|
| 27 | |
|---|
| 28 | #include <stdint.h> // uint32_t, etc. |
|---|
| 29 | |
|---|
| 30 | // CHANGE THIS TO MATCH YOUR FILE: |
|---|
| 31 | #define THIS_NAMESPACE t_SimpleTransportMaster |
|---|
| 32 | #include "test_macros.h" |
|---|
| 33 | #include "test_utils.h" |
|---|
| 34 | |
|---|
| 35 | using namespace H2Core; |
|---|
| 36 | |
|---|
| 37 | namespace THIS_NAMESPACE |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | struct Fixture |
|---|
| 41 | { |
|---|
| 42 | Song* s; |
|---|
| 43 | SimpleTransportMaster x; |
|---|
| 44 | |
|---|
| 45 | Fixture() { |
|---|
| 46 | s = Song::load( "test/t_SimpleTransportMaster.h2song" ); |
|---|
| 47 | x.set_current_song(s); |
|---|
| 48 | } |
|---|
| 49 | ~Fixture() { |
|---|
| 50 | x.set_current_song(0); |
|---|
| 51 | delete s; |
|---|
| 52 | } |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | } // namespace THIS_NAMESPACE |
|---|
| 58 | |
|---|
| 59 | TEST_BEGIN( Fixture ); |
|---|
| 60 | |
|---|
| 61 | TEST_CASE( 010_defaults ) |
|---|
| 62 | { |
|---|
| 63 | TransportPosition pos; |
|---|
| 64 | |
|---|
| 65 | CK( x.get_state() == TransportPosition::STOPPED ); |
|---|
| 66 | CK( 0 == x.get_current_frame() ); |
|---|
| 67 | x.get_position(&pos); |
|---|
| 68 | H2TEST_VALID_POS(pos, s); |
|---|
| 69 | CK( pos.bar == 1 ); |
|---|
| 70 | CK( pos.beat == 1 ); |
|---|
| 71 | CK( pos.tick == 0 ); |
|---|
| 72 | CK( pos.bar_start_tick == 0 ); |
|---|
| 73 | CK( pos.frame == 0 ); |
|---|
| 74 | CK( pos.bbt_offset == 0 ); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void map_frame_to_bbt(uint32_t frame, uint32_t& bar, uint32_t& beat, uint32_t& tick, uint32_t& bbt_offset, |
|---|
| 78 | uint32_t& bar_start_tick, double frames_per_tick) |
|---|
| 79 | { |
|---|
| 80 | tick = floor( double(frame) / frames_per_tick ); |
|---|
| 81 | bbt_offset = frame - round(double(tick) * frames_per_tick); |
|---|
| 82 | bar = (tick / 192) + 1; |
|---|
| 83 | bar_start_tick = (bar - 1) * 192; |
|---|
| 84 | tick = tick % 192; |
|---|
| 85 | beat = (tick / 48) + 1; |
|---|
| 86 | tick = tick % 48; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | TEST_CASE( 020_start_stop ) |
|---|
| 91 | { |
|---|
| 92 | TransportPosition pos, posb; |
|---|
| 93 | x.get_position(&pos); |
|---|
| 94 | H2TEST_VALID_POS(pos, s); |
|---|
| 95 | x.processed_frames(1024); |
|---|
| 96 | x.get_position(&posb); |
|---|
| 97 | H2TEST_VALID_POS(posb, s); |
|---|
| 98 | CK( pos.frame == 0 ); |
|---|
| 99 | CK( pos.bbt_offset == 0 ); |
|---|
| 100 | CK( pos.frame == posb.frame ); |
|---|
| 101 | CK( pos.bar == posb.bar ); |
|---|
| 102 | CK( pos.beat == posb.beat ); |
|---|
| 103 | CK( pos.tick == posb.tick ); |
|---|
| 104 | CK( pos.bbt_offset == posb.bbt_offset ); |
|---|
| 105 | |
|---|
| 106 | CK( pos.frame_rate == 48000.0 ); |
|---|
| 107 | uint32_t frame, delta = 1237; |
|---|
| 108 | uint32_t tot_frames = pos.frames_per_tick() * 192 * 8; |
|---|
| 109 | uint32_t tpbar = 192; // ticks per bar |
|---|
| 110 | uint32_t bar, beat, tick, bbt_offset, __bar_start_tick; |
|---|
| 111 | uint32_t fpt, fpb; // frames per tick, frames per bar |
|---|
| 112 | double tickdrift; |
|---|
| 113 | |
|---|
| 114 | x.start(); |
|---|
| 115 | for( frame = 0 ; frame <= tot_frames ; frame += delta ) { |
|---|
| 116 | x.get_position(&pos); |
|---|
| 117 | H2TEST_VALID_POS(pos, s); |
|---|
| 118 | |
|---|
| 119 | CK( pos.beats_per_minute == 100.0 ); |
|---|
| 120 | map_frame_to_bbt(frame, bar, beat, tick, bbt_offset, __bar_start_tick, pos.frames_per_tick()); |
|---|
| 121 | |
|---|
| 122 | BOOST_MESSAGE("pos B:b.t.o @F = " << pos.bar << ":" << pos.beat << "." |
|---|
| 123 | << pos.tick << "." << pos.bbt_offset << " @" << pos.frame); |
|---|
| 124 | BOOST_MESSAGE("cal B:b.t.o @F = " << bar << ":" << beat << "." |
|---|
| 125 | << tick << "." << bbt_offset << " @" << frame); |
|---|
| 126 | CK( pos.frame == frame ); |
|---|
| 127 | CK( pos.bar == bar ); |
|---|
| 128 | CK( pos.beat == beat ); |
|---|
| 129 | tickdrift = fabs(double(pos.tick) - double(tick)); |
|---|
| 130 | if( tickdrift > (48.0/2.0) ) { |
|---|
| 131 | tickdrift -= pos.frames_per_tick(); |
|---|
| 132 | } |
|---|
| 133 | CK( fabs(tickdrift) < 1.5 ); |
|---|
| 134 | // This drifts to much to check: |
|---|
| 135 | // CK( pos.bbt_offset == bbt_offset ); |
|---|
| 136 | CK( pos.bar_start_tick == __bar_start_tick ); |
|---|
| 137 | |
|---|
| 138 | x.processed_frames(delta); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | TEST_END() |
|---|