Skip to content

Commit

Permalink
Merge pull request #68 from shiguredo/feature/nanobind-v2.0.0
Browse files Browse the repository at this point in the history
Feature/nanobind v2.0.0
  • Loading branch information
voluntas authored May 30, 2024
2 parents 785d24b + 37da844 commit df7a3f7
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 88 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"editor.formatOnSave": true
},
"files.associations": {
"*.cs": "csharp",
"CMakeLists.txt": "cmake",
"algorithm": "cpp",
"__bits": "cpp",
"__config": "cpp",
Expand Down Expand Up @@ -119,7 +121,9 @@
"strstream": "cpp",
"typeindex": "cpp",
"source_location": "cpp",
"__memory": "cpp"
"__memory": "cpp",
"compare": "cpp",
"concepts": "cpp"
},
"C_Cpp.errorSquiggles": "disabled",
// nanobind 周りでエラーが消えないので全部消す
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ build-backend = "setuptools.build_meta"

[tool.rye]
dev-dependencies = [
"nanobind~=1.9.2",
"nanobind~=2.0.0",
"setuptools>=69.2",
"build~=1.1.1",
"wheel~=0.43.0",
Expand Down
5 changes: 2 additions & 3 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# features: []
# all-features: false
# with-sources: false
# generate-hashes: false

-e file:.
auditwheel==6.0.0
Expand All @@ -17,7 +16,7 @@ importlib-metadata==7.1.0
# via build
iniconfig==2.0.0
# via pytest
nanobind==1.9.2
nanobind==2.0.0
packaging==24.0
# via auditwheel
# via build
Expand All @@ -30,10 +29,10 @@ pyproject-hooks==1.1.0
# via build
pytest==8.2.1
ruff==0.4.5
setuptools==70.0.0
tomli==2.0.1
# via build
# via pytest
wheel==0.43.0
zipp==3.19.0
# via importlib-metadata
setuptools==70.0.0
1 change: 0 additions & 1 deletion requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
# features: []
# all-features: false
# with-sources: false
# generate-hashes: false

-e file:.
3 changes: 2 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import hashlib
import multiprocessing
import os
import platform
import shutil
Expand Down Expand Up @@ -763,7 +764,7 @@ def main():
mkdir_p(sora_build_dir)
with cd(sora_build_dir):
cmd(["cmake", BASE_DIR, *cmake_args])
cmd(["cmake", "--build", ".", "--config", configuration])
cmd(["cmake", "--build", ".", "--config", configuration, f"-j{multiprocessing.cpu_count()}"])

for file in os.listdir(sora_src_dir):
if file.startswith("sora_sdk_ext.") and (
Expand Down
6 changes: 3 additions & 3 deletions src/sora_audio_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ void SoraAudioSinkImpl::AppendData(const int16_t* audio_data,

if (on_data_) {
size_t shape[2] = {number_of_frames, number_of_channels_};
auto data = nb::ndarray<nb::numpy, int16_t, nb::shape<nb::any, nb::any>>(
(void*)audio_data, 2, shape);
auto data = nb::ndarray<nb::numpy, int16_t, nb::shape<-1, -1>>(
(void*)audio_data, 2, shape, nb::handle());
/* まだ使ったことながない。現状 Python 側で on_frame と同じ感覚でコールバックの外に値を持ち出すと落ちるはず。 */
on_data_(data);
}
Expand Down Expand Up @@ -177,7 +177,7 @@ nb::tuple SoraAudioSinkImpl::Read(size_t frames, float timeout) {
});

size_t shape[2] = {num_of_samples / number_of_channels_, number_of_channels_};
auto output = nb::ndarray<nb::numpy, int16_t, nb::shape<nb::any, nb::any>>(
auto output = nb::ndarray<nb::numpy, int16_t, nb::shape<-1, -1>>(
(int16_t*)output_data, 2, shape, deleter);
return nb::make_tuple(true, output);
}
3 changes: 1 addition & 2 deletions src/sora_audio_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class SoraAudioSinkImpl : public webrtc::AudioTrackSinkInterface,
* 実装上の留意点:コールバックと Read 関数の共存はパフォーマンスや使い方の面で難しいことが判明したので、
* on_data_, on_format_ ともに廃止予定です。
*/
std::function<void(
nb::ndarray<nb::numpy, int16_t, nb::shape<nb::any, nb::any>>)>
std::function<void(nb::ndarray<nb::numpy, int16_t, nb::shape<-1, -1>>)>
on_data_;
std::function<void(int, size_t)> on_format_;

Expand Down
16 changes: 7 additions & 9 deletions src/sora_audio_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,20 @@ void SoraAudioSource::OnData(const int16_t* data, size_t samples_per_channel) {
source_->OnData(data, samples_per_channel, absl::nullopt);
}

void SoraAudioSource::OnData(nb::ndarray<int16_t,
nb::shape<nb::any, nb::any>,
nb::c_contig,
nb::device::cpu> ndarray,
double timestamp) {
void SoraAudioSource::OnData(
nb::ndarray<int16_t, nb::shape<-1, -1>, nb::c_contig, nb::device::cpu>
ndarray,
double timestamp) {
if (!track_) {
return;
}
source_->OnData(ndarray.data(), ndarray.shape(0),
(int64_t)(timestamp * 1000));
}

void SoraAudioSource::OnData(nb::ndarray<int16_t,
nb::shape<nb::any, nb::any>,
nb::c_contig,
nb::device::cpu> ndarray) {
void SoraAudioSource::OnData(
nb::ndarray<int16_t, nb::shape<-1, -1>, nb::c_contig, nb::device::cpu>
ndarray) {
if (!track_) {
return;
}
Expand Down
16 changes: 7 additions & 9 deletions src/sora_audio_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,20 @@ class SoraAudioSource : public SoraTrackInterface {
* @param ndarray NumPy の配列 numpy.ndarray で チャンネルごとのサンプル数 x チャンネル数 になっている音声データ
* @param timestamp Python の time.time() で取得できるエポック秒で表されるフレームのタイムスタンプ
*/
void OnData(nb::ndarray<int16_t,
nb::shape<nb::any, nb::any>,
nb::c_contig,
nb::device::cpu> ndarray,
double timestamp);
void OnData(
nb::ndarray<int16_t, nb::shape<-1, -1>, nb::c_contig, nb::device::cpu>
ndarray,
double timestamp);
/**
* Sora に送る音声データを渡します。
*
* タイムスタンプは先に受け取ったデータと連続になっていると想定してサンプル数から自動生成します。
*
* @param ndarray NumPy の配列 numpy.ndarray で チャンネルごとのサンプル数 x チャンネル数 になっている音声データ
*/
void OnData(nb::ndarray<int16_t,
nb::shape<nb::any, nb::any>,
nb::c_contig,
nb::device::cpu> ndarray);
void OnData(
nb::ndarray<int16_t, nb::shape<-1, -1>, nb::c_contig, nb::device::cpu>
ndarray);

private:
rtc::scoped_refptr<SoraAudioSourceInterface> source_;
Expand Down
8 changes: 4 additions & 4 deletions src/sora_audio_stream_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ SoraAudioFrame::SoraAudioFrame(
absolute_capture_timestamp_ms));
}

nb::ndarray<nb::numpy, int16_t, nb::shape<nb::any, nb::any>>
SoraAudioFrame::Data() const {
nb::ndarray<nb::numpy, int16_t, nb::shape<-1, -1>> SoraAudioFrame::Data()
const {
// Data はまだ vector の時は返せてない
size_t shape[2] = {static_cast<size_t>(samples_per_channel()),
static_cast<size_t>(num_channels())};
return nb::ndarray<nb::numpy, int16_t, nb::shape<nb::any, nb::any>>(
(int16_t*)RawData(), 2, shape);
return nb::ndarray<nb::numpy, int16_t, nb::shape<-1, -1>>(
(int16_t*)RawData(), 2, shape, nb::handle());
}

const int16_t* SoraAudioFrame::RawData() const {
Expand Down
2 changes: 1 addition & 1 deletion src/sora_audio_stream_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class SoraAudioFrame {
*
* @return NumPy の配列 numpy.ndarray で サンプル数 x チャンネル数 になっている音声データ
*/
nb::ndarray<nb::numpy, int16_t, nb::shape<nb::any, nb::any>> Data() const;
nb::ndarray<nb::numpy, int16_t, nb::shape<-1, -1>> Data() const;
/**
* SoraAudioFrame 内の音声データへの直接参照を返します。
*
Expand Down
37 changes: 17 additions & 20 deletions src/sora_sdk_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,32 @@ NB_MODULE(sora_sdk_ext, m) {
nb::overload_cast<const int16_t*, size_t>(&SoraAudioSource::OnData),
"data"_a, "samples_per_channel"_a)
.def("on_data",
nb::overload_cast<nb::ndarray<int16_t, nb::shape<nb::any, nb::any>,
nb::overload_cast<nb::ndarray<int16_t, nb::shape<-1, -1>,
nb::c_contig, nb::device::cpu>,
double>(&SoraAudioSource::OnData),
"ndarray"_a, "timestamp"_a)
.def("on_data",
nb::overload_cast<nb::ndarray<int16_t, nb::shape<nb::any, nb::any>,
nb::overload_cast<nb::ndarray<int16_t, nb::shape<-1, -1>,
nb::c_contig, nb::device::cpu>>(
&SoraAudioSource::OnData),
"ndarray"_a);

nb::class_<SoraVideoSource, SoraTrackInterface>(m, "SoraVideoSource")
.def(
"on_captured",
nb::overload_cast<nb::ndarray<uint8_t, nb::shape<nb::any, nb::any, 3>,
nb::c_contig, nb::device::cpu>>(
&SoraVideoSource::OnCaptured),
"ndarray"_a)
.def(
"on_captured",
nb::overload_cast<nb::ndarray<uint8_t, nb::shape<nb::any, nb::any, 3>,
nb::c_contig, nb::device::cpu>,
double>(&SoraVideoSource::OnCaptured),
"ndarray"_a, "timestamp"_a)
.def(
"on_captured",
nb::overload_cast<nb::ndarray<uint8_t, nb::shape<nb::any, nb::any, 3>,
nb::c_contig, nb::device::cpu>,
int64_t>(&SoraVideoSource::OnCaptured),
"ndarray"_a, "timestamp_us"_a);
.def("on_captured",
nb::overload_cast<nb::ndarray<uint8_t, nb::shape<-1, -1, 3>,
nb::c_contig, nb::device::cpu>>(
&SoraVideoSource::OnCaptured),
"ndarray"_a)
.def("on_captured",
nb::overload_cast<nb::ndarray<uint8_t, nb::shape<-1, -1, 3>,
nb::c_contig, nb::device::cpu>,
double>(&SoraVideoSource::OnCaptured),
"ndarray"_a, "timestamp"_a)
.def("on_captured",
nb::overload_cast<nb::ndarray<uint8_t, nb::shape<-1, -1, 3>,
nb::c_contig, nb::device::cpu>,
int64_t>(&SoraVideoSource::OnCaptured),
"ndarray"_a, "timestamp_us"_a);

nb::class_<SoraAudioSinkImpl>(m, "SoraAudioSinkImpl",
nb::type_slots(audio_sink_slots))
Expand Down
7 changes: 3 additions & 4 deletions src/sora_video_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ SoraVideoFrame::SoraVideoFrame(
argb_data_.get(), width_ * 3, width_, height_, libyuv::FOURCC_24BG);
}

nb::ndarray<nb::numpy, uint8_t, nb::shape<nb::any, nb::any, 3>>
SoraVideoFrame::Data() {
nb::ndarray<nb::numpy, uint8_t, nb::shape<-1, -1, 3>> SoraVideoFrame::Data() {
size_t shape[3] = {static_cast<size_t>(height_), static_cast<size_t>(width_),
3};
return nb::ndarray<nb::numpy, uint8_t, nb::shape<nb::any, nb::any, 3>>(
argb_data_.get(), 3, shape);
return nb::ndarray<nb::numpy, uint8_t, nb::shape<-1, -1, 3>>(
argb_data_.get(), 3, shape, nb::handle());
}

SoraVideoSinkImpl::SoraVideoSinkImpl(SoraTrackInterface* track)
Expand Down
2 changes: 1 addition & 1 deletion src/sora_video_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SoraVideoFrame {
*
* @return NumPy の配列 numpy.ndarray で H x W x BGR になっているフレームデータ
*/
nb::ndarray<nb::numpy, uint8_t, nb::shape<nb::any, nb::any, 3>> Data();
nb::ndarray<nb::numpy, uint8_t, nb::shape<-1, -1, 3>> Data();

private:
// width や height は ndarray に情報として含まれるため、これらを別で返す関数は不要
Expand Down
25 changes: 11 additions & 14 deletions src/sora_video_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,23 @@ void SoraVideoSource::PublisherDisposed() {
Disposed();
}

void SoraVideoSource::OnCaptured(nb::ndarray<uint8_t,
nb::shape<nb::any, nb::any, 3>,
nb::c_contig,
nb::device::cpu> ndarray) {
void SoraVideoSource::OnCaptured(
nb::ndarray<uint8_t, nb::shape<-1, -1, 3>, nb::c_contig, nb::device::cpu>
ndarray) {
OnCaptured(ndarray, rtc::TimeMicros());
}

void SoraVideoSource::OnCaptured(nb::ndarray<uint8_t,
nb::shape<nb::any, nb::any, 3>,
nb::c_contig,
nb::device::cpu> ndarray,
double timestamp) {
void SoraVideoSource::OnCaptured(
nb::ndarray<uint8_t, nb::shape<-1, -1, 3>, nb::c_contig, nb::device::cpu>
ndarray,
double timestamp) {
OnCaptured(ndarray, (int64_t)(timestamp * 1000000));
}

void SoraVideoSource::OnCaptured(nb::ndarray<uint8_t,
nb::shape<nb::any, nb::any, 3>,
nb::c_contig,
nb::device::cpu> ndarray,
int64_t timestamp_us) {
void SoraVideoSource::OnCaptured(
nb::ndarray<uint8_t, nb::shape<-1, -1, 3>, nb::c_contig, nb::device::cpu>
ndarray,
int64_t timestamp_us) {
int width = ndarray.shape(1);
int height = ndarray.shape(0);
std::unique_ptr<uint8_t> data(new uint8_t[width * height * 3]);
Expand Down
25 changes: 11 additions & 14 deletions src/sora_video_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ class SoraVideoSource : public SoraTrackInterface {
*
* @param ndarray NumPy の配列 numpy.ndarray で H x W x BGR になっているフレームデータ
*/
void OnCaptured(nb::ndarray<uint8_t,
nb::shape<nb::any, nb::any, 3>,
nb::c_contig,
nb::device::cpu> ndarray);
void OnCaptured(
nb::ndarray<uint8_t, nb::shape<-1, -1, 3>, nb::c_contig, nb::device::cpu>
ndarray);
/**
* Sora に映像データとして送るフレームを渡します。
*
Expand All @@ -61,11 +60,10 @@ class SoraVideoSource : public SoraTrackInterface {
* @param ndarray NumPy の配列 numpy.ndarray で H x W x BGR になっているフレームデータ
* @param timestamp Python の time.time() で取得できるエポック秒で表されるフレームのタイムスタンプ
*/
void OnCaptured(nb::ndarray<uint8_t,
nb::shape<nb::any, nb::any, 3>,
nb::c_contig,
nb::device::cpu> ndarray,
double timestamp);
void OnCaptured(
nb::ndarray<uint8_t, nb::shape<-1, -1, 3>, nb::c_contig, nb::device::cpu>
ndarray,
double timestamp);
/**
* Sora に映像データとして送るフレームを渡します。
*
Expand All @@ -77,11 +75,10 @@ class SoraVideoSource : public SoraTrackInterface {
* @param ndarray NumPy の配列 numpy.ndarray で H x W x BGR になっているフレームデータ
* @param timestamp_us マイクロ秒単位の整数で表されるフレームのタイムスタンプ
*/
void OnCaptured(nb::ndarray<uint8_t,
nb::shape<nb::any, nb::any, 3>,
nb::c_contig,
nb::device::cpu> ndarray,
int64_t timestamp_us);
void OnCaptured(
nb::ndarray<uint8_t, nb::shape<-1, -1, 3>, nb::c_contig, nb::device::cpu>
ndarray,
int64_t timestamp_us);

private:
struct Frame {
Expand Down

0 comments on commit df7a3f7

Please sign in to comment.