11#include " stdafx.h"
22#include " Emu/System.h"
3+ #include " Emu/system_config.h"
34#include " qt_video_source.h"
45
56#include " Loader/ISO.h"
67
8+ #include < QAudioOutput>
79#include < QFile>
810
11+ static video_source* s_audio_source = nullptr ;
12+ static std::unique_ptr<QMediaPlayer> s_audio_player = nullptr ;
13+ static std::unique_ptr<QAudioOutput> s_audio_output = nullptr ;
14+ static std::unique_ptr<QBuffer> s_audio_buffer = nullptr ;
15+ static std::unique_ptr<QByteArray> s_audio_data = nullptr ;
16+
917qt_video_source::qt_video_source ()
1018 : video_source()
1119{
@@ -21,6 +29,11 @@ void qt_video_source::set_video_path(const std::string& video_path)
2129 m_video_path = QString::fromStdString (video_path);
2230}
2331
32+ void qt_video_source::set_audio_path (const std::string& audio_path)
33+ {
34+ m_audio_path = QString::fromStdString (audio_path);
35+ }
36+
2437void qt_video_source::set_iso_path (const std::string& iso_path)
2538{
2639 m_iso_path = iso_path;
@@ -89,7 +102,6 @@ void qt_video_source::init_movie()
89102 m_video_buffer = std::make_unique<QBuffer>(&m_video_data);
90103 m_video_buffer->open (QIODevice::ReadOnly);
91104 m_movie = std::make_unique<QMovie>(m_video_buffer.get ());
92-
93105 }
94106
95107 if (!m_movie->isValid ())
@@ -179,6 +191,8 @@ void qt_video_source::start_movie()
179191 m_media_player->play ();
180192 }
181193
194+ start_audio ();
195+
182196 m_active = true ;
183197}
184198
@@ -196,6 +210,71 @@ void qt_video_source::stop_movie()
196210 m_media_player.reset ();
197211 m_video_buffer.reset ();
198212 m_video_data.clear ();
213+
214+ stop_audio ();
215+ }
216+
217+ void qt_video_source::start_audio ()
218+ {
219+ if (m_audio_path.isEmpty () || s_audio_source == this ) return ;
220+
221+ if (!s_audio_player)
222+ {
223+ s_audio_output = std::make_unique<QAudioOutput>();
224+ s_audio_player = std::make_unique<QMediaPlayer>();
225+ s_audio_player->setAudioOutput (s_audio_output.get ());
226+ }
227+
228+ if (m_iso_path.empty ())
229+ {
230+ s_audio_player->setSource (QUrl::fromLocalFile (m_audio_path));
231+ }
232+ else
233+ {
234+ iso_archive archive (m_iso_path);
235+ auto audio_file = archive.open (m_audio_path.toStdString ());
236+ const auto audio_size = audio_file.size ();
237+ if (audio_size == 0 ) return ;
238+
239+ auto old_audio_data = std::move (s_audio_data);
240+ s_audio_data = std::make_unique<QByteArray>(audio_size, 0 );
241+ audio_file.read (s_audio_data->data (), audio_size);
242+
243+ if (!s_audio_buffer)
244+ {
245+ s_audio_buffer = std::make_unique<QBuffer>();
246+ }
247+
248+ s_audio_buffer->setBuffer (s_audio_data.get ());
249+ s_audio_buffer->open (QIODevice::ReadOnly);
250+ s_audio_player->setSourceDevice (s_audio_buffer.get ());
251+
252+ if (old_audio_data)
253+ {
254+ old_audio_data.reset ();
255+ }
256+ }
257+
258+ s_audio_output->setVolume (g_cfg.audio .volume .get () / 100 .0f );
259+ s_audio_player->play ();
260+ s_audio_source = this ;
261+ }
262+
263+ void qt_video_source::stop_audio ()
264+ {
265+ if (s_audio_source != this ) return ;
266+
267+ s_audio_source = nullptr ;
268+
269+ if (s_audio_player)
270+ {
271+ s_audio_player->stop ();
272+ s_audio_player.reset ();
273+ }
274+
275+ s_audio_output.reset ();
276+ s_audio_buffer.reset ();
277+ s_audio_data.reset ();
199278}
200279
201280QPixmap qt_video_source::get_movie_image (const QVideoFrame& frame) const
@@ -288,6 +367,14 @@ void qt_video_source_wrapper::set_video_path(const std::string& video_path)
288367 });
289368}
290369
370+ void qt_video_source_wrapper::set_audio_path (const std::string& audio_path)
371+ {
372+ Emu.CallFromMainThread ([this , path = audio_path]()
373+ {
374+ // TODO
375+ });
376+ }
377+
291378void qt_video_source_wrapper::set_active (bool active)
292379{
293380 Emu.CallFromMainThread ([this , active]()
0 commit comments