-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from martinRenou/implement_audio_video_widgets
Implement audio and video widgets
- Loading branch information
Showing
11 changed files
with
501 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/*************************************************************************** | ||
* Copyright (c) 2017, Sylvain Corlay and Johan Mabille * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
****************************************************************************/ | ||
|
||
#ifndef XWIDGETS_AUDIO_HPP | ||
#define XWIDGETS_AUDIO_HPP | ||
|
||
#include <cstddef> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "xmaterialize.hpp" | ||
#include "xmedia.hpp" | ||
|
||
namespace xw | ||
{ | ||
/********************* | ||
* audio declaration * | ||
*********************/ | ||
|
||
template <class D> | ||
class xaudio : public xmedia<D> | ||
{ | ||
public: | ||
|
||
using base_type = xmedia<D>; | ||
using derived_type = D; | ||
|
||
void serialize_state(xeus::xjson& state, xeus::buffer_sequence&) const; | ||
void apply_patch(const xeus::xjson&, const xeus::buffer_sequence&); | ||
|
||
XPROPERTY(std::string, derived_type, format, "mp4"); | ||
XPROPERTY(bool, derived_type, autoplay, true); | ||
XPROPERTY(bool, derived_type, loop, true); | ||
XPROPERTY(bool, derived_type, controls, true); | ||
|
||
protected: | ||
|
||
xaudio(); | ||
using base_type::base_type; | ||
|
||
private: | ||
|
||
void set_defaults(); | ||
}; | ||
|
||
using audio = xmaterialize<xaudio>; | ||
|
||
using audio_generator = xgenerator<xaudio>; | ||
|
||
/************************* | ||
* xaudio implementation * | ||
*************************/ | ||
|
||
template <class D> | ||
inline void xaudio<D>::serialize_state(xeus::xjson& state, xeus::buffer_sequence& buffers) const | ||
{ | ||
base_type::serialize_state(state, buffers); | ||
|
||
set_patch_from_property(format, state, buffers); | ||
set_patch_from_property(autoplay, state, buffers); | ||
set_patch_from_property(loop, state, buffers); | ||
set_patch_from_property(controls, state, buffers); | ||
} | ||
|
||
template <class D> | ||
inline void xaudio<D>::apply_patch(const xeus::xjson& patch, const xeus::buffer_sequence& buffers) | ||
{ | ||
base_type::apply_patch(patch, buffers); | ||
|
||
set_property_from_patch(format, patch, buffers); | ||
set_property_from_patch(autoplay, patch, buffers); | ||
set_property_from_patch(loop, patch, buffers); | ||
set_property_from_patch(controls, patch, buffers); | ||
} | ||
|
||
template <class D> | ||
inline xaudio<D>::xaudio() | ||
: base_type() | ||
{ | ||
set_defaults(); | ||
} | ||
|
||
template <class D> | ||
inline void xaudio<D>::set_defaults() | ||
{ | ||
this->_model_name() = "AudioModel"; | ||
this->_view_name() = "AudioView"; | ||
} | ||
|
||
/********************** | ||
* custom serializers * | ||
**********************/ | ||
|
||
inline void set_patch_from_property(const decltype(audio::value)& property, | ||
xeus::xjson& patch, | ||
xeus::buffer_sequence& buffers) | ||
{ | ||
patch[property.name()] = xbuffer_reference_prefix() + std::to_string(buffers.size()); | ||
buffers.emplace_back(property().data(), property().size()); | ||
} | ||
|
||
inline void set_property_from_patch(decltype(audio::value)& property, | ||
const xeus::xjson& patch, | ||
const xeus::buffer_sequence& buffers) | ||
{ | ||
auto it = patch.find(property.name()); | ||
if (it != patch.end()) | ||
{ | ||
using value_type = typename decltype(audio::value)::value_type; | ||
std::size_t index = buffer_index(patch[property.name()].template get<std::string>()); | ||
const auto& value_buffer = buffers[index]; | ||
const char* value_buf = value_buffer.data<const char>(); | ||
property = value_type(value_buf, value_buf + value_buffer.size()); | ||
} | ||
} | ||
|
||
inline audio_generator audio_from_file(const std::string& filename) | ||
{ | ||
return audio_generator().value(read_file(filename)); | ||
} | ||
|
||
inline audio_generator audio_from_url(const std::string& url) | ||
{ | ||
std::vector<char> value(url.cbegin(), url.cend()); | ||
return audio_generator().value(value).format("url"); | ||
} | ||
|
||
/********************* | ||
* precompiled types * | ||
*********************/ | ||
|
||
#ifndef _WIN32 | ||
extern template class xmaterialize<xaudio>; | ||
extern template xmaterialize<xaudio>::xmaterialize(); | ||
extern template class xtransport<xmaterialize<xaudio>>; | ||
extern template class xgenerator<xaudio>; | ||
extern template xgenerator<xaudio>::xgenerator(); | ||
extern template class xtransport<xgenerator<xaudio>>; | ||
#endif | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/*************************************************************************** | ||
* Copyright (c) 2018, Sylvain Corlay, Johan Mabille an * | ||
* * | ||
* Distributed under the terms of the BSD 3-Clause License. * | ||
* * | ||
* The full license is in the file LICENSE, distributed with this software. * | ||
****************************************************************************/ | ||
|
||
#ifndef XWIDGETS_MEDIA_HPP | ||
#define XWIDGETS_MEDIA_HPP | ||
|
||
#include <fstream> | ||
#include <cstddef> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "xmaterialize.hpp" | ||
#include "xwidget.hpp" | ||
|
||
namespace xw | ||
{ | ||
/********************* | ||
* media declaration * | ||
*********************/ | ||
|
||
template <class D> | ||
class xmedia : public xwidget<D> | ||
{ | ||
public: | ||
|
||
using base_type = xwidget<D>; | ||
using derived_type = D; | ||
|
||
using value_type = std::vector<char>; | ||
|
||
void serialize_state(xeus::xjson& state, xeus::buffer_sequence&) const; | ||
void apply_patch(const xeus::xjson&, const xeus::buffer_sequence&); | ||
|
||
XPROPERTY(value_type, derived_type, value); | ||
|
||
const std::vector<xjson_path_type>& buffer_paths() const; | ||
|
||
protected: | ||
|
||
xmedia(); | ||
using base_type::base_type; | ||
|
||
private: | ||
|
||
void set_defaults(); | ||
}; | ||
|
||
using media = xmaterialize<xmedia>; | ||
|
||
using media_generator = xgenerator<xmedia>; | ||
|
||
/************************* | ||
* xmedia implementation * | ||
*************************/ | ||
|
||
template <class D> | ||
inline void xmedia<D>::serialize_state(xeus::xjson& state, xeus::buffer_sequence& buffers) const | ||
{ | ||
base_type::serialize_state(state, buffers); | ||
|
||
set_patch_from_property(value, state, buffers); | ||
} | ||
|
||
template <class D> | ||
inline void xmedia<D>::apply_patch(const xeus::xjson& patch, const xeus::buffer_sequence& buffers) | ||
{ | ||
base_type::apply_patch(patch, buffers); | ||
|
||
set_property_from_patch(value, patch, buffers); | ||
} | ||
|
||
template <class D> | ||
inline const std::vector<xjson_path_type>& xmedia<D>::buffer_paths() const | ||
{ | ||
static const std::vector<xjson_path_type> default_buffer_paths = {{"value"}}; | ||
return default_buffer_paths; | ||
} | ||
|
||
template <class D> | ||
inline xmedia<D>::xmedia() | ||
: base_type() | ||
{ | ||
set_defaults(); | ||
} | ||
|
||
template <class D> | ||
inline void xmedia<D>::set_defaults() | ||
{ | ||
this->_model_module() = "@jupyter-widgets/controls"; | ||
this->_view_module() = "@jupyter-widgets/controls"; | ||
this->_model_module_version() = XWIDGETS_CONTROLS_VERSION; | ||
this->_view_module_version() = XWIDGETS_CONTROLS_VERSION; | ||
} | ||
|
||
inline std::vector<char> read_file(const std::string& filename) | ||
{ | ||
const char* cstr = filename.c_str(); | ||
std::basic_ifstream<char> file(cstr, std::ios::binary); | ||
return std::vector<char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); | ||
} | ||
|
||
/********************* | ||
* precompiled types * | ||
*********************/ | ||
|
||
#ifndef _WIN32 | ||
extern template class xmaterialize<xmedia>; | ||
extern template xmaterialize<xmedia>::xmaterialize(); | ||
extern template class xtransport<xmaterialize<xmedia>>; | ||
extern template class xgenerator<xmedia>; | ||
extern template xgenerator<xmedia>::xgenerator(); | ||
extern template class xtransport<xgenerator<xmedia>>; | ||
#endif | ||
} | ||
#endif |
Oops, something went wrong.