@@ -94,6 +94,8 @@ py::object py_filter (vec<T> & self, py::callable & func, const bool inplace) {
94
94
}
95
95
96
96
97
+ #define NDARR (DTYPE, DIM ) py::ndarray<DTYPE, py::ndim<DIM>, py::c_contig, py::device::cpu>
98
+
97
99
template <typename T>
98
100
std::tuple<py::class_<T>, py::class_<vec<T>>> time_stamp_base (py::module_ &m, const std::string &name) {
99
101
typedef typename T::unit unit;
@@ -129,6 +131,10 @@ std::tuple<py::class_<T>, py::class_<vec<T>>> time_stamp_base(py::module_ &m, co
129
131
return std::make_tuple (event, vec_T);
130
132
}
131
133
134
+ void show (NDARR(float , 1 ) arg) {
135
+
136
+ }
137
+
132
138
// a template functions for binding all specializations of Note, and return it
133
139
template <typename T>
134
140
py::class_<Note<T>> bind_note_class (py::module_ &m, const std::string & name_) {
@@ -167,7 +173,17 @@ py::class_<Note<T>> bind_note_class(py::module_ &m, const std::string & name_) {
167
173
.def (" shift_velocity" , [](Note<T> &self, const int8_t offset, const bool inplace) {
168
174
if (inplace) return py::cast (self.shift_velocity_inplace (offset), py::rv_policy::reference);
169
175
else return py::cast (self.shift_velocity (offset), py::rv_policy::move);
170
- }, py::arg (" offset" ), py::arg (" inplace" )=false , " Shift the velocity by offset" );
176
+ }, py::arg (" offset" ), py::arg (" inplace" )=false , " Shift the velocity by offset" )
177
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (unit, 1 ) duration, NDARR (i8, 1 ) pitch, NDARR (i8, 1 ) velocity) {
178
+ if (time .size () != duration.size () || time .size () != pitch.size () || time .size () != velocity.size ()) {
179
+ throw std::invalid_argument (" time, duration, pitch, velocity must have the same size" );
180
+ }
181
+ auto size = time .size ();
182
+ vec<Note<T>> ans; ans.reserve (size);
183
+ for (size_t i = 0 ; i < size; ++i) {
184
+ ans.emplace_back (time (i), duration (i), pitch (i), velocity (i));
185
+ } return ans;
186
+ });
171
187
}
172
188
173
189
// bind symusic::KeySignature<T>
@@ -195,7 +211,17 @@ py::class_<symusic::KeySignature<T>> bind_key_signature_class(py::module_ &m, co
195
211
.def (py::init<unit, i8, i8>(), py::arg (" time" ), py::arg (" key" ), py::arg (" tonality" ))
196
212
.def_rw (" key" , &symusic::KeySignature<T>::key)
197
213
.def_rw (" tonality" , &symusic::KeySignature<T>::tonality)
198
- .def_prop_ro (" degree" , &symusic::KeySignature<T>::degree);
214
+ .def_prop_ro (" degree" , &symusic::KeySignature<T>::degree)
215
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (i8, 1 ) key, NDARR (i8, 1 ) tonality) {
216
+ if (time .size () != key.size () || time .size () != tonality.size ()) {
217
+ throw std::invalid_argument (" time, key, tonality must have the same size" );
218
+ }
219
+ auto size = time .size ();
220
+ vec<symusic::KeySignature<T>> ans; ans.reserve (size);
221
+ for (size_t i = 0 ; i < size; ++i) {
222
+ ans.emplace_back (time (i), key (i), tonality (i));
223
+ } return ans;
224
+ });
199
225
}
200
226
201
227
// bind symusic::TimeSignature<T>
@@ -221,7 +247,17 @@ py::class_<symusic::TimeSignature<T>> bind_time_signature_class(py::module_ &m,
221
247
return time_sig
222
248
.def (py::init<unit, u8, u8>())
223
249
.def_rw (" numerator" , &symusic::TimeSignature<T>::numerator)
224
- .def_rw (" denominator" , &symusic::TimeSignature<T>::denominator);
250
+ .def_rw (" denominator" , &symusic::TimeSignature<T>::denominator)
251
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (u8, 1 ) numerator, NDARR (u8, 1 ) denominator) {
252
+ if (time .size () != numerator.size () || time .size () != denominator.size ()) {
253
+ throw std::invalid_argument (" time, numerator, denominator must have the same size" );
254
+ }
255
+ auto size = time .size ();
256
+ vec<symusic::TimeSignature<T>> ans; ans.reserve (size);
257
+ for (size_t i = 0 ; i < size; ++i) {
258
+ ans.emplace_back (time (i), numerator (i), denominator (i));
259
+ } return ans;
260
+ });
225
261
}
226
262
227
263
// bind symusic::ControlChange<T>
@@ -248,7 +284,17 @@ py::class_<symusic::ControlChange<T>> bind_control_change_class(py::module_ &m,
248
284
return control_change
249
285
.def (py::init<unit, u8, u8>(), py::arg (" time" ), py::arg (" number" ), py::arg (" value" ))
250
286
.def_rw (" value" , &symusic::ControlChange<T>::value)
251
- .def_rw (" number" , &symusic::ControlChange<T>::number);
287
+ .def_rw (" number" , &symusic::ControlChange<T>::number)
288
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (u8, 1 ) number, NDARR (u8, 1 ) value) {
289
+ if (time .size () != number.size () || time .size () != value.size ()) {
290
+ throw std::invalid_argument (" time, number, value must have the same size" );
291
+ }
292
+ auto size = time .size ();
293
+ vec<symusic::ControlChange<T>> ans; ans.reserve (size);
294
+ for (size_t i = 0 ; i < size; ++i) {
295
+ ans.emplace_back (time (i), number (i), value (i));
296
+ } return ans;
297
+ });
252
298
}
253
299
254
300
// bind Pedal<T>
@@ -277,7 +323,17 @@ py::class_<symusic::Pedal<T>> bind_pedal_class(py::module_ &m, const std::string
277
323
return pedal
278
324
.def (py::init<unit, unit>(), py::arg (" time" ), py::arg (" duration" ))
279
325
.def_rw (" duration" , &symusic::Pedal<T>::duration)
280
- .def_prop_ro (" end" , &symusic::Pedal<T>::end);
326
+ .def_prop_ro (" end" , &symusic::Pedal<T>::end)
327
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (unit, 1 ) duration) {
328
+ if (time .size () != duration.size ()) {
329
+ throw std::invalid_argument (" time, duration must have the same size" );
330
+ }
331
+ auto size = time .size ();
332
+ vec<symusic::Pedal<T>> ans; ans.reserve (size);
333
+ for (size_t i = 0 ; i < size; ++i) {
334
+ ans.emplace_back (time (i), duration (i));
335
+ } return ans;
336
+ });
281
337
}
282
338
283
339
// bind symusic::Tempo<T>
@@ -308,7 +364,17 @@ py::class_<symusic::Tempo<T>> bind_tempo_class(py::module_ &m, const std::string
308
364
}, py::arg (" time" ), py::arg (" qpm" )=py::none (), py::arg (" mspq" )=py::none ())
309
365
.def_rw (" mspq" , &symusic::Tempo<T>::mspq, " Microseconds per quarter note" )
310
366
.def_prop_rw (" tempo" , &Tempo<T>::qpm, &Tempo<T>::set_qpm, " The same as qpm" )
311
- .def_prop_rw (" qpm" , &Tempo<T>::qpm, &Tempo<T>::set_qpm, " Quarter per minute, the same as tempo" );
367
+ .def_prop_rw (" qpm" , &Tempo<T>::qpm, &Tempo<T>::set_qpm, " Quarter per minute, the same as tempo" )
368
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (i32, 1 ) mspq) {
369
+ if (time .size () != mspq.size ()) {
370
+ throw std::invalid_argument (" time, mspq must have the same size" );
371
+ }
372
+ auto size = time .size ();
373
+ vec<symusic::Tempo<T>> ans; ans.reserve (size);
374
+ for (size_t i = 0 ; i < size; ++i) {
375
+ ans.emplace_back (time (i), mspq (i));
376
+ } return ans;
377
+ });
312
378
}
313
379
314
380
// bind symusic::PitchBend<T>
@@ -333,7 +399,17 @@ py::class_<symusic::PitchBend<T>> bind_pitch_bend_class(py::module_ &m, const st
333
399
334
400
return pitch_bend
335
401
.def (py::init<unit, i32>(), py::arg (" time" ), py::arg (" value" ))
336
- .def_rw (" value" , &symusic::PitchBend<T>::value);
402
+ .def_rw (" value" , &symusic::PitchBend<T>::value)
403
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (i32, 1 ) value) {
404
+ if (time .size () != value.size ()) {
405
+ throw std::invalid_argument (" time, value must have the same size" );
406
+ }
407
+ auto size = time .size ();
408
+ vec<symusic::PitchBend<T>> ans; ans.reserve (size);
409
+ for (size_t i = 0 ; i < size; ++i) {
410
+ ans.emplace_back (time (i), value (i));
411
+ } return ans;
412
+ });
337
413
}
338
414
339
415
// bind symusic::TextMeta<T>
@@ -351,7 +427,10 @@ py::class_<symusic::TextMeta<T>> bind_text_meta_class(py::module_ &m, const std:
351
427
352
428
return text_meta
353
429
.def (py::init<unit, std::string &>(), py::arg (" time" ), py::arg (" text" ))
354
- .def_rw (" text" , &symusic::TextMeta<T>::text);
430
+ .def_rw (" text" , &symusic::TextMeta<T>::text)
431
+ .def (" from_numpy" , [](NDARR (unit, 1 ) time , NDARR (std::string, 1 ) text) {
432
+ throw std::runtime_error (" TextMeta does not support numpy" );
433
+ });
355
434
}
356
435
357
436
template <typename T>
0 commit comments