From 9ccb9d74f3ff656b218e5d282d1657251349c1d7 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 19 Feb 2013 01:21:57 +0200 Subject: modified: sound_detector/main.cpp --- sound_detector/main.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'sound_detector/main.cpp') 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 #include #include +} //boost #include @@ -45,11 +47,59 @@ std::vector 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) -- cgit v1.2.3