kohacraft Lab
/
kc_YMZ294player
YMZ294 Player. modified from "Yamaguchi's YMZ294 Library" for LPC1114.
Player/Player.cpp@0:7a56bf0441ea, 2015-12-09 (annotated)
- Committer:
- kohacraft
- Date:
- Wed Dec 09 01:38:08 2015 +0000
- Revision:
- 0:7a56bf0441ea
YMZ294 Player. modified from "Yamaguchi's YMZ294 Library" for LPC1114
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kohacraft | 0:7a56bf0441ea | 1 | #include "Player.h" |
kohacraft | 0:7a56bf0441ea | 2 | |
kohacraft | 0:7a56bf0441ea | 3 | #define min(x, y) ((x) < (y) ? (x) : (y)) |
kohacraft | 0:7a56bf0441ea | 4 | |
kohacraft | 0:7a56bf0441ea | 5 | Player::Player(char *s1, char *s2, char *s3) { |
kohacraft | 0:7a56bf0441ea | 6 | MusicString ms1(s1); |
kohacraft | 0:7a56bf0441ea | 7 | MusicString ms2(s2); |
kohacraft | 0:7a56bf0441ea | 8 | MusicString ms3(s3); |
kohacraft | 0:7a56bf0441ea | 9 | layer1 = Layer(ms1); |
kohacraft | 0:7a56bf0441ea | 10 | layer2 = Layer(ms2); |
kohacraft | 0:7a56bf0441ea | 11 | layer3 = Layer(ms3); |
kohacraft | 0:7a56bf0441ea | 12 | } |
kohacraft | 0:7a56bf0441ea | 13 | |
kohacraft | 0:7a56bf0441ea | 14 | void Player::play(YMZ294& soundGen, int freq, int shape, int tempo) { |
kohacraft | 0:7a56bf0441ea | 15 | bool attenuate = (shape < 8|| shape == 9 || shape == 15); |
kohacraft | 0:7a56bf0441ea | 16 | for (int tick = 0, dur = 0;; tick += dur) { |
kohacraft | 0:7a56bf0441ea | 17 | dur = layer1.play(soundGen, CHANNEL_A, tick, attenuate); |
kohacraft | 0:7a56bf0441ea | 18 | if (!layer2.isNull()) { |
kohacraft | 0:7a56bf0441ea | 19 | int dur2 = layer2.play(soundGen, CHANNEL_B, tick, attenuate); |
kohacraft | 0:7a56bf0441ea | 20 | dur = min(dur, dur2); |
kohacraft | 0:7a56bf0441ea | 21 | } |
kohacraft | 0:7a56bf0441ea | 22 | if (!layer3.isNull()) { |
kohacraft | 0:7a56bf0441ea | 23 | int dur3 = layer3.play(soundGen, CHANNEL_C, tick, attenuate); |
kohacraft | 0:7a56bf0441ea | 24 | dur = min(dur, dur3); |
kohacraft | 0:7a56bf0441ea | 25 | } |
kohacraft | 0:7a56bf0441ea | 26 | if (dur == 0) |
kohacraft | 0:7a56bf0441ea | 27 | break; |
kohacraft | 0:7a56bf0441ea | 28 | soundGen.setEnvelope(freq, shape); |
kohacraft | 0:7a56bf0441ea | 29 | soundGen.setMixer(TONE_A, TONE_B, TONE_C); |
kohacraft | 0:7a56bf0441ea | 30 | wait(dur / 480.0 * 60.0 / tempo); |
kohacraft | 0:7a56bf0441ea | 31 | } |
kohacraft | 0:7a56bf0441ea | 32 | soundGen.setMixer(NONE); |
kohacraft | 0:7a56bf0441ea | 33 | soundGen.setVolume(CHANNEL_A, 0); |
kohacraft | 0:7a56bf0441ea | 34 | soundGen.setVolume(CHANNEL_B, 0); |
kohacraft | 0:7a56bf0441ea | 35 | soundGen.setVolume(CHANNEL_C, 0); |
kohacraft | 0:7a56bf0441ea | 36 | } |
kohacraft | 0:7a56bf0441ea | 37 | |
kohacraft | 0:7a56bf0441ea | 38 | void Player::rewind() { |
kohacraft | 0:7a56bf0441ea | 39 | layer1.rewind(); |
kohacraft | 0:7a56bf0441ea | 40 | layer2.rewind(); |
kohacraft | 0:7a56bf0441ea | 41 | layer3.rewind(); |
kohacraft | 0:7a56bf0441ea | 42 | } |