324 lines
8.0 KiB
C++
324 lines
8.0 KiB
C++
/*
|
|
osc/argument.h
|
|
|
|
Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <bufferstream.h>
|
|
#include <chrono>
|
|
#include <vector>
|
|
|
|
|
|
/**
|
|
* @brief The Open Sound Control message format.
|
|
*
|
|
* \cite Spec10 Open Sound Control (OSC) is an open, transport-independent, message-based
|
|
* protocol developed for communication among computers, sound synthesizers, and other
|
|
* multimedia devices.
|
|
*/
|
|
namespace OSC {
|
|
|
|
|
|
/**
|
|
* @brief The Argument struct
|
|
*/
|
|
struct Argument
|
|
: public streamable
|
|
{
|
|
/**
|
|
* @brief Argument
|
|
* @param type
|
|
*/
|
|
Argument(char type) : type_(type) {};
|
|
|
|
virtual std::string type() const;
|
|
|
|
private:
|
|
char type_;
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The int32 Argument type
|
|
*/
|
|
struct int32
|
|
: Argument
|
|
{
|
|
int32() : Argument('i') {};
|
|
|
|
virtual size_t streamSize() const override { return 4; }
|
|
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
|
|
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
|
|
|
|
int32_t value; //!< value of the integer
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The float32 Argument type
|
|
*/
|
|
struct float32
|
|
: Argument
|
|
{
|
|
float32() : Argument('f') {};
|
|
|
|
virtual size_t streamSize() const override { return 4; }
|
|
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
|
|
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
|
|
|
|
float value; //!< value of the floating point number
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The string Argument type
|
|
*/
|
|
struct string
|
|
: Argument
|
|
{
|
|
string() : Argument('s') {};
|
|
|
|
virtual size_t streamSize() const override;
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override;
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override;
|
|
|
|
std::string value; //!< charecters of the string
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The blob Argument type
|
|
*/
|
|
struct blob
|
|
: Argument
|
|
{
|
|
blob() : Argument('b') {};
|
|
|
|
virtual size_t streamSize() const override;
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override;
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override;
|
|
|
|
std::vector<uint8_t> value; //!< blob data
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The int64 Argument type
|
|
*/
|
|
struct int64
|
|
: Argument
|
|
{
|
|
int64() : Argument('h') {};
|
|
|
|
virtual size_t streamSize() const override { return 8; }
|
|
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
|
|
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
|
|
|
|
int64_t value; //!< value of the integer
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The timetag Argument type
|
|
*/
|
|
struct timetag
|
|
: Argument
|
|
{
|
|
timetag() : Argument('t'), value(0) {};
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> time() const;
|
|
void setTime(std::chrono::time_point<std::chrono::system_clock> time = std::chrono::system_clock::now());
|
|
|
|
virtual size_t streamSize() const override { return 8; }
|
|
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
|
|
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
|
|
|
|
union {
|
|
uint64_t value;
|
|
struct {
|
|
uint32_t seconds; //!< the number of seconds since midnight on January 1, 1900
|
|
uint32_t fractional; //!< fractional parts of a second
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The float64 Argument type
|
|
*/
|
|
struct float64
|
|
: Argument
|
|
{
|
|
float64() : Argument('d') {};
|
|
|
|
virtual size_t streamSize() const override { return 8; }
|
|
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
|
|
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
|
|
|
|
double value; //!< value of the float
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The character Argument type
|
|
*/
|
|
struct character
|
|
: Argument
|
|
{
|
|
character() : Argument('c') {};
|
|
|
|
virtual size_t streamSize() const override { return 4; }
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override;
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override;
|
|
|
|
char value; //!< value of the argument
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The rgba Argument type
|
|
*/
|
|
struct rgba
|
|
: Argument
|
|
{
|
|
rgba() : Argument('r') {};
|
|
|
|
virtual size_t streamSize() const override { return 4; }
|
|
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
|
|
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
|
|
|
|
union {
|
|
uint32_t value;
|
|
struct {
|
|
uint8_t red;
|
|
uint8_t blue;
|
|
uint8_t green;
|
|
uint8_t alpha;
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The midi Argument type
|
|
*/
|
|
struct midi
|
|
: Argument
|
|
{
|
|
midi() : Argument('m') {};
|
|
|
|
virtual size_t streamSize() const override { return 4; }
|
|
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
|
|
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
|
|
|
|
union {
|
|
uint32_t value;
|
|
struct {
|
|
uint8_t port;
|
|
uint8_t status;
|
|
uint8_t data1;
|
|
uint8_t data2;
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The True Argument type
|
|
*/
|
|
struct True
|
|
: Argument
|
|
{
|
|
True() : Argument('T') {};
|
|
|
|
virtual size_t streamSize() const override { return 0; }
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override {};
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
|
|
|
|
const bool value = true; //!< value of the argument
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The False Argument type
|
|
*/
|
|
struct False
|
|
: Argument
|
|
{
|
|
False() : Argument('F') {};
|
|
|
|
virtual size_t streamSize() const override { return 0; }
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override {};
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
|
|
|
|
const bool value = false; //!< value of the argument
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The Null Argument type
|
|
*/
|
|
struct Null
|
|
: Argument
|
|
{
|
|
Null() : Argument('N') {};
|
|
|
|
virtual size_t streamSize() const override { return 0; }
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override {};
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The impulse Argument type
|
|
*/
|
|
struct impulse
|
|
: Argument
|
|
{
|
|
impulse() : Argument('I') {};
|
|
|
|
virtual size_t streamSize() const override { return 0; }
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override {};
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The array Argument type
|
|
*/
|
|
struct array
|
|
: Argument
|
|
{
|
|
array() : Argument('[') {};
|
|
|
|
void setTypes(std::string types);
|
|
|
|
virtual std::string type() const override;
|
|
virtual size_t streamSize() const override;
|
|
virtual void iStream(std::shared_ptr<bufferstream>) override;
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const override;
|
|
|
|
std::vector<std::shared_ptr<Argument>> members; //!< components of the array
|
|
};
|
|
|
|
} // namespace OSC
|