2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream_speex.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "audio_stream_speex.h"
|
|
|
|
|
|
|
|
#include "os/os.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "os_support.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#define READ_CHUNK 1024
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static _FORCE_INLINE_ uint16_t le_short(uint16_t s) {
|
|
|
|
uint16_t ret = s;
|
2014-02-13 21:03:28 +00:00
|
|
|
#if 0 //def BIG_ENDIAN_ENABLED
|
2014-02-10 01:10:30 +00:00
|
|
|
ret = s>>8;
|
|
|
|
ret += s<<8;
|
|
|
|
#endif
|
2017-03-18 23:36:26 +00:00
|
|
|
return ret;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioStreamPlaybackSpeex::mix(int16_t *p_buffer, int p_frames) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//printf("update, loops %i, read ofs %i\n", (int)loops, read_ofs);
|
|
|
|
//printf("playing %i, paused %i\n", (int)playing, (int)paused);
|
|
|
|
|
2015-10-08 17:50:18 +00:00
|
|
|
if (!active || !playing || !data.size())
|
2015-09-09 21:50:52 +00:00
|
|
|
return 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
if (read_ofs >= data.size()) {
|
|
|
|
if (loops) {
|
|
|
|
reload();
|
|
|
|
++loop_count;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
int todo = p_frames;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (todo < page_size) {
|
2015-09-09 21:50:52 +00:00
|
|
|
return 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2016-07-06 17:04:21 +00:00
|
|
|
int eos = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
while (todo > page_size) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int ret = 0;
|
|
|
|
while ((todo > page_size && packets_available && !eos) || (ret = ogg_sync_pageout(&oy, &og)) == 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!packets_available) {
|
2017-03-18 23:36:26 +00:00
|
|
|
/*Add page to the bitstream*/
|
2014-02-10 01:10:30 +00:00
|
|
|
ogg_stream_pagein(&os, &og);
|
|
|
|
page_granule = ogg_page_granulepos(&og);
|
|
|
|
page_nb_packets = ogg_page_packets(&og);
|
2017-03-18 23:36:26 +00:00
|
|
|
packet_no = 0;
|
|
|
|
if (page_granule > 0 && frame_size) {
|
|
|
|
skip_samples = page_nb_packets * frame_size * nframes - (page_granule - last_granule);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (ogg_page_eos(&og))
|
|
|
|
skip_samples = -skip_samples;
|
|
|
|
/*else if (!ogg_page_bos(&og))
|
|
|
|
skip_samples = 0;*/
|
2017-03-18 23:36:26 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
skip_samples = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_granule = page_granule;
|
2017-03-18 23:36:26 +00:00
|
|
|
packets_available = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
/*Extract all available packets*/
|
|
|
|
while (todo > page_size && !eos) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ogg_stream_packetout(&os, &op) != 1) {
|
|
|
|
packets_available = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet_no++;
|
|
|
|
|
|
|
|
/*End of stream condition*/
|
|
|
|
if (op.e_o_s)
|
2017-03-18 23:36:26 +00:00
|
|
|
eos = 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
/*Copy Ogg packet to Speex bitstream*/
|
2017-03-18 23:36:26 +00:00
|
|
|
speex_bits_read_from(&bits, (char *)op.packet, op.bytes);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j != nframes; j++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int16_t *out = p_buffer;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
/*Decode frame*/
|
|
|
|
ret = speex_decode_int(st, &bits, out);
|
|
|
|
|
|
|
|
/*for (i=0;i<frame_size*channels;i++)
|
|
|
|
printf ("%d\n", (int)output[i]);*/
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ret == -1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
printf("decode returned -1\n");
|
|
|
|
break;
|
|
|
|
};
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ret == -2) {
|
|
|
|
OS::get_singleton()->printerr("Decoding error: corrupted stream?\n");
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if (speex_bits_remaining(&bits) < 0) {
|
|
|
|
OS::get_singleton()->printerr("Decoding overflow: corrupted stream?\n");
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//if (channels==2)
|
|
|
|
// speex_decode_stereo_int(output, frame_size, &stereo);
|
|
|
|
|
|
|
|
/*Convert to short and save to output file*/
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < frame_size * stream_channels; i++) {
|
|
|
|
out[i] = le_short(out[i]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int new_frame_size = frame_size;
|
|
|
|
|
|
|
|
/*printf ("packet %d %d\n", packet_no, skip_samples);*/
|
2017-03-18 23:36:26 +00:00
|
|
|
if (packet_no == 1 && j == 0 && skip_samples > 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/*printf ("chopping first packet\n");*/
|
|
|
|
new_frame_size -= skip_samples;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if (packet_no == page_nb_packets && skip_samples < 0) {
|
|
|
|
int packet_length = nframes * frame_size + skip_samples;
|
|
|
|
new_frame_size = packet_length - j * frame_size;
|
|
|
|
if (new_frame_size < 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
new_frame_size = 0;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (new_frame_size > frame_size)
|
2014-02-10 01:10:30 +00:00
|
|
|
new_frame_size = frame_size;
|
|
|
|
/*printf ("chopping end: %d %d %d\n", new_frame_size, packet_length, packet_no);*/
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p_buffer += new_frame_size * stream_channels;
|
|
|
|
todo -= new_frame_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
//todo = get_todo();
|
|
|
|
|
|
|
|
//todo is still greater than page size, can write more
|
|
|
|
if (todo > page_size || eos) {
|
|
|
|
if (read_ofs < data.size()) {
|
|
|
|
|
|
|
|
//char *buf;
|
|
|
|
int nb_read = MIN(data.size() - read_ofs, READ_CHUNK);
|
|
|
|
|
|
|
|
/*Get the ogg buffer for writing*/
|
2017-03-18 23:36:26 +00:00
|
|
|
char *ogg_dst = ogg_sync_buffer(&oy, nb_read);
|
2014-02-10 01:10:30 +00:00
|
|
|
/*Read bitstream from input file*/
|
|
|
|
copymem(ogg_dst, &data[read_ofs], nb_read);
|
|
|
|
read_ofs += nb_read;
|
|
|
|
ogg_sync_wrote(&oy, nb_read);
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (loops) {
|
2014-02-10 01:10:30 +00:00
|
|
|
reload();
|
|
|
|
++loop_count;
|
2015-09-09 21:50:52 +00:00
|
|
|
//break;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
playing = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
unload();
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2015-09-09 21:50:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return p_frames - todo;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
void AudioStreamPlaybackSpeex::unload() {
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!active) return;
|
|
|
|
|
|
|
|
speex_bits_destroy(&bits);
|
|
|
|
if (st)
|
|
|
|
speex_decoder_destroy(st);
|
2015-09-09 21:50:52 +00:00
|
|
|
|
|
|
|
ogg_sync_clear(&oy);
|
2014-02-10 01:10:30 +00:00
|
|
|
active = false;
|
|
|
|
//data.resize(0);
|
|
|
|
st = NULL;
|
|
|
|
|
|
|
|
frame_size = 0;
|
|
|
|
page_size = 0;
|
|
|
|
loop_count = 0;
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
void *AudioStreamPlaybackSpeex::process_header(ogg_packet *op, int *frame_size, int *rate, int *nframes, int *channels, int *extra_headers) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
SpeexHeader *header;
|
|
|
|
int modeID;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
header = speex_packet_to_header((char *)op->packet, op->bytes);
|
|
|
|
if (!header) {
|
|
|
|
OS::get_singleton()->printerr("Cannot read header\n");
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if (header->mode >= SPEEX_NB_MODES) {
|
|
|
|
OS::get_singleton()->printerr("Mode number %d does not (yet/any longer) exist in this version\n",
|
|
|
|
header->mode);
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
modeID = header->mode;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const SpeexMode *mode = speex_lib_get_mode(modeID);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (header->speex_version_id > 1) {
|
|
|
|
OS::get_singleton()->printerr("This file was encoded with Speex bit-stream version %d, which I don't know how to decode\n", header->speex_version_id);
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (mode->bitstream_version < header->mode_bitstream_version) {
|
|
|
|
OS::get_singleton()->printerr("The file was encoded with a newer version of Speex. You need to upgrade in order to play it.\n");
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
if (mode->bitstream_version > header->mode_bitstream_version) {
|
|
|
|
OS::get_singleton()->printerr("The file was encoded with an older version of Speex. You would need to downgrade the version in order to play it.\n");
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void *state = speex_decoder_init(mode);
|
|
|
|
if (!state) {
|
|
|
|
OS::get_singleton()->printerr("Decoder initialization failed.\n");
|
2014-02-10 01:10:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
//speex_decoder_ctl(state, SPEEX_SET_ENH, &enh_enabled);
|
|
|
|
speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, frame_size);
|
|
|
|
|
|
|
|
if (!*rate)
|
|
|
|
*rate = header->rate;
|
|
|
|
|
|
|
|
speex_decoder_ctl(state, SPEEX_SET_SAMPLING_RATE, rate);
|
|
|
|
|
|
|
|
*nframes = header->frames_per_packet;
|
|
|
|
|
|
|
|
*channels = header->nb_channels;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (*channels != 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
OS::get_singleton()->printerr("Only MONO speex streams supported\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*extra_headers = header->extra_headers;
|
|
|
|
|
|
|
|
speex_free(header);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
void AudioStreamPlaybackSpeex::reload() {
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (active)
|
|
|
|
unload();
|
|
|
|
|
|
|
|
if (!data.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ogg_sync_init(&oy);
|
|
|
|
speex_bits_init(&bits);
|
|
|
|
|
|
|
|
read_ofs = 0;
|
2017-03-18 23:36:26 +00:00
|
|
|
// char *buf;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int packet_count = 0;
|
|
|
|
int extra_headers = 0;
|
|
|
|
int stream_init = 0;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
page_granule = 0;
|
|
|
|
last_granule = 0;
|
|
|
|
skip_samples = 0;
|
|
|
|
page_nb_packets = 0;
|
|
|
|
packets_available = false;
|
|
|
|
packet_no = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int eos = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
/*Get the ogg buffer for writing*/
|
|
|
|
int nb_read = MIN(data.size() - read_ofs, READ_CHUNK);
|
2017-03-18 23:36:26 +00:00
|
|
|
char *ogg_dst = ogg_sync_buffer(&oy, nb_read);
|
2014-02-10 01:10:30 +00:00
|
|
|
/*Read bitstream from input file*/
|
|
|
|
copymem(ogg_dst, &data[read_ofs], nb_read);
|
|
|
|
read_ofs += nb_read;
|
|
|
|
ogg_sync_wrote(&oy, nb_read);
|
|
|
|
|
|
|
|
/*Loop for all complete pages we got (most likely only one)*/
|
2017-03-18 23:36:26 +00:00
|
|
|
while (ogg_sync_pageout(&oy, &og) == 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (stream_init == 0) {
|
|
|
|
ogg_stream_init(&os, ogg_page_serialno(&og));
|
|
|
|
stream_init = 1;
|
|
|
|
}
|
|
|
|
/*Add page to the bitstream*/
|
|
|
|
ogg_stream_pagein(&os, &og);
|
|
|
|
page_granule = ogg_page_granulepos(&og);
|
|
|
|
page_nb_packets = ogg_page_packets(&og);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (page_granule > 0 && frame_size) {
|
|
|
|
skip_samples = page_nb_packets * frame_size * nframes - (page_granule - last_granule);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (ogg_page_eos(&og))
|
|
|
|
skip_samples = -skip_samples;
|
|
|
|
/*else if (!ogg_page_bos(&og))
|
|
|
|
skip_samples = 0;*/
|
2017-03-18 23:36:26 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
skip_samples = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_granule = page_granule;
|
|
|
|
/*Extract all available packets*/
|
2017-03-18 23:36:26 +00:00
|
|
|
while (!eos && ogg_stream_packetout(&os, &op) == 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/*If first packet, process as Speex header*/
|
2017-03-18 23:36:26 +00:00
|
|
|
if (packet_count == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
int rate = 0;
|
|
|
|
int channels;
|
|
|
|
st = process_header(&op, &frame_size, &rate, &nframes, &channels, &extra_headers);
|
|
|
|
if (!nframes)
|
2017-03-18 23:36:26 +00:00
|
|
|
nframes = 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!st) {
|
|
|
|
unload();
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
page_size = nframes * frame_size;
|
2017-03-18 23:36:26 +00:00
|
|
|
stream_srate = rate;
|
|
|
|
stream_channels = channels;
|
|
|
|
stream_minbuff_size = page_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (packet_count == 1) {
|
|
|
|
} else if (packet_count <= 1 + extra_headers) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/* Ignore extra headers */
|
|
|
|
};
|
|
|
|
};
|
|
|
|
++packet_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
} while (packet_count <= extra_headers);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
active = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
void AudioStreamPlaybackSpeex::_bind_methods() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
//ObjectTypeDB::bind_method(_MD("set_file","file"),&AudioStreamPlaybackSpeex::set_file);
|
2017-03-18 23:36:26 +00:00
|
|
|
// ObjectTypeDB::bind_method(_MD("get_file"),&AudioStreamPlaybackSpeex::get_file);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_bundled"), &AudioStreamPlaybackSpeex::_set_bundled);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_bundled"), &AudioStreamPlaybackSpeex::_get_bundled);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_BUNDLE), _SCS("_set_bundled"), _SCS("_get_bundled"));
|
2015-09-09 21:50:52 +00:00
|
|
|
//ADD_PROPERTY( PropertyInfo(Variant::STRING,"file",PROPERTY_HINT_FILE,"*.spx"),_SCS("set_file"),_SCS("get_file"));
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioStreamPlaybackSpeex::_set_bundled(const Dictionary &dict) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(!dict.has("filename"));
|
|
|
|
ERR_FAIL_COND(!dict.has("data"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
filename = dict["filename"];
|
|
|
|
data = dict["data"];
|
|
|
|
};
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
Dictionary AudioStreamPlaybackSpeex::_get_bundled() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Dictionary d;
|
|
|
|
d["filename"] = filename;
|
|
|
|
d["data"] = data;
|
|
|
|
return d;
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioStreamPlaybackSpeex::set_data(const Vector<uint8_t> &p_data) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
data = p_data;
|
2014-02-10 01:10:30 +00:00
|
|
|
reload();
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
void AudioStreamPlaybackSpeex::play(float p_from_pos) {
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
reload();
|
|
|
|
if (!active)
|
|
|
|
return;
|
|
|
|
playing = true;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioStreamPlaybackSpeex::stop() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
unload();
|
|
|
|
playing = false;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool AudioStreamPlaybackSpeex::is_playing() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
return playing;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioStreamPlaybackSpeex::set_loop(bool p_enable) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
loops = p_enable;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool AudioStreamPlaybackSpeex::has_loop() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return loops;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioStreamPlaybackSpeex::get_length() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String AudioStreamPlaybackSpeex::get_stream_name() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioStreamPlaybackSpeex::get_loop_count() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioStreamPlaybackSpeex::get_pos() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-09 21:50:52 +00:00
|
|
|
void AudioStreamPlaybackSpeex::seek_pos(float p_time){
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
AudioStreamPlaybackSpeex::AudioStreamPlaybackSpeex() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
active = false;
|
2015-09-09 21:50:52 +00:00
|
|
|
st = NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
stream_channels = 1;
|
|
|
|
stream_srate = 1;
|
|
|
|
stream_minbuff_size = 1;
|
|
|
|
playing = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
AudioStreamPlaybackSpeex::~AudioStreamPlaybackSpeex() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
unload();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:50:52 +00:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioStreamSpeex::set_file(const String &p_file) {
|
2015-09-09 21:50:52 +00:00
|
|
|
|
|
|
|
if (this->file == p_file)
|
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
this->file = p_file;
|
2015-09-09 21:50:52 +00:00
|
|
|
|
|
|
|
if (p_file == "") {
|
|
|
|
data.resize(0);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
Error err;
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *file = FileAccess::open(p_file, FileAccess::READ, &err);
|
2015-09-09 21:50:52 +00:00
|
|
|
if (err != OK) {
|
|
|
|
data.resize(0);
|
|
|
|
};
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
|
|
|
|
this->file = p_file;
|
|
|
|
data.resize(file->get_len());
|
|
|
|
int read = file->get_buffer(&data[0], data.size());
|
|
|
|
memdelete(file);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
RES ResourceFormatLoaderAudioStreamSpeex::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2015-08-23 23:15:56 +00:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-18 23:36:26 +00:00
|
|
|
*r_error = OK;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
AudioStreamSpeex *stream = memnew(AudioStreamSpeex);
|
|
|
|
stream->set_file(p_path);
|
|
|
|
return Ref<AudioStreamSpeex>(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatLoaderAudioStreamSpeex::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
|
|
|
|
p_extensions->push_back("spx");
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ResourceFormatLoaderAudioStreamSpeex::handles_type(const String &p_type) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return (p_type == "AudioStream" || p_type == "AudioStreamSpeex");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatLoaderAudioStreamSpeex::get_resource_type(const String &p_path) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_path.extension().to_lower() == "spx")
|
2014-02-10 01:10:30 +00:00
|
|
|
return "AudioStreamSpeex";
|
|
|
|
return "";
|
|
|
|
}
|