2010-12-08 04:36:10 +01:00
|
|
|
// Copyright (c) 2009-2010, Niels Martin Hansen
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file audio_controller.h
|
|
|
|
/// @see audio_controller.cpp
|
|
|
|
/// @ingroup audio_ui
|
|
|
|
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
#pragma once
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2012-12-01 20:36:30 +01:00
|
|
|
#include <cstdint>
|
2010-12-08 04:36:10 +01:00
|
|
|
#include <memory>
|
2012-12-01 20:36:30 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
#include <wx/event.h>
|
|
|
|
#include <wx/timer.h>
|
|
|
|
#include <wx/power.h>
|
|
|
|
|
|
|
|
#include <libaegisub/exception.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs_fwd.h>
|
2010-12-31 22:02:42 +01:00
|
|
|
#include <libaegisub/signal.h>
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
class AudioPlayer;
|
|
|
|
class AudioProvider;
|
|
|
|
class AudioTimingController;
|
2012-02-02 00:59:12 +01:00
|
|
|
namespace agi { struct Context; }
|
|
|
|
class TimeRange;
|
2011-09-15 07:16:26 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @class AudioController
|
2012-02-02 00:59:12 +01:00
|
|
|
/// @brief Manage an open audio stream
|
2010-12-08 04:36:10 +01:00
|
|
|
///
|
2010-12-30 23:19:30 +01:00
|
|
|
/// Creates and destroys audio providers and players. This behaviour should at
|
|
|
|
/// some point be moved to a separate class, as it adds too many
|
|
|
|
/// responsibilities to this class, but at the time of writing, it would extend
|
|
|
|
/// the scope of reworking components too much.
|
2010-12-08 04:36:10 +01:00
|
|
|
///
|
2010-12-30 23:19:30 +01:00
|
|
|
/// There is not supposed to be a way to get direct access to the audio
|
|
|
|
/// providers or players owned by a controller. If some operation that isn't
|
|
|
|
/// possible in the existing design is needed, the controller should be
|
|
|
|
/// extended in some way to allow it.
|
2014-03-13 02:39:07 +01:00
|
|
|
class AudioController final : public wxEvtHandler {
|
2011-09-28 21:51:35 +02:00
|
|
|
/// Project context this controller belongs to
|
|
|
|
agi::Context *context;
|
|
|
|
|
|
|
|
/// Slot for subtitles save signal
|
|
|
|
agi::signal::Connection subtitle_save_slot;
|
|
|
|
|
2010-12-08 09:09:16 +01:00
|
|
|
/// A new audio stream was opened (and any previously open was closed)
|
|
|
|
agi::signal::Signal<AudioProvider*> AnnounceAudioOpen;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2010-12-08 09:09:16 +01:00
|
|
|
/// The current audio stream was closed
|
|
|
|
agi::signal::Signal<> AnnounceAudioClose;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2010-12-08 09:09:16 +01:00
|
|
|
/// Playback is in progress and the current position was updated
|
2012-02-02 00:58:58 +01:00
|
|
|
agi::signal::Signal<int> AnnouncePlaybackPosition;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2010-12-08 09:09:16 +01:00
|
|
|
/// Playback has stopped
|
|
|
|
agi::signal::Signal<> AnnouncePlaybackStop;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2010-12-08 09:09:16 +01:00
|
|
|
/// The timing controller was replaced
|
|
|
|
agi::signal::Signal<> AnnounceTimingControllerChanged;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// The audio output object
|
2013-06-08 06:19:40 +02:00
|
|
|
std::unique_ptr<AudioPlayer> player;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// The audio provider
|
2013-06-08 06:19:40 +02:00
|
|
|
std::unique_ptr<AudioProvider> provider;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// The current timing mode, if any; owned by the audio controller
|
2013-01-04 16:01:50 +01:00
|
|
|
std::unique_ptr<AudioTimingController> timing_controller;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2010-12-08 09:09:25 +01:00
|
|
|
/// The URL of the currently open audio, if any
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path audio_url;
|
2010-12-08 09:09:25 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
enum PlaybackMode {
|
|
|
|
PM_NotPlaying,
|
|
|
|
PM_Range,
|
|
|
|
PM_PrimaryRange,
|
|
|
|
PM_ToEnd
|
|
|
|
};
|
|
|
|
/// The current playback mode
|
|
|
|
PlaybackMode playback_mode;
|
|
|
|
|
|
|
|
|
|
|
|
/// Timer used for playback position updates
|
|
|
|
wxTimer playback_timer;
|
|
|
|
|
|
|
|
/// Event handler for the playback timer
|
|
|
|
void OnPlaybackTimer(wxTimerEvent &event);
|
|
|
|
|
2010-12-08 09:09:16 +01:00
|
|
|
/// @brief Timing controller signals primary playback range changed
|
|
|
|
void OnTimingControllerUpdatedPrimaryRange();
|
|
|
|
|
|
|
|
/// @brief Timing controller signals that the rendering style ranges have changed
|
|
|
|
void OnTimingControllerUpdatedStyleRanges();
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2011-09-28 21:51:35 +02:00
|
|
|
/// Subtitles save slot which adds the audio uri to the subtitles
|
|
|
|
void OnSubtitlesSave();
|
|
|
|
|
2011-11-19 02:14:42 +01:00
|
|
|
/// Handler for the current audio player changing
|
|
|
|
void OnAudioPlayerChanged();
|
|
|
|
|
|
|
|
/// Handler for the current audio provider changing
|
|
|
|
void OnAudioProviderChanged();
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
#ifdef wxHAS_POWER_EVENTS
|
|
|
|
/// Handle computer going into suspend mode by stopping audio and closing device
|
|
|
|
void OnComputerSuspending(wxPowerEvent &event);
|
|
|
|
/// Handle computer resuming from suspend by re-opening the audio device
|
|
|
|
void OnComputerResuming(wxPowerEvent &event);
|
|
|
|
#endif
|
|
|
|
|
2012-02-02 00:58:58 +01:00
|
|
|
/// @brief Convert a count of audio samples to a time in milliseconds
|
|
|
|
/// @param samples Sample count to convert
|
|
|
|
/// @return The number of milliseconds equivalent to the sample-count, rounded down
|
|
|
|
int64_t MillisecondsFromSamples(int64_t samples) const;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2012-02-02 00:58:58 +01:00
|
|
|
/// @brief Convert a time in milliseconds to a count of audio samples
|
|
|
|
/// @param ms Time in milliseconds to convert
|
|
|
|
/// @return The index of the first sample that is wholly inside the millisecond
|
|
|
|
int64_t SamplesFromMilliseconds(int64_t ms) const;
|
|
|
|
|
|
|
|
public:
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @brief Constructor
|
2011-09-28 21:51:35 +02:00
|
|
|
AudioController(agi::Context *context);
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Destructor
|
|
|
|
~AudioController();
|
|
|
|
|
|
|
|
/// @brief Open an audio stream
|
|
|
|
/// @param url URL of the stream to open
|
2013-01-04 16:01:50 +01:00
|
|
|
void OpenAudio(agi::fs::path const& url);
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Closes the current audio stream
|
|
|
|
void CloseAudio();
|
|
|
|
|
|
|
|
/// @brief Determine whether audio is currently open
|
|
|
|
/// @return True if an audio stream is open and can be played back
|
|
|
|
bool IsAudioOpen() const;
|
|
|
|
|
|
|
|
/// @brief Get the URL for the current open audio stream
|
|
|
|
/// @return The URL for the audio stream
|
|
|
|
///
|
2010-12-30 23:19:30 +01:00
|
|
|
/// The returned URL can be passed into OpenAudio() later to open the same
|
|
|
|
/// stream again.
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path GetAudioURL() const { return audio_url; }
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
/// @brief Start or restart audio playback, playing a range
|
|
|
|
/// @param range The range of audio to play back
|
|
|
|
///
|
2010-12-30 23:19:30 +01:00
|
|
|
/// The end of the played back range may be requested changed, but is not
|
|
|
|
/// changed automatically from any other operations.
|
2012-02-02 00:58:58 +01:00
|
|
|
void PlayRange(const TimeRange &range);
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Start or restart audio playback, playing the primary playback range
|
|
|
|
///
|
2010-12-30 23:19:30 +01:00
|
|
|
/// If the primary playback range is updated during playback, the end of
|
|
|
|
/// the active playback range will be updated to match the new selection.
|
|
|
|
/// The playback end can not be changed in any other way.
|
2010-12-08 04:36:10 +01:00
|
|
|
void PlayPrimaryRange();
|
|
|
|
|
2011-12-27 03:23:04 +01:00
|
|
|
/// @brief Start or restart audio playback, playing from a point to the end of of the primary playback range
|
2012-02-02 00:58:58 +01:00
|
|
|
/// @param start_ms Time in milliseconds to start playback at
|
2011-12-27 03:23:04 +01:00
|
|
|
///
|
|
|
|
/// This behaves like PlayPrimaryRange, but the start point can differ from
|
|
|
|
/// the beginning of the primary range.
|
2012-02-02 00:58:58 +01:00
|
|
|
void PlayToEndOfPrimary(int start_ms);
|
2011-12-27 03:23:04 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @brief Start or restart audio playback, playing from a point to the end of stream
|
2012-02-02 00:58:58 +01:00
|
|
|
/// @param start_ms Time in milliseconds to start playback at
|
2010-12-08 04:36:10 +01:00
|
|
|
///
|
2010-12-30 23:19:30 +01:00
|
|
|
/// Playback to end cannot be converted to a range playback like range
|
|
|
|
/// playback can, it will continue until the end is reached, it is stopped,
|
|
|
|
/// or restarted.
|
2012-02-02 00:58:58 +01:00
|
|
|
void PlayToEnd(int start_ms);
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Stop all audio playback
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
/// @brief Determine whether playback is ongoing
|
|
|
|
/// @return True if audio is being played back
|
|
|
|
bool IsPlaying();
|
|
|
|
|
|
|
|
/// @brief Get the current playback position
|
2012-02-02 00:58:58 +01:00
|
|
|
/// @return Approximate current time in milliseconds being heard by the user
|
2010-12-08 04:36:10 +01:00
|
|
|
///
|
|
|
|
/// Returns 0 if playback is stopped. The return value is only approximate.
|
2012-02-02 00:58:58 +01:00
|
|
|
int GetPlaybackPosition();
|
|
|
|
|
|
|
|
/// Get the duration of the currently open audio in milliseconds, or 0 if none
|
|
|
|
/// @return Duration in milliseconds
|
|
|
|
int GetDuration() const;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Get the primary playback range
|
2012-02-02 00:58:58 +01:00
|
|
|
/// @return An immutable TimeRange object
|
|
|
|
TimeRange GetPrimaryPlaybackRange() const;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Set the playback audio volume
|
|
|
|
/// @param volume The new amplification factor for the audio
|
|
|
|
void SetVolume(double volume);
|
|
|
|
|
|
|
|
/// @brief Return the current timing controller
|
|
|
|
/// @return The current timing controller or 0
|
2010-12-08 09:09:16 +01:00
|
|
|
AudioTimingController * GetTimingController() const { return timing_controller.get(); }
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Change the current timing controller
|
2010-12-30 23:19:30 +01:00
|
|
|
/// @param new_mode The new timing controller or 0. This may be the same
|
|
|
|
/// object as the current timing controller, to signal that the timing
|
|
|
|
/// controller has changed the object being timed, e.g. changed to a new
|
|
|
|
/// dialogue line.
|
2010-12-08 04:36:10 +01:00
|
|
|
void SetTimingController(AudioTimingController *new_controller);
|
|
|
|
|
2011-01-16 08:18:00 +01:00
|
|
|
/// @brief Save a portion of the decoded loaded audio to a wav file
|
|
|
|
/// @param filename File to save to
|
2012-02-02 00:58:58 +01:00
|
|
|
/// @param range Time range to save
|
2013-01-04 16:01:50 +01:00
|
|
|
void SaveClip(agi::fs::path const& filename, TimeRange const& range) const;
|
2011-01-16 08:18:00 +01:00
|
|
|
|
2010-12-08 09:09:16 +01:00
|
|
|
DEFINE_SIGNAL_ADDERS(AnnounceAudioOpen, AddAudioOpenListener)
|
|
|
|
DEFINE_SIGNAL_ADDERS(AnnounceAudioClose, AddAudioCloseListener)
|
|
|
|
DEFINE_SIGNAL_ADDERS(AnnouncePlaybackPosition, AddPlaybackPositionListener)
|
|
|
|
DEFINE_SIGNAL_ADDERS(AnnouncePlaybackStop, AddPlaybackStopListener)
|
|
|
|
DEFINE_SIGNAL_ADDERS(AnnounceTimingControllerChanged, AddTimingControllerListener)
|
2010-12-08 04:36:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace agi {
|
2012-01-08 02:33:39 +01:00
|
|
|
/// Base class for all audio-related errors
|
|
|
|
DEFINE_BASE_EXCEPTION(AudioError, Exception)
|
|
|
|
|
|
|
|
/// Opening the audio failed for any reason
|
|
|
|
DEFINE_SIMPLE_EXCEPTION(AudioOpenError, AudioError, "audio/open")
|
|
|
|
|
|
|
|
/// There are no audio providers available to open audio files
|
|
|
|
DEFINE_SIMPLE_EXCEPTION(NoAudioProvidersError, AudioOpenError, "audio/open/no_providers")
|
|
|
|
|
|
|
|
/// The file exists, but no providers could find any audio tracks in it
|
|
|
|
DEFINE_SIMPLE_EXCEPTION(AudioDataNotFoundError, AudioOpenError, "audio/open/no_tracks")
|
|
|
|
|
|
|
|
/// There are audio tracks, but no provider could actually read them
|
|
|
|
DEFINE_SIMPLE_EXCEPTION(AudioProviderOpenError, AudioOpenError, "audio/open/provider")
|
|
|
|
|
|
|
|
/// The audio cache failed to initialize
|
|
|
|
DEFINE_SIMPLE_EXCEPTION(AudioCacheOpenError, AudioOpenError, "audio/open/cache")
|
|
|
|
|
|
|
|
/// There are no audio players available
|
|
|
|
DEFINE_SIMPLE_EXCEPTION(NoAudioPlayersError, AudioOpenError, "audio/open/no_players")
|
|
|
|
|
|
|
|
/// The audio player failed to initialize
|
|
|
|
DEFINE_SIMPLE_EXCEPTION(AudioPlayerOpenError, AudioOpenError, "audio/open/player")
|
2011-12-28 22:27:06 +01:00
|
|
|
}
|