root/trunk/libjdkmidi/trunk/include/jdkmidi/smpte.h

Revision 552, 7.5 kB (checked in by jeffk@…, 8 months ago)

formatting fixed

Line 
1/*
2 *  libjdkmidi-2004 C++ Class Library for MIDI
3 *
4 *  Copyright (C) 2004  J.D. Koftinoff Software, Ltd.
5 *  www.jdkoftinoff.com
6 *  jeffk@jdkoftinoff.com
7 *
8 *  *** RELEASED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) April 27, 2004 ***
9 *
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation; either version 2 of the License, or
13 *  (at your option) any later version.
14 *
15 *  This program is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with this program; if not, write to the Free Software
22 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23*/
24/*
25** Copyright 1986 to 1998 By J.D. Koftinoff Software, Ltd.
26**
27** All rights reserved.
28**
29** No one may duplicate this source code in any form for any reason
30** without the written permission given by J.D. Koftinoff Software, Ltd.
31**
32*/
33
34#ifndef JDKMIDI_SMPTE_H
35#define JDKMIDI_SMPTE_H
36
37namespace jdkmidi
38{
39
40  enum SMPTE_RATE
41  {
42    SMPTE_RATE_24=0,
43    SMPTE_RATE_25,
44    SMPTE_RATE_2997,
45    SMPTE_RATE_2997DF,
46    SMPTE_RATE_30,
47    SMPTE_RATE_30DF
48  };
49 
50 
51 
52  enum SAMPLE_RATE
53  {
54    SAMPLE_32000=0,
55    SAMPLE_44056,
56    SAMPLE_44100,
57    SAMPLE_47952,
58    SAMPLE_48000,
59    SAMPLE_48048
60  };
61 
62 
63 
64 
65//
66// MDGetSMPTERateFrequency() converts the SMPTE_RATE enum to a double frequency.
67//
68
69  inline double GetSMPTERateFrequency ( SMPTE_RATE r )
70  {
71    extern const double smpte_smpte_rates[];
72   
73    return smpte_smpte_rates[ ( int ) r];
74  }
75 
76 
77//
78// MDGetSMPTERateFrequencyLong() convert the SMPTE_RATE enum to an int
79// frequency times 100
80//
81
82  inline long GetSMPTERateFrequencyLong ( SMPTE_RATE r )
83  {
84    extern const double smpte_smpte_rates_long[];
85   
86    return ( long ) smpte_smpte_rates_long[ ( int ) r];
87  }
88 
89 
90//
91// GetSampleRateFrequency() convert the SAMPLE_RATE enum to a double frequency
92//
93
94
95  inline  double  GetSampleRateFrequency ( SAMPLE_RATE r )
96  {
97    extern const double smpte_sample_rates[];
98   
99    return smpte_sample_rates[ ( int ) r];
100  }
101 
102 
103//
104// MDGetSampleRateFrequencyLong() converts the SAMPLE_RATE enum to a long
105// frequency times 10
106//
107
108
109  inline long GetSampleRateFrequencyLong ( SAMPLE_RATE r )
110  {
111    // return the sample rate as a long word of the frequency times 10.
112   
113    extern const long smpte_sample_rates_long[];
114    return smpte_sample_rates_long[ ( int ) r];
115  }
116 
117 
118 
119 
120 
121 
122  class  SMPTE
123  {
124    public:
125      SMPTE (
126        SMPTE_RATE smpte_rate=SMPTE_RATE_30,
127        SAMPLE_RATE sample_rate=SAMPLE_48000
128      );
129     
130      SMPTE (
131        const SMPTE & s
132      );
133     
134      void SetSMPTERate ( SMPTE_RATE r )
135      {
136        smpte_rate=r;
137        sample_number_dirty=true;
138      }
139      SMPTE_RATE GetSMPTERate()
140      {
141        return smpte_rate;
142      }
143     
144      void SetSampleRate ( SAMPLE_RATE r )
145      {
146        sample_rate=r;
147        sample_number_dirty=true;
148      }
149      SAMPLE_RATE GetSampleRate()
150      {
151        return sample_rate;
152      }
153     
154      void SetSampleNumber ( ulong n )
155      {
156        sample_number=n;
157        SampleToTime();
158      }
159      ulong GetSampleNumber()
160      {
161        if ( sample_number_dirty ) TimeToSample();
162        return sample_number;
163      }
164     
165      void SetTime ( uchar h, uchar m, uchar s, uchar f=0, uchar sf=0 )
166      {
167        hours=h;
168        minutes=m;
169        seconds=s;
170        frames=f;
171        sub_frames=sf;
172        sample_number_dirty=true;
173      }
174     
175      uchar GetHours()
176      {
177        return hours;
178      }
179      uchar GetMinutes()
180      {
181        return minutes;
182      }
183      uchar GetSeconds()
184      {
185        return seconds;
186      }
187      uchar  GetFrames()
188      {
189        return frames;
190      }
191      uchar GetSubFrames()
192      {
193        return sub_frames;
194      }
195     
196      void SetHours ( uchar h )
197      {
198        hours=h;
199        sample_number_dirty=true;
200      }
201      void SetMinutes ( uchar m )
202      {
203        minutes=m;
204        sample_number_dirty=true;
205      }
206      void SetSeconds ( uchar s )
207      {
208        seconds=s;
209        sample_number_dirty=true;
210      }
211      void SetFrames ( uchar f )
212      {
213        frames=f;
214        sample_number_dirty=true;
215      }
216      void SetSubFrames ( uchar sf )
217      {
218        sub_frames=sf;
219        sample_number_dirty=true;
220      }
221     
222      void AddHours ( char h );
223      void AddMinutes ( char m );
224      void AddSeconds ( char s );
225      void AddFrames ( char f );
226      void AddSubFrames ( char sf );
227      void AddSamples ( long n )
228      {
229        sample_number=GetSampleNumber() +n;
230        SampleToTime();
231      }
232     
233      void IncHours()
234      {
235        AddHours ( 1 );
236      }
237      void IncMinutes()
238      {
239        AddMinutes ( 1 );
240      }
241      void IncSeconds()
242      {
243        AddSeconds ( 1 );
244      }
245      void IncFrames()
246      {
247        AddFrames ( 1 );
248      }
249      void IncSubFrames()
250      {
251        AddSubFrames ( 1 );
252      }
253      void IncSamples()
254      {
255        AddSamples ( 1 );
256      }
257     
258      void DecHours()
259      {
260        AddHours ( -1 );
261      }
262      void DecMinutes()
263      {
264        AddMinutes ( -1 );
265      }
266      void DecSeconds()
267      {
268        AddSeconds ( -1 );
269      }
270      void DecFrames()
271      {
272        AddFrames ( -1 );
273      }
274      void DecSubFrames()
275      {
276        AddSubFrames ( -1 );
277      }
278      void DecSamples()
279      {
280        AddSamples ( -1 );
281      }
282     
283     
284     
285      const SMPTE & operator =  ( const SMPTE & s )
286      {
287        Copy ( s );
288        return *this;
289      }
290      bool operator == ( SMPTE & s )
291      {
292        return Compare ( s ) ==0;
293      }
294      bool operator != ( SMPTE & s )
295      {
296        return Compare ( s ) !=0;
297      }
298      bool operator <  ( SMPTE & s )
299      {
300        return Compare ( s ) <0;
301      }
302      bool operator >  ( SMPTE & s )
303      {
304        return Compare ( s ) >0;
305      }
306      bool operator <= ( SMPTE & s )
307      {
308        return Compare ( s ) <=0;
309      }
310      bool operator >= ( SMPTE & s )
311      {
312        return Compare ( s ) >=0;
313      }
314     
315      const SMPTE & operator += ( SMPTE & s )
316      {
317        Add ( s );
318        return *this;
319      }
320      const SMPTE & operator -= ( SMPTE & s )
321      {
322        Subtract ( s );
323        return *this;
324      }
325     
326    protected:
327      void SampleToTime();
328      void TimeToSample();
329     
330      void Copy ( const SMPTE & s );
331      int Compare ( SMPTE & s );
332      void Add ( SMPTE & s );
333      void Subtract ( SMPTE & s );
334     
335      long GetSampleRateLong()
336      {
337        return GetSampleRateFrequencyLong ( sample_rate );
338      }
339     
340      int GetSMPTERateLong()
341      {
342        return GetSMPTERateFrequencyLong ( smpte_rate );
343      }
344     
345    private:
346      SMPTE_RATE  smpte_rate;
347      SAMPLE_RATE sample_rate;
348      ulong  sample_number;
349     
350      uchar  hours;
351      uchar  minutes;
352      uchar  seconds;
353      uchar  frames;
354      uchar  sub_frames;
355      uchar  sample_number_dirty;
356     
357     
358      friend SMPTE operator + ( SMPTE a, SMPTE b );
359      friend SMPTE operator - ( SMPTE a, SMPTE b );
360  };
361 
362  inline SMPTE operator + ( SMPTE a, SMPTE b )
363  {
364    SMPTE c ( a );
365   
366    c+=b;
367    return c;
368  }
369 
370  inline SMPTE operator - ( SMPTE a, SMPTE b )
371  {
372    SMPTE c ( a );
373   
374    c-=b;
375    return c;
376  }
377 
378}
379
380#endif
381
Note: See TracBrowser for help on using the browser.