diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-19 01:21:57 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-19 01:21:57 +0200 |
commit | 9ccb9d74f3ff656b218e5d282d1657251349c1d7 (patch) | |
tree | 11f5d7ae86d2a1685f4ceba77b26acc7c2c0b8c2 /sound_detector/main.cpp | |
parent | 908fc2b63dca73ba642bacaf8bb7a038b8df3880 (diff) |
modified: sound_detector/main.cpp
Diffstat (limited to 'sound_detector/main.cpp')
-rw-r--r-- | sound_detector/main.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/sound_detector/main.cpp b/sound_detector/main.cpp index 068964c..d35a261 100644 --- a/sound_detector/main.cpp +++ b/sound_detector/main.cpp @@ -18,9 +18,11 @@ //ffmpeg +extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswresample/swresample.h> +} //boost #include <boost/thread.hpp> @@ -45,11 +47,59 @@ std::vector<int16_t> buffer; +void encode_start() +{ + AVCodec *codec; + AVCodecContext *c= NULL; + avcodec_register_all(); + codec = avcodec_find_encoder(CODEC_ID_VORBIS); + if (!codec) + { + fprintf(stderr, "codec not found\n"); + exit(1); + } + c= avcodec_alloc_context3(codec); + c->channels = 1; + c->bit_rate = 32000; + c->sample_rate = 11025; + c->sample_fmt = AV_SAMPLE_FMT_S16; + if (avcodec_open2(c, codec, NULL) < 0) + { + fprintf(stderr, "could not open codec\n"); + exit(1); + } + + AVPacket p; + av_init_packet(&p); + AVFrame *f = avcodec_alloc_frame(); + + f->nb_samples = c->frame_size; + union int_map + { + int16_t t16; + uint8_t t8[2]; + }; + int_map tmp_buf[f->nb_samples /2]; + for(int i = 0; i < (f->nb_samples/2); i++) + tmp_buf[i].t16 = buffer[i]; + uint8_t tmp_buf2[f->nb_samples]; + for(int i = 0, ii = 0; i < (f->nb_samples/2); i++) + { + tmp_buf2[ii] = tmp_buf[i].t8[0]; + ii++; + tmp_buf2[ii] = tmp_buf[i].t8[1]; + ii++; + } + f->data[0] = tmp_buf2; + int got_packet = 0; + avcodec_encode_audio2(c, &p, f, &got_packet); +} void handle_data() { while(true) { + encode_start(); boost::this_thread::sleep(boost::posix_time::seconds(1)); printf("buffer contain %u, required %d\n", buffer.size(), rate*3); if(!sound_detected) |