Skip to content

Commit ac5ef32

Browse files
committed
add pyvec
1 parent dca0bed commit ac5ef32

File tree

10 files changed

+2018
-1025
lines changed

10 files changed

+2018
-1025
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
[submodule "3rdparty/prestosynth"]
2020
path = 3rdparty/prestosynth
2121
url = https://github.com/lzqlzzq/prestosynth.git
22+
[submodule "3rdparty/pyvec"]
23+
path = 3rdparty/pyvec
24+
url = https://github.com/Yikai-Liao/pyvec.git

3rdparty/pyvec

Submodule pyvec added at 9d0edc8

CMakeLists.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ endif()
2121
add_subdirectory(3rdparty/fmt EXCLUDE_FROM_ALL)
2222
add_subdirectory(3rdparty/minimidi EXCLUDE_FROM_ALL)
2323
add_subdirectory(3rdparty/prestosynth EXCLUDE_FROM_ALL)
24+
add_subdirectory(3rdparty/pyvec EXCLUDE_FROM_ALL)
2425

2526
include_directories(include)
2627
include_directories(3rdparty/pdqsort)
@@ -36,6 +37,7 @@ add_library(symusic ${symusic_src})
3637
target_link_libraries(symusic fmt::fmt-header-only)
3738
target_link_libraries(symusic minimidi)
3839
target_link_libraries(symusic prestosynth)
40+
target_link_libraries(symusic pyvec)
3941

4042
if(BUILD_PY)
4143
message("Building python binding.")
@@ -51,13 +53,13 @@ if(BUILD_PY)
5153

5254
find_package(nanobind CONFIG REQUIRED)
5355

54-
nanobind_add_module(core py_src/core.cpp)
56+
nanobind_add_module(core NOMINSIZE py_src/core.cpp py_src/py_utils.h py_src/bind_vector_copy.h)
5557
target_link_libraries(core PRIVATE symusic)
5658

57-
install(TARGETS core LIBRARY DESTINATION symusic)
58-
# install abc2midi and midi2abc
59-
add_subdirectory(3rdparty/abcmidi)
60-
install(TARGETS abc2midi midi2abc DESTINATION symusic/bin)
59+
# install(TARGETS core LIBRARY DESTINATION symusic)
60+
# # install abc2midi and midi2abc
61+
# add_subdirectory(3rdparty/abcmidi)
62+
# install(TARGETS abc2midi midi2abc DESTINATION symusic/bin)
6163
endif()
6264

6365
if(BUILD_TEST)
@@ -69,13 +71,15 @@ if(BUILD_TEST)
6971
add_executable(synth tests/synth.cpp)
7072
add_executable(sequence tests/sequence.cpp)
7173
add_executable(adjust_time tests/adjust_time.cpp)
74+
add_executable(copy_shared tests/copy_shared_bench.cpp)
7275

7376
target_link_libraries(note_count PRIVATE symusic)
7477
target_link_libraries(dump PRIVATE symusic)
7578
target_link_libraries(io_bench PRIVATE symusic nanobench)
7679
target_link_libraries(synth PUBLIC symusic)
7780
target_link_libraries(sequence PUBLIC symusic)
7881
target_link_libraries(adjust_time PUBLIC symusic)
82+
target_link_libraries(copy_shared PUBLIC nanobench)
7983

8084
endif()
8185

include/symusic/conversion.h

+58
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,63 @@ ScoreNative<T> to_native(const Score<T>& score);
3131
template<TType T>
3232
TrackNative<T> to_native(const Track<T>& track);
3333

34+
/*
35+
* Conversion between shared and native
36+
*/
37+
38+
template<TType T>
39+
Track<T> to_shared(TrackNative<T>&& track) {
40+
Track<T> new_track{std::move(track.name), track.program, track.is_drum};
41+
new_track.notes = details::to_shared_vec(std::move(track.notes));
42+
new_track.controls = details::to_shared_vec(std::move(track.controls));
43+
new_track.pitch_bends = details::to_shared_vec(std::move(track.pitch_bends));
44+
new_track.pedals = details::to_shared_vec(std::move(track.pedals));
45+
return new_track;
46+
}
47+
48+
template<TType T>
49+
Score<T> to_shared(ScoreNative<T>&& score) {
50+
Score<T> new_score{score.ticks_per_quarter};
51+
new_score.time_signatures = details::to_shared_vec(std::move(score.time_signatures));
52+
new_score.key_signatures = details::to_shared_vec(std::move(score.key_signatures));
53+
new_score.tempos = details::to_shared_vec(std::move(score.tempos));
54+
new_score.lyrics = details::to_shared_vec(std::move(score.lyrics));
55+
new_score.markers = details::to_shared_vec(std::move(score.markers));
56+
new_score.tracks->reserve(score.tracks.size());
57+
for (auto & track : score.tracks) {
58+
shared<Track<T>> new_track = std::make_shared<Track<T>>(std::move(to_shared(std::move(track))));
59+
new_score.tracks->push_back(std::move(new_track));
60+
}
61+
return new_score;
62+
}
63+
64+
template<TType T>
65+
TrackNative<T> to_native(const Track<T>& track) {
66+
TrackNative<T> new_track{track.name, track.program, track.is_drum};
67+
new_track.notes = details::to_native_vec(track.notes);
68+
new_track.controls = details::to_native_vec(track.controls);
69+
new_track.pitch_bends = details::to_native_vec(track.pitch_bends);
70+
new_track.pedals = details::to_native_vec(track.pedals);
71+
return new_track;
72+
}
73+
74+
template<TType T>
75+
ScoreNative<T> to_native(const Score<T>& score) {
76+
ScoreNative<T> new_score{score.ticks_per_quarter};
77+
new_score.time_signatures = details::to_native_vec(score.time_signatures);
78+
new_score.key_signatures = details::to_native_vec(score.key_signatures);
79+
new_score.tempos = details::to_native_vec(score.tempos);
80+
new_score.lyrics = details::to_native_vec(score.lyrics);
81+
new_score.markers = details::to_native_vec(score.markers);
82+
new_score.tracks.reserve(score.tracks->size());
83+
for (const shared<Track<T>>& track : *score.tracks) {
84+
new_score.tracks.push_back(std::move(to_native(*track)));
85+
}
86+
return new_score;
87+
}
88+
3489
} // namespace symusic
90+
91+
92+
3593
#endif // LIBSYMUSIC_CONVERSION_H

include/symusic/event.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "symusic/mtype.h"
1111
#include "symusic/io/iodef.h"
1212
#include "symusic/time_unit.h"
13+
#include "iostream"
1314

1415
namespace symusic {
1516

@@ -42,8 +43,7 @@ namespace symusic {
4243
template<DataFormat F> \
4344
[[nodiscard]] vec<u8> dumps() const; \
4445
EVENT shift_time(unit offset) const; \
45-
EVENT& shift_time_inplace(unit offset);
46-
46+
EVENT& shift_time_inplace(unit offset); \
4747
/*
4848
* List of all the events (based on TimeStamp):
4949
* - Note(duration: unit, pitch: i8, velocity: i8)

include/symusic/score.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct Score {
141141

142142
template<typename T>
143143
struct ScoreNative {
144-
u16 ticks_per_quarter;
144+
i32 ticks_per_quarter;
145145
vec<TimeSignature<T>> time_signatures;
146146
vec<KeySignature<T>> key_signatures;
147147
vec<Tempo<T>> tempos;
@@ -151,7 +151,7 @@ struct ScoreNative {
151151

152152
ScoreNative() : ticks_per_quarter(0) {}
153153

154-
explicit ScoreNative(const u16 ticks_per_quarter) : ticks_per_quarter(ticks_per_quarter) {}
154+
explicit ScoreNative(const i32 ticks_per_quarter) : ticks_per_quarter(ticks_per_quarter) {}
155155

156156
[[nodiscard]] bool empty() const {
157157
return time_signatures.empty() && key_signatures.empty() && tempos.empty() &&

0 commit comments

Comments
 (0)