OSDN Git Service

qq
[shooting3/shootinggame.git] / ShootingGame / Sequencer.cpp
1 #include "pch.h"
2 #include "Sequencer.h"
3
4 namespace sf {
5
6   struct Sequencer::impl 
7   {
8     impl(Synthesizer& synth,WAVEFORMATEXTENSIBLE& format) 
9       : synth_(synth), format_(format) ,tempo_(120.0f),timebase_(480.0f),meter_(4.0f),meterDiv_(4.0f),
10       currentTime_(0),status_(Status::Stop)
11     {
12       Update();
13       for(int i = 0,end = synth_.Voices();i < end;++i)
14       {
15         gateBuffer_.push_back(0);
16         currentIndexs_.push_back(0);
17         SequenceTracks_.push_back(SequenceDatasType());
18       }
19     }
20
21     void Reset()
22     {
23       for(int i = 0,end = synth_.Voices();i < end;++i)
24       {
25         gateBuffer_.at(i) = 0;
26         currentIndexs_.at(i) = 0;
27       }
28     }
29     void Update()
30     {
31       // stepDeltaTime_ = (int64_t)((((double)meter_ * (double)tempo_ * (double)format_.Format.nSamplesPerSec) / ((double)meterDiv_ * 60.0 * (double)timebase_ )) * (double)(0x100000000));
32       stepDeltaTime_ = (int64_t)((  ((double)tempo_ * (double)timebase_ ) / (60.0  * (double)format_.Format.nSamplesPerSec)) * (double)(0x100000000));
33     }
34
35
36
37     void Process()
38     {
39       if(status_ == Status::Play){
40         currentTime_ += stepDeltaTime_;
41         int currentTime = currentTime_ >> 32;
42         for(int i  = 0,end = SequenceTracks_.size();i < end;++i)
43         {
44           SequenceDatasType& track(SequenceTracks_[i]);
45           if(!track.empty() && track.size() > currentIndexs_[i]) {
46             if(currentTime >= track[currentIndexs_[i]].step) 
47             {
48               do
49               { 
50                 SequenceData& d(SequenceTracks_[i][currentIndexs_[i]]);
51                 synth_.NoteOn(i,d.pitch,d.velocity);
52                 gateBuffer_[i] = (int64_t)d.gateTime << 32;
53                 currentIndexs_[i] = currentIndexs_[i] + 1;
54                 if(currentIndexs_[i] >= track.size())
55                 {
56                   break;
57                 }
58               } while(currentTime >= SequenceTracks_[i][currentIndexs_[i]].step);
59             }
60           }
61           // \83Q\81[\83g\83^\83C\83\80\82Ì\8f\88\97\9d
62           if(gateBuffer_[i] > 0)
63           {
64             gateBuffer_[i] -= stepDeltaTime_;
65             if(gateBuffer_[i] <= 0)
66             {
67               gateBuffer_[i] = 0;
68               synth_.NoteOff(i);
69             }
70           }
71         }
72       }
73     }
74
75     float TimeBase() const {return timebase_;}
76     void TimeBase(float v) {timebase_ = v;Update();}
77
78     float Tempo() const {return tempo_;}
79     void Tempo(float v) {tempo_ = v;Update();}
80
81     std::vector<SequenceDatasType>& SequenceTracks() {return SequenceTracks_;}
82
83     void Play()
84     {
85       status_ = Status::Play;
86     }
87
88     void Pause()
89     {
90       status_ = Status::Pause;
91     }
92
93     void Stop()
94     {
95       status_ = Status::Stop;
96       Reset();
97     }
98
99   private:
100
101     WAVEFORMATEXTENSIBLE& format_;
102     float tempo_;
103     float timebase_;
104     int meter_;
105     int meterDiv_;
106     int64_t stepDeltaTime_;
107     int64_t currentTime_;
108     std::vector<int> currentIndexs_;
109     std::vector<int64_t> gateBuffer_;
110     SequenceTracksType SequenceTracks_;
111     Synthesizer& synth_;
112     Status status_;
113   };
114
115   Sequencer::Sequencer(Synthesizer& synth,WAVEFORMATEXTENSIBLE& format) : impl_(new impl(synth,format))
116   {
117
118   }
119
120   Sequencer::~Sequencer()
121   {
122
123   }
124
125   float Sequencer::TimeBase() const {return impl_->TimeBase();}
126   void Sequencer::TimeBase(float v) {impl_->TimeBase(v);}
127
128   float Sequencer::Tempo() const {return impl_->Tempo();}
129   void Sequencer::Tempo(float v) {impl_->Tempo(v);}
130
131   void Sequencer::Process() {impl_->Process();}
132   void Sequencer::Play()
133   {
134     impl_->Play();
135   }
136
137   void Sequencer::Pause()
138   {
139     impl_->Pause();
140   }
141
142   void Sequencer::Stop()
143   {
144     impl_->Stop();
145   }
146
147   SequenceTracksType& Sequencer::SequenceTracks() {return impl_->SequenceTracks();}
148
149
150
151 }