-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.hpp
109 lines (99 loc) · 3.3 KB
/
note.hpp
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
/*
* aige::SingingRivuleProject
* file:note.hpp
* GNU AFFERO GENERAL PUBLIC LICENSE
* Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#ifndef NOTE_HPP
#define NOTE_HPP
#include <vector>
#include <tuple>
#include <string>
namespace vmic{
class notes{
public:
std::vector<std::tuple<int,int,int> > note;
int notes_index;
notes(){
notes_index = 0;
}
void load(int sampleRate,const std::string & path = "./notes.txt"){
printf("load notes\n");
FILE * fp = fopen(path.c_str(),"r");
char buf[4096];
float time_start,time_dur;
int pitch;
if(fp){
note.clear();
fgets(buf,sizeof(buf),fp);
int notes_num = atoi(buf);
if(notes_num>0){
printf("notes num:%d\n",notes_num);
for(int i=0;(i<notes_num && !feof(fp));++i){
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),fp);
if(sscanf(buf,"%f %f %d",&time_start,&time_dur,&pitch)>=3){
int start = time_start*sampleRate/2;
int dur = time_dur*sampleRate/2;
int targetPitch = (440 * 8192 * pow(2.0 , (pitch-57.0)/12.0 ))/sampleRate;
note.push_back(std::make_tuple(start,dur,targetPitch));
}
}
}
fclose(fp);
}
}
int inArea(const std::tuple<int,int,int> & n,int t){
if(t>=std::get<0>(n)){
if(t<(std::get<0>(n)+std::get<1>(n))){
return 0;
}else{
return 1;
}
}else{
return -1;
}
}
int getNoteIndex(int time){
int status;
if(note.empty()){
return -1;
}else{
if(notes_index>=(int)note.size()){
notes_index = note.size()-1;
}else if(notes_index<0){
notes_index = 0;
}
status = inArea(note[notes_index],time);//检查当前位置
if(status==0){
return notes_index;
}else if(status==1){
for( ; notes_index<(int)note.size() ; ++notes_index){
if(inArea(note[notes_index],time)==0){
return notes_index;
}
}
}else if(status==-1){
for( ; notes_index>=0 ; --notes_index){
if(inArea(note[notes_index],time)==0){
return notes_index;
}
}
}
}
return -1;
}
int getNote(int time){
int id = getNoteIndex(time);
try{
if(id>=0){
return std::get<2>(note.at(id));
}
}catch(...){}
return 0;
}
};
}
#endif // NOTE_HPP