OSDN Git Service

[Qt][MOVIE_SAVER] Fix choppy sound.
authorK.Ohta <whatisthis.sowhat@gmail.com>
Mon, 30 May 2016 13:08:48 +0000 (22:08 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Mon, 30 May 2016 13:08:48 +0000 (22:08 +0900)
source/src/qt/avio/movie_saver.cpp
source/src/qt/avio/movie_saver.h
source/src/qt/avio/movie_saver_audio.cpp
source/src/qt/avio/movie_saver_fileio.cpp
source/src/qt/common/qt_utils.cpp
source/src/qt/osd_base.h
source/src/qt/osd_sound.cpp

index 8b9e2b4..38a10e5 100644 (file)
@@ -115,7 +115,7 @@ void MOVIE_SAVER::enqueue_video(QByteArray *p, int width, int height)
        if(!recording) return;
        if(p == NULL) return;
        QByteArray *pp = new QByteArray(p->data(), p->size());
-       //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue video data %d bytes", p->size());
+       AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue video data %d bytes", p->size());
        video_data_queue.enqueue(pp);
        //video_width_queue.enqueue(width);
        //video_height_queue.enqueue(height);
@@ -145,13 +145,13 @@ bool MOVIE_SAVER::dequeue_video(uint32_t *p)
        return true;
 }
 
-void MOVIE_SAVER::enqueue_audio(QByteArray *p)
+void MOVIE_SAVER::enqueue_audio(int16_t *p, int size)
 {
 #if defined(USE_MOVIE_SAVER)
        if(!recording) return;
        if(p == NULL) return;
-       QByteArray *pp = new QByteArray(p->data(), p->size());
-       //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue audio data %d bytes", p->size());
+       QByteArray *pp = new QByteArray((const char *)p, size);
+       //AGAR_DebugLog(AGAR_LOG_DEBUG, "Movie: Enqueue audio data %d bytes", size);
        audio_data_queue.enqueue(pp);
 #endif   
 }
index 47db303..a6e0cf8 100644 (file)
@@ -163,7 +163,7 @@ public:
 public slots:
        void run();
        void enqueue_video(QByteArray *p, int width, int height);
-       void enqueue_audio(QByteArray *p);
+       void enqueue_audio(int16_t *p, int size);
        void do_close();
        bool do_open(QString filename, int);
        void do_set_width(int width);
index 4578646..f218434 100644 (file)
@@ -77,12 +77,6 @@ bool MOVIE_SAVER::open_audio(void)
                return false;
     }
 
-    /* init signal generator */
-    ost->t     = 0;
-    ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
-    /* increment frequency by 110 Hz per second */
-    ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
-
     if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
         nb_samples = 10000;
     else
@@ -144,7 +138,7 @@ void *MOVIE_SAVER::get_audio_frame()
                }
         for (i = 0; i < ost->st->codec->channels; i++) {
             q[offset++] = audio_frame_buf[audio_offset++];
-                       audio_remain--;
+                       audio_remain -= sizeof(int16_t);
                        if(audio_remain <= 0) {
                                audio_offset = 0;
                                break;
@@ -195,7 +189,7 @@ int MOVIE_SAVER::write_audio_frame()
         /* convert samples from native format to destination codec format, using the resampler */
             /* compute destination number of samples */
             dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
-                                            c->sample_rate, c->sample_rate, AV_ROUND_UP);
+                                            c->sample_rate, 48000, AV_ROUND_UP);
             av_assert0(dst_nb_samples == frame->nb_samples);
 
         /* when we pass a frame to the encoder, it may keep a reference to it
@@ -209,14 +203,14 @@ int MOVIE_SAVER::write_audio_frame()
                }
                /* convert to destination format */
                ret = swr_convert(ost->swr_ctx,
-                              ost->frame->data, dst_nb_samples,
-                              (const uint8_t **)frame->data, frame->nb_samples);
-            if (ret < 0) {
-                AGAR_DebugLog(AGAR_LOG_INFO, "Error while converting\n");
-                return -1;
-            }
-            frame = ost->frame;
-
+                                                 ost->frame->data, dst_nb_samples,
+                                                 (const uint8_t **)frame->data, frame->nb_samples);
+               if (ret < 0) {
+                       AGAR_DebugLog(AGAR_LOG_INFO, "Error while converting\n");
+                       return -1;
+               }
+               frame = ost->frame;
+               
         frame->pts = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
         ost->samples_count += dst_nb_samples;
                totalAudioFrame++;
index f72bcdc..996050c 100644 (file)
@@ -76,13 +76,13 @@ bool MOVIE_SAVER::add_stream(void *_ost, void *_oc,
     case AVMEDIA_TYPE_AUDIO:
         c->sample_fmt  = (*codec)->sample_fmts ?
             (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
-        c->bit_rate    = 64000;
-        c->sample_rate = 44100;
+        c->bit_rate    = 128000;
+        c->sample_rate = 48000;
         if ((*codec)->supported_samplerates) {
             c->sample_rate = (*codec)->supported_samplerates[0];
             for (i = 0; (*codec)->supported_samplerates[i]; i++) {
-                if ((*codec)->supported_samplerates[i] == 44100)
-                    c->sample_rate = 44100;
+                if ((*codec)->supported_samplerates[i] == 48000)
+                    c->sample_rate = 48000;
             }
         }
         c->channels        = av_get_channel_layout_nb_channels(c->channel_layout);
index 0d383f0..00e2faa 100644 (file)
@@ -321,7 +321,7 @@ void Ui_MainWindow::LaunchEmuThread(void)
        connect(emu->get_osd(), SIGNAL(sig_movie_set_width(int)), hSaveMovieThread, SLOT(do_set_width(int)));
        connect(emu->get_osd(), SIGNAL(sig_movie_set_height(int)), hSaveMovieThread, SLOT(do_set_height(int)));
    
-       connect(emu->get_osd(), SIGNAL(sig_enqueue_audio(QByteArray *)), hSaveMovieThread, SLOT(enqueue_audio(QByteArray *)));
+       connect(emu->get_osd(), SIGNAL(sig_enqueue_audio(int16_t*, int)), hSaveMovieThread, SLOT(enqueue_audio(int16_t *, int)));
        connect(emu->get_osd(), SIGNAL(sig_enqueue_video(QByteArray *, int, int)), hSaveMovieThread, SLOT(enqueue_video(QByteArray *, int, int)));
        connect(this, SIGNAL(sig_quit_movie_thread()), hSaveMovieThread, SLOT(do_exit()));
 
index 696da1d..8387eaa 100644 (file)
@@ -445,7 +445,7 @@ signals:
        int sig_put_string_debugger(QString);
        int sig_console_input_string(QString);
        int sig_enqueue_video(QByteArray *data, int width, int height); 
-       int sig_enqueue_audio(QByteArray *data);
+       int sig_enqueue_audio(int16_t *data, int size);
        int sig_movie_set_width(int);
        int sig_movie_set_height(int);
        int sig_debugger_finished();
index 875d66c..75949b7 100644 (file)
@@ -209,8 +209,9 @@ void OSD_BASE::update_sound(int* extra_frames)
                        int length = samples * sizeof(int16_t) * 2; // stereo
                        if(now_record_video) {
                                if(sound_samples > rec_sound_buffer_ptr) {
-                                       QByteArray *p = new QByteArray((const char *)(sound_buffer + rec_sound_buffer_ptr * 2), length);
-                                       emit sig_enqueue_audio(p);
+                                       //QByteArray *p = new QByteArray((const char *)(sound_buffer + rec_sound_buffer_ptr * 2), length);
+                                       //emit sig_enqueue_audio(p);
+                                       emit sig_enqueue_audio((int16_t *)(&(sound_buffer[rec_sound_buffer_ptr * 2])), length); 
                                }
                        }
                        // record sound