-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusic.cpp
More file actions
126 lines (108 loc) · 2.27 KB
/
Copy pathMusic.cpp
File metadata and controls
126 lines (108 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Music.cpp
*
* Created: 2018-06-26 오전 6:36:52
* Modified 2018-11-19 for Attiny 4313
* Author: Cakeng (PARK JONG SEOK)
*
* NO LICENCE INCLUDED
* Contact cakeng@naver.com to
* use, modify, or share the software for any purpose
* other than personal use.
*
*/
#include "Music.h"
void MusicObj::dataRead(uint16_t data) //36 on MIDI is O2.
{
musicTime = (data>>9)&(0b01111111);
noteBuffer = (o2notebox[(((data>>2)&(0b01111111)))%12]>>((int8_t)(((data>>2)&(0b01111111))/12)-3));
channelVolume = data&0b00000011;
TCNT1 = 0;
ICR1 = noteBuffer;
OCR1A = noteBuffer*3/4*channelVolume/8;
}
void MusicObj::musicOff()
{
onFlag = false;
musicPosition = 0;
musicDown();
}
void MusicObj::musicDown()
{
TCCR1A = 0;
TCCR1B = 0;
ICR1= 0;
DDRB &= ~(1<<DDB3);
PORTB &= ~(1<<PORTB3);
}
void MusicObj::musicOut()
{
TCCR1A = controlAOnSettings;
TCCR1B = controlBOnSettings;
DDRB |= (1<<DDB3);
}
MusicObj::MusicObj(uint16_t tickFreq) //Ticks to reach 1 second.
{
//Clear Output on Compare match, Fast PWM, Top Input Capture Register
//PWM Frequency 250000/ICR1 Hz.
controlAOnSettings = (1<<COM1A1)|(1<<WGM11);
controlBOnSettings = (1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10);
musicPosition = 0;
musicTime = 0;
noteBuffer = 0;
musicTicks = 0;
channelVolume = 0;
o2notebox[0] = 3822;
o2notebox[1] = 3607;
o2notebox[2] = 3405;
o2notebox[3] = 3214;
o2notebox[4] = 3033;
o2notebox[5] = 2863;
o2notebox[6] = 2702;
o2notebox[7] = 2551;
o2notebox[8] = 2407;
o2notebox[9] = 2272;
o2notebox[10] = 2145;
o2notebox[11] = 2024;
musicTimeConst = (((uint32_t)tickFreq)*8/100); //80ms
sheetLength = sizeof(sheet0)/2;
TCCR1A = 0;
TCCR1B = 0;
ICR1 = 0;
OCR1A = 0;
onFlag = false;
musicOff();
}
void MusicObj::musicFunction()
{
if (onFlag)
{
if (musicTicks > musicTime*musicTimeConst)
{
musicTicks = 0;
musicPosition++;
if (musicPosition == sheetLength)
{
musicPosition = 0;
}
uint16_t buf = pgm_read_word(sheet0+musicPosition);
dataRead(buf);
if (channelVolume != 0)
{
musicOut();
}
else
{
musicDown();
}
}
}
}
void MusicObj::autoRoutine()
{
musicTicks++;
if(musicTicks & 0x8000)
{
musicTicks = 0;
}
}