Ardour  8.7-15-gadf511264b
SMF.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2014 David Robillard <d@drobilla.net>
3  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009 Hans Baier <hansfbaier@googlemail.com>
5  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef EVORAL_SMF_HPP
23 #define EVORAL_SMF_HPP
24 
25 #include <glibmm/threads.h>
26 #include <set>
27 
28 #include "evoral/visibility.h"
29 #include "evoral/types.h"
30 
31 struct smf_struct;
32 struct smf_track_struct;
33 struct smf_tempo_struct;
34 typedef smf_struct smf_t;
37 
38 namespace Evoral {
39 
52 public:
53  class FileError : public std::exception {
54  public:
55  FileError (std::string const & n) : _file_name (n) {}
56  ~FileError () throw () {}
57  const char* what() const throw() { return "Unknown SMF error"; }
58  std::string file_name () const { return _file_name; }
59  private:
60  std::string _file_name;
61  };
62 
63  SMF();
64  virtual ~SMF();
65 
66  static bool test(const std::string& path);
67  int open (const std::string& path, int track = 1, bool scan = true);
68  // XXX 19200 = 10 * Temporal::ticks_per_beat
69  int create(const std::string& path, int track=1, uint16_t ppqn=19200);
70  void close();
71 
72  void seek_to_start() const;
73  int seek_to_track(int track);
74 
75  int read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf, event_id_t* note_id) const;
76 
77  uint16_t num_tracks() const;
78  uint16_t ppqn() const;
79  bool is_empty() const { return _empty; }
80 
81  void begin_write();
82  void append_event_delta(uint32_t delta_t, uint32_t size, const uint8_t* buf, event_id_t note_id);
83  void end_write(std::string const &);
84 
85  void flush() {};
86 
87  double round_to_file_precision (double val) const;
88 
89  int smf_format () const;
90 
91  int num_channels () const { return _num_channels; }
92  typedef std::bitset<16> UsedChannels;
93  UsedChannels const& used_channels () const { return _used_channels; }
94  void set_used_channels (UsedChannels used) { _used_channels = used; }
95  uint64_t n_note_on_events () const { return _n_note_on_events; }
96  bool has_pgm_change () const { return _has_pgm_change; }
97 
98  void track_names (std::vector<std::string>&) const;
99  void instrument_names (std::vector<std::string>&) const;
100 
101  int num_tempos () const;
102 
103  /* This is exactly modelled on smf_tempo_t */
104  struct Tempo {
105  size_t time_pulses;
111 
112  Tempo ()
113  : time_pulses (0)
114  , microseconds_per_quarter_note (-1)
115  , numerator (-1)
116  , denominator (-1)
117  , clocks_per_click (-1)
118  , notes_per_note (-1) {}
120 
121  double tempo() const {
122  return 60.0 * (1000000.0 / (double) microseconds_per_quarter_note);
123  }
124  };
125 
126  Tempo* nth_tempo (size_t n) const;
127 
128  struct MarkerAt {
129  std::string text;
130  size_t time_pulses; /* type matches libsmf smf_event_struct.time_pulses */
131 
132  MarkerAt (std::string const & txt, size_t tp) : text (txt), time_pulses (tp) {}
133  };
134 
135  typedef std::vector<MarkerAt> Markers;
136  Markers const & markers() const { return _markers; }
137  void load_markers ();
138 
139  private:
142  bool _empty;
143 
144  mutable Glib::Threads::Mutex _smf_lock;
145 
146  mutable Markers _markers;
147 
148  protected:
153 };
154 
155 }; /* namespace Evoral */
156 
157 #endif /* EVORAL_SMF_HPP */
smf_tempo_struct smf_tempo_t
Definition: SMF.h:36
smf_struct smf_t
Definition: SMF.h:33
smf_track_struct smf_track_t
Definition: SMF.h:35
std::string file_name() const
Definition: SMF.h:58
std::string _file_name
Definition: SMF.h:60
FileError(std::string const &n)
Definition: SMF.h:55
const char * what() const
Definition: SMF.h:57
uint64_t n_note_on_events() const
Definition: SMF.h:95
void begin_write()
void end_write(std::string const &)
smf_t * _smf
Definition: SMF.h:140
uint16_t num_tracks() const
void append_event_delta(uint32_t delta_t, uint32_t size, const uint8_t *buf, event_id_t note_id)
double round_to_file_precision(double val) const
bool is_empty() const
Definition: SMF.h:79
std::bitset< 16 > UsedChannels
Definition: SMF.h:92
int _num_channels
Definition: SMF.h:151
smf_track_t * _smf_track
Definition: SMF.h:141
Glib::Threads::Mutex _smf_lock
Definition: SMF.h:144
int num_tempos() const
bool _has_pgm_change
Definition: SMF.h:150
void instrument_names(std::vector< std::string > &) const
UsedChannels const & used_channels() const
Definition: SMF.h:93
int read_event(uint32_t *delta_t, uint32_t *size, uint8_t **buf, event_id_t *note_id) const
void set_used_channels(UsedChannels used)
Definition: SMF.h:94
void load_markers()
void close()
void track_names(std::vector< std::string > &) const
UsedChannels _used_channels
Definition: SMF.h:152
int num_channels() const
Definition: SMF.h:91
bool _empty
true iff file contains(non-empty) events
Definition: SMF.h:142
static bool test(const std::string &path)
int smf_format() const
int seek_to_track(int track)
uint64_t _n_note_on_events
Definition: SMF.h:149
void seek_to_start() const
Markers const & markers() const
Definition: SMF.h:136
int create(const std::string &path, int track=1, uint16_t ppqn=19200)
Markers _markers
Definition: SMF.h:146
std::vector< MarkerAt > Markers
Definition: SMF.h:135
bool has_pgm_change() const
Definition: SMF.h:96
void flush()
Definition: SMF.h:85
uint16_t ppqn() const
virtual ~SMF()
Tempo * nth_tempo(size_t n) const
int open(const std::string &path, int track=1, bool scan=true)
#define LIBEVORAL_API
Definition: editor.h:87
int32_t event_id_t
MarkerAt(std::string const &txt, size_t tp)
Definition: SMF.h:132
size_t time_pulses
Definition: SMF.h:130
std::string text
Definition: SMF.h:129
int microseconds_per_quarter_note
Definition: SMF.h:106
int clocks_per_click
Definition: SMF.h:109
size_t time_pulses
Definition: SMF.h:105
Tempo(smf_tempo_t *)
int denominator
Definition: SMF.h:108
double tempo() const
Definition: SMF.h:121
int notes_per_note
Definition: SMF.h:110