Skip to content

Commit

Permalink
Merge pull request #19 from hungrymonkey/talkingtreestorage
Browse files Browse the repository at this point in the history
Talkingtreestorage
  • Loading branch information
jcalifornia authored Feb 2, 2018
2 parents d3a7b73 + 90d3e7e commit 71899ae
Show file tree
Hide file tree
Showing 45 changed files with 5,216 additions and 0 deletions.
36 changes: 36 additions & 0 deletions modules/talkingtree_storage/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python

# SCsub
Import('env')

env_talking_tree_storage = env.Clone()

thirdparty_libskeleton_srcs = [
"skeleton.c",
"skeleton_query.c",
"skeleton_vector.c"
]

thirdparty_libskeleton_dir = "#thirdparty/libskeleton/"
thirdparty_libskeleton_sources = [thirdparty_libskeleton_dir + "src/" + f for f in thirdparty_libskeleton_srcs]
env_talking_tree_storage.Append(CPPPATH=[thirdparty_libskeleton_dir + "include" ])
env_talking_tree_storage.add_source_files(env.modules_sources, thirdparty_libskeleton_sources)

if env['builtin_opus']:
thirdparty_dir = "#thirdparty/opus/"

thirdparty_include_paths = [
"",
"celt",
"opus",
"silk",
"silk/fixed",
"silk/float",
]
env_talking_tree_storage.Append(CPPPATH=[thirdparty_dir + "/" + dir for dir in thirdparty_include_paths])
# also requires libogg
if env['builtin_libogg']:
env_talking_tree_storage.Append(CPPPATH=["#thirdparty/libogg"])

env_talking_tree_storage.add_source_files(env.modules_sources,"*.cpp")
env_talking_tree_storage.Append(CXXFLAGS=['-O2', '-std=c++14'])
6 changes: 6 additions & 0 deletions modules/talkingtree_storage/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def can_build(platform):
return True

def configure(env):
env.ParseConfig('pkg-config kate --cflags --libs')
pass
48 changes: 48 additions & 0 deletions modules/talkingtree_storage/ogg_routines.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#include "ogg_routines.h"

#include "ustring.h"
#include <string.h>

//https://dismaldenizen.wordpress.com/2010/08/20/the-ogg-container-format-explained/
int htogg_write_page(ogg_page *page, FileAccess *fa){
fa->store_buffer(page->header, page->header_len);
fa->store_buffer(page->body, page->body_len);
return page->header_len + page->body_len;
}

int ogg_buffer(FileAccess *fa, ogg_sync_state *oy){
uint8_t * buffer = (uint8_t *) ogg_sync_buffer (oy, 4096);
int bytes = fa->get_buffer(buffer, 4096);
ogg_sync_wrote (oy,bytes);
return (bytes);
}

bool is_fishhead_packet(ogg_packet *packet){
return packet &&
packet->bytes > 8 &&
memcmp(packet->packet, "fishead", 8) == 0;
}

bool is_fishbone_packet(ogg_packet *packet){
return packet->packet &&
packet->bytes >= 52 &&
memcmp(packet->packet, "fisbone", 8) == 0;
}

unsigned char * write_le32(unsigned char* p, const ogg_uint32_t num){
ogg_int32_t n = num;
ogg_int32_t i;
for (i=0; i<4; i++) {
p[i] = (unsigned char)(n & 0xff);
n >>= 8;
}
return p + 4;
}

unsigned char * write_le16(unsigned char* p, const ogg_uint32_t num){
ogg_uint16_t n = num;
p[0] = (unsigned char)(n & 0xff);
p[1] = (unsigned char)((n >> 8) & 0xff);
return p + 2;
}
15 changes: 15 additions & 0 deletions modules/talkingtree_storage/ogg_routines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "ogg/ogg.h"
#include "os/file_access.h"


int htogg_write_page(ogg_page *page, FileAccess *fa);

int ogg_buffer(FileAccess *fa, ogg_sync_state *oy);

bool is_fishhead_packet(ogg_packet *packet);

bool is_fishbone_packet(ogg_packet *packet);

unsigned char * write_le32(unsigned char* p, const ogg_uint32_t num);

unsigned char * write_le16(unsigned char* p, const ogg_uint32_t num);
22 changes: 22 additions & 0 deletions modules/talkingtree_storage/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "register_types.h"
#include "class_db.h"
#include "talking_tree_storage.h"
#include "talkingtree_storage_loader.h"


static ResourceFormatTalkingTreeStorage *talkingtree_storage_loader = NULL;
static TalkingTreeStorage *talking_tree_storage_writer = NULL;
void register_talkingtree_storage_types(){


talkingtree_storage_loader = memnew(ResourceFormatTalkingTreeStorage);
ResourceLoader::add_resource_format_loader(talkingtree_storage_loader);

talking_tree_storage_writer = memnew(TalkingTreeStorage);
talking_tree_storage_writer->init();
talking_tree_storage_writer->set_singleton();
ClassDB::register_class<TreecursionWriter>();
}
void unregister_talkingtree_storage_types(){
talking_tree_storage_writer->finish();
}
5 changes: 5 additions & 0 deletions modules/talkingtree_storage/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* register_types.h */

void register_talkingtree_storage_types();
void unregister_talkingtree_storage_types();
/* yes, the word in the middle must be the same as the module folder name */
92 changes: 92 additions & 0 deletions modules/talkingtree_storage/talking_tree_storage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* talking_tree_storage.cpp */

#include "talking_tree_storage.h"
#include "os/os.h"



void TalkingTreeStorage::_bind_methods() {
ClassDB::bind_method(D_METHOD("close_file"), &TalkingTreeStorage::close_file);
ClassDB::bind_method(D_METHOD("write_header"), &TalkingTreeStorage::write_header);
ClassDB::bind_method(D_METHOD("create_file"), &TalkingTreeStorage::new_file);
}
//https://xiph.org/ogg/doc/rfc5334.txt

TalkingTreeStorage *TalkingTreeStorage::_singleton = NULL;
TalkingTreeStorage *TalkingTreeStorage::get_singleton() {
return _singleton;
}
void TalkingTreeStorage::set_singleton() {
_singleton = this;
}
void TalkingTreeStorage::new_file(){
treecursion = memnew(TreecursionWriter);
}
void TalkingTreeStorage::write_header(){
treecursion->write_header();
}
void TalkingTreeStorage::close_file(){
memdelete(treecursion);
treecursion = NULL;
}
void TalkingTreeStorage::lock() {
if (!_thread || !_mutex)
return;
_mutex->lock();
}
void TalkingTreeStorage::unlock() {

if (!_thread || !_mutex)
return;
_mutex->unlock();
}
//https://github.com/gcp/opus-tools/blob/master/src/opusenc.c#L86


Error TalkingTreeStorage::init(){
_thread_exited = false;
_mutex = Mutex::create();
_thread = Thread::create(TalkingTreeStorage::thread_func, this);
return OK;
}


void TalkingTreeStorage::thread_func(void *p_udata){
TalkingTreeStorage *ac = (TalkingTreeStorage *) p_udata;


//every half second.
uint64_t usdelay = 500000;
while(!ac -> _exit_thread){
if(ac->treecursion != NULL){

}
OS::get_singleton()->delay_usec(usdelay);
}
}

void TalkingTreeStorage::finish() {
if (!_thread)
return;
_exit_thread = true;
Thread::wait_to_finish(_thread);

if(opusEncoder){
opus_multistream_encoder_destroy(opusEncoder);
}

memdelete(_thread);
if (_mutex)
memdelete(_mutex);
_thread = NULL;
}


TalkingTreeStorage::TalkingTreeStorage() {
//opusEncoder = opus_multistream_encoder_create();

}

TalkingTreeStorage::~TalkingTreeStorage() {

}
65 changes: 65 additions & 0 deletions modules/talkingtree_storage/talking_tree_storage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* talking_tree_storage.h */
#ifndef TALKING_TREE_STORAGE_H
#define TALKING_TREE_STORAGE_H



#include "object.h"
#include "variant.h"
#include "core/os/thread.h"
#include "core/os/mutex.h"

#include "reference.h"
#include "os/file_access.h"
#include "ustring.h"

#include "ogg/ogg.h"
#include "treecusion_types.h"
#include "treecursion_writer.h"


#include <opus.h>
#include <opus_multistream.h>

class TalkingTreeStorage : public Object {
GDCLASS(TalkingTreeStorage, Object);

static TalkingTreeStorage *_singleton;
static void thread_func(void *p_udata);
public:
static TalkingTreeStorage *get_singleton();
void set_singleton();
void lock();
void unlock();
void finish();
Error init();

private:
bool _thread_exited;
mutable bool _exit_thread;
Thread *_thread;
Mutex *_mutex;

protected:
static void _bind_methods();

public:
void new_file();
void write_header();
void close_file();
TalkingTreeStorage();
~TalkingTreeStorage();

private:
enum{
//https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__multistream.html
SAMPLE_RATE = 48000,

};
OpusMSEncoder *opusEncoder;
TreecursionWriter *treecursion;


};

#endif
29 changes: 29 additions & 0 deletions modules/talkingtree_storage/talkingtree_storage_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "talkingtree_storage_loader.h"
#include "treecursion_reader.h"


ResourceFormatTalkingTreeStorage::ResourceFormatTalkingTreeStorage() {
}
RES ResourceFormatTalkingTreeStorage::load(const String &p_path, const String &p_original_path, Error *r_error) {
if (r_error)
*r_error = OK;

TreecursionReader *treecursion = memnew(TreecursionReader);
treecursion->set_file(p_path);
return Ref<TreecursionReader>(treecursion);
}

void ResourceFormatTalkingTreeStorage::get_recognized_extensions(List<String> *p_extensions) const {

p_extensions->push_back("htogg");
}
String ResourceFormatTalkingTreeStorage::get_resource_type(const String &p_path) const {

if (p_path.get_extension().to_lower() == "htogg")
return "Treecursion";
return "";
}

bool ResourceFormatTalkingTreeStorage::handles_type(const String &p_type) const {
return (p_type == "Treecursion");
}
19 changes: 19 additions & 0 deletions modules/talkingtree_storage/talkingtree_storage_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef TALKINGTREE_STORAGE_LOADER_H
#define TALKINGTREE_STORAGE_LOADER_H

#include "io/resource_loader.h"


class ResourceFormatTalkingTreeStorage : public ResourceFormatLoader {
public:
virtual RES load(const String &p_path, const String &p_original_path, Error *r_error = NULL);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;

ResourceFormatTalkingTreeStorage();
virtual ~ResourceFormatTalkingTreeStorage() {}
};


#endif // TALKINGTREE_STORAGE_LOADER_H
Loading

0 comments on commit 71899ae

Please sign in to comment.