Skip to content

Commit

Permalink
Initializing the library the worst way possible
Browse files Browse the repository at this point in the history
At least, it's kind of recording something...

However, I'm still stuck due to this bug :
godotengine/godot#33184

So, I'll have to test this back under Windows...

Signed-off-by: Miouyouyou (Myy) <myy@miouyouyou.fr>
  • Loading branch information
Miouyouyou committed Dec 10, 2020
1 parent 478a28d commit 22a494a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 21 additions & 6 deletions speech.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Speech : public Node {
PoolByteArray input_byte_array;
float volume = 0.0;

mutable Mutex *audio_mutex;
//mutable Mutex *audio_mutex;

int skipped_audio_packets = 0;

Expand Down Expand Up @@ -90,6 +90,7 @@ class Speech : public Node {
// copying from the back.
InputPacket *get_next_valid_input_packet() {
if (current_input_size < MAX_AUDIO_BUFFER_ARRAY_SIZE) {
printf("current_input_size : %d\n", current_input_size);
InputPacket *input_packet = &input_audio_buffer_array[current_input_size];
current_input_size++;
return input_packet;
Expand Down Expand Up @@ -122,18 +123,20 @@ class Speech : public Node {
speech_processor->compress_buffer_internal(&input_byte_array, &compressed_buffer_input);
{
// Lock
MutexLock mutex_lock(audio_mutex);
//MutexLock mutex_lock(audio_mutex);

// Find the next valid input packet in the queue
InputPacket *input_packet = get_next_valid_input_packet();
// Copy the buffer size from the compressed_buffer_input back into the input packet
memcpy(
input_packet->compressed_byte_array.write().ptr(),
compressed_buffer_input.compressed_byte_array->read().ptr(),
SpeechProcessor::PCM_BUFFER_SIZE);
input_packet->compressed_byte_array.write().ptr(),
compressed_buffer_input.compressed_byte_array->read().ptr(),
SpeechProcessor::PCM_BUFFER_SIZE);

input_packet->buffer_size = compressed_buffer_input.buffer_size;
input_packet->loudness = p_mic_input->volume;


}
}

Expand Down Expand Up @@ -183,14 +186,15 @@ class Speech : public Node {
// Copys all the input buffers to the output buffers
// Returns the amount of buffers
Array copy_and_clear_buffers() {
MutexLock mutex_lock(audio_mutex);
//MutexLock mutex_lock(audio_mutex);

Array output_array;
output_array.resize(current_input_size);

for (int i = 0; i < current_input_size; i++) {
Dictionary dict;

printf("i = %d; i < %d\n", i, current_input_size);
dict["byte_array"] = input_audio_buffer_array[i].compressed_byte_array;
dict["buffer_size"] = input_audio_buffer_array[i].buffer_size;
dict["loudness"] = input_audio_buffer_array[i].loudness;
Expand All @@ -211,9 +215,11 @@ class Speech : public Node {
}

bool start_recording() {
print_line("Starting to record");
if (speech_processor) {
speech_processor->start();
skipped_audio_packets = 0;
print_line("Recording...");
return true;
}

Expand All @@ -237,9 +243,12 @@ class Speech : public Node {
}

void _init() {
print_error("Initializing Speech !");
if (!Engine::get_singleton()->is_editor_hint()) {
preallocate_buffers();
speech_processor = memnew(SpeechProcessor);
print_error("speech processor Initialized !");
//audio_mutex = Mutex::create();
}
}

Expand All @@ -254,6 +263,11 @@ class Speech : public Node {
void _notification(int p_what) {
if (!Engine::get_singleton()->is_editor_hint()) {
switch (p_what) {
case NOTIFICATION_READY: {
_init();
_ready();
}
break;
case NOTIFICATION_EXIT_TREE: {
speech_processor->queue_delete();
break;
Expand All @@ -266,6 +280,7 @@ class Speech : public Node {
}

void set_streaming_bus(const String &p_name) {
print_line(String("p_name : ") + p_name);
if (speech_processor) {
speech_processor->set_streaming_bus(p_name);
}
Expand Down
8 changes: 8 additions & 0 deletions speech_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ PoolVector2Array SpeechProcessor::decompress_buffer(
}

void SpeechProcessor::set_streaming_bus(const String &p_name) {
printf("Meow meow meow\n");
print_error(String("p_name : ") + p_name);
if (!audio_server) {
return;
}
Expand Down Expand Up @@ -297,6 +299,12 @@ void SpeechProcessor::_ready() {

void SpeechProcessor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY:
if (!Engine::get_singleton()->is_editor_hint()) {
_init();
_ready();
}
break;
case NOTIFICATION_ENTER_TREE:
if (!Engine::get_singleton()->is_editor_hint()) {
mix_byte_array.resize(BUFFER_FRAME_COUNT * BUFFER_BYTE_COUNT);
Expand Down

0 comments on commit 22a494a

Please sign in to comment.