Skip to content

Instantly share code, notes, and snippets.

@klemensbaum
Created March 7, 2017 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save klemensbaum/fb7c41c981ac43a5abbb3d8fb99d3902 to your computer and use it in GitHub Desktop.
Save klemensbaum/fb7c41c981ac43a5abbb3d8fb99d3902 to your computer and use it in GitHub Desktop.
#include <speex/speex.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <cassert>
int main(int argc, char const *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <encoded-file>\n";
return EXIT_FAILURE;
}
SpeexBits bits;
void *dec_state;
dec_state = speex_decoder_init(&speex_nb_mode);
spx_int32_t enh = 1;
speex_decoder_ctl(dec_state, SPEEX_SET_ENH, &enh);
spx_int32_t frame_size;
speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &frame_size);
speex_bits_init(&bits);
std::ifstream in(argv[1], std::ios::binary);
std::vector<char> encoded_data{
std::istream_iterator<char>{in}, std::istream_iterator<char>{}
};
speex_bits_read_from(&bits, encoded_data.data(), encoded_data.size());
// From WebRTC:
static const int kMaxFrameSize = 2880; // 60 ms @ 48 kHz.
std::int16_t raw_data[kMaxFrameSize];
std::size_t raw_data_offset= 0;
while (speex_decode_int(dec_state, &bits, raw_data + raw_data_offset) == 0)
{
raw_data_offset += frame_size;
}
assert(raw_data_offset <= sizeof(raw_data));
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment