Initial commit
This commit is contained in:
commit
acadc92e68
780 files changed
+468285
No files matched your search
@@ -0,0 +1,169 @@
|
||||
# clang-format 14
|
||||
|
||||
Language: Cpp
|
||||
Standard: c++20
|
||||
|
||||
AccessModifierOffset: -2 #?
|
||||
AlignAfterOpenBracket: AlwaysBreak
|
||||
AlignArrayOfStructures: Left
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveBitFields: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignConsecutiveMacros: true
|
||||
AlignEscapedNewlines: DontAlign
|
||||
AlignOperands: AlignAfterOperator
|
||||
AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: true
|
||||
AllowAllConstructorInitializersOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLambdasOnASingleLine: Inline
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
AttributeMacros: []
|
||||
BinPackArguments: true
|
||||
BinPackParameters: false
|
||||
BitFieldColonSpacing: Both
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: true
|
||||
AfterClass: false
|
||||
AfterControlStatement: MultiLine
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
#AfterObjCDeclaration
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
BeforeLambdaBody: true
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: false
|
||||
SplitEmptyNamespace: true
|
||||
#BreakAfterJavaFieldAnnotations
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeBraces: Custom
|
||||
BreakBeforeConceptDeclarations: true
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializers: BeforeComma
|
||||
BreakInheritanceList: BeforeComma
|
||||
BreakStringLiterals: false
|
||||
ColumnLimit: 0
|
||||
CommentPragmas: '' #?
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 2 #?
|
||||
ContinuationIndentWidth: 2 #?
|
||||
Cpp11BracedListStyle: true
|
||||
DeriveLineEnding: true
|
||||
DerivePointerAlignment: false
|
||||
EmptyLineAfterAccessModifier: Leave
|
||||
EmptyLineBeforeAccessModifier: Leave
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros: []
|
||||
IfMacros: ['MPT_MAYBE_CONSTANT_IF']
|
||||
IncludeBlocks: Preserve
|
||||
IncludeCategories: [] #?
|
||||
IncludeIsMainRegex: '' #?
|
||||
IncludeIsMainSourceRegex: '' #?
|
||||
IndentAccessModifiers: false
|
||||
IndentCaseBlocks: true
|
||||
IndentCaseLabels: true
|
||||
IndentExternBlock: NoIndent
|
||||
IndentGotoLabels: false
|
||||
IndentPPDirectives: None
|
||||
#IndentRequiresClause: true
|
||||
#BeforeHash
|
||||
IndentWidth: 2
|
||||
IndentWrappedFunctionNames: true
|
||||
InsertTrailingCommas: None
|
||||
#JavaImportGroups
|
||||
#JavaScriptQuotes
|
||||
#JavaScriptWrapImports
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
LambdaBodyIndentation: OuterScope
|
||||
MacroBlockBegin: '' #?
|
||||
MacroBlockEnd: '' #?
|
||||
MaxEmptyLinesToKeep: 3
|
||||
NamespaceIndentation: None
|
||||
NamespaceMacros: [] #?
|
||||
#ObjCBinPackProtocolList
|
||||
#ObjCBlockIndentWidth
|
||||
#ObjCBreakBeforeNestedBlockParam
|
||||
#ObjCSpaceAfterProperty
|
||||
#ObjCSpaceBeforeProtocolList
|
||||
PackConstructorInitializers: Never
|
||||
#PenaltyBreakAssignment
|
||||
#PenaltyBreakBeforeFirstCallParameter
|
||||
#PenaltyBreakComment
|
||||
#PenaltyBreakFirstLessLess
|
||||
#PenaltyBreakOpenParenthesis
|
||||
#PenaltyBreakString
|
||||
#PenaltyBreakTemplateDeclaration
|
||||
#PenaltyExcessCharacter
|
||||
#PenaltyIndentedWhitespace
|
||||
#PenaltyReturnTypeOnItsOwnLine
|
||||
PointerAlignment: Middle
|
||||
PPIndentWidth: -1
|
||||
#RawStringFormats
|
||||
QualifierAlignment: Leave
|
||||
#QualifierOrder: ['static', 'inline', 'constexpr', 'volatile', 'const', 'restrict', 'type']
|
||||
ReferenceAlignment: Pointer
|
||||
ReflowComments: false
|
||||
RemoveBracesLLVM: false
|
||||
SeparateDefinitionBlocks: Leave
|
||||
ShortNamespaceLines: 1
|
||||
SortIncludes: false
|
||||
#SortJavaStaticImport
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: true
|
||||
AfterForeachMacros: true
|
||||
AfterFunctionDeclarationName: false
|
||||
AfterFunctionDefinitionName: false
|
||||
AfterIfMacros: true
|
||||
AfterOverloadedOperator: false
|
||||
#AfterRequiresInClause: false
|
||||
#AfterRequiresInExpression: false
|
||||
BeforeNonEmptyParentheses: false
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceBeforeSquareBrackets: false
|
||||
SpaceInEmptyBlock: true
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 2
|
||||
SpacesInAngles: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInConditionalStatement: true
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInLineCommentPrefix:
|
||||
Minimum: 1
|
||||
Maximum: -1
|
||||
SpacesInParentheses: true
|
||||
SpacesInSquareBrackets: false
|
||||
StatementAttributeLikeMacros: []
|
||||
StatementMacros: [ 'MPT_WARNING', 'MPT_TEST_GROUP_INLINE_IDENTIFIER', 'MPT_TEST_GROUP_INLINE', 'MPT_TEST_GROUP_STATIC' ] #?
|
||||
TabWidth: 2
|
||||
TypenameMacros: [] #?
|
||||
UseCRLF: false
|
||||
UseTab: ForContinuationAndIndentation
|
||||
WhitespaceSensitiveMacros:
|
||||
- MPT_PP_STRINGIFY
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,546 @@
|
||||
/*
|
||||
* openmpt123.hpp
|
||||
* --------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_HPP
|
||||
#define OPENMPT123_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
|
||||
#include "openmpt123_exception.hpp"
|
||||
#include "openmpt123_terminal.hpp"
|
||||
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/float.hpp"
|
||||
#include "mpt/base/math.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/preprocessor.hpp"
|
||||
#include "mpt/base/saturate_round.hpp"
|
||||
#include "mpt/exception/exception_text.hpp"
|
||||
#include "mpt/format/concat.hpp"
|
||||
#include "mpt/format/message.hpp"
|
||||
#include "mpt/format/message_macros.hpp"
|
||||
#include "mpt/format/simple.hpp"
|
||||
#include "mpt/io_file/fstream.hpp"
|
||||
#include "mpt/parse/parse.hpp"
|
||||
#include "mpt/path/native_path.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string/utility.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <libopenmpt/libopenmpt.hpp>
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
template <>
|
||||
struct make_string_type<mpt::native_path> {
|
||||
using type = mpt::native_path;
|
||||
};
|
||||
|
||||
|
||||
template <>
|
||||
struct is_string_type<mpt::native_path> : public std::true_type { };
|
||||
|
||||
template <>
|
||||
struct string_transcoder<mpt::native_path> {
|
||||
using string_type = mpt::native_path;
|
||||
static inline mpt::widestring decode( const string_type & src ) {
|
||||
return mpt::transcode<mpt::widestring>( src.AsNative() );
|
||||
}
|
||||
static inline string_type encode( const mpt::widestring & src ) {
|
||||
return mpt::native_path::FromNative( mpt::transcode<mpt::native_path::raw_path_type>( src ) );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
struct show_help_exception {
|
||||
mpt::ustring message;
|
||||
bool longhelp;
|
||||
show_help_exception( const mpt::ustring & msg = MPT_USTRING(""), bool longhelp_ = true ) : message(msg), longhelp(longhelp_) { }
|
||||
};
|
||||
|
||||
struct args_nofiles_exception {
|
||||
args_nofiles_exception() = default;
|
||||
};
|
||||
|
||||
struct args_error_exception {
|
||||
args_error_exception() = default;
|
||||
};
|
||||
|
||||
struct show_help_keyboard_exception { };
|
||||
|
||||
|
||||
template <typename Tstring, typename Tchar, typename T>
|
||||
inline Tstring align_right( const Tchar pad, std::size_t width, const T val ) {
|
||||
assert( Tstring( 1, pad ).length() == 1 );
|
||||
Tstring str = mpt::default_formatter::template format<Tstring>( val );
|
||||
if ( width > str.length() ) {
|
||||
str.insert( str.begin(), width - str.length(), pad );
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
struct field {
|
||||
mpt::ustring key;
|
||||
mpt::ustring val;
|
||||
};
|
||||
|
||||
|
||||
inline mpt::ustring append_software_tag( mpt::ustring software ) {
|
||||
mpt::ustring openmpt123 = mpt::ustring()
|
||||
+ MPT_USTRING("openmpt123 ")
|
||||
+ mpt::transcode<mpt::ustring>( mpt::source_encoding, OPENMPT123_VERSION_STRING )
|
||||
+ MPT_USTRING(" (libopenmpt ")
|
||||
+ mpt::transcode<mpt::ustring>( mpt::common_encoding::utf8, openmpt::string::get( "library_version" ) )
|
||||
+ MPT_USTRING(", OpenMPT ")
|
||||
+ mpt::transcode<mpt::ustring>( mpt::common_encoding::utf8, openmpt::string::get( "core_version" ) )
|
||||
+ MPT_USTRING(")")
|
||||
;
|
||||
if ( software.empty() ) {
|
||||
software = openmpt123;
|
||||
} else {
|
||||
software += MPT_USTRING(" (via ") + openmpt123 + MPT_USTRING(")");
|
||||
}
|
||||
return software;
|
||||
}
|
||||
|
||||
inline mpt::ustring get_encoder_tag() {
|
||||
return mpt::ustring()
|
||||
+ MPT_USTRING("openmpt123 ")
|
||||
+ mpt::transcode<mpt::ustring>( mpt::source_encoding, OPENMPT123_VERSION_STRING )
|
||||
+ MPT_USTRING(" (libopenmpt ")
|
||||
+ mpt::transcode<mpt::ustring>( mpt::common_encoding::utf8, openmpt::string::get( "library_version" ) )
|
||||
+ MPT_USTRING(", OpenMPT ")
|
||||
+ mpt::transcode<mpt::ustring>( mpt::common_encoding::utf8, openmpt::string::get( "core_version" ) )
|
||||
+ MPT_USTRING(")");
|
||||
}
|
||||
|
||||
inline mpt::native_path get_extension( mpt::native_path filename ) {
|
||||
mpt::native_path tmp = filename.GetFilenameExtension();
|
||||
if ( !tmp.empty() ) {
|
||||
tmp = mpt::native_path::FromNative( tmp.AsNative().substr( 1 ) );
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
enum class Mode {
|
||||
None,
|
||||
Probe,
|
||||
Info,
|
||||
UI,
|
||||
Batch,
|
||||
Render
|
||||
};
|
||||
|
||||
inline mpt::ustring mode_to_string( Mode mode ) {
|
||||
switch ( mode ) {
|
||||
case Mode::None: return MPT_USTRING("none"); break;
|
||||
case Mode::Probe: return MPT_USTRING("probe"); break;
|
||||
case Mode::Info: return MPT_USTRING("info"); break;
|
||||
case Mode::UI: return MPT_USTRING("ui"); break;
|
||||
case Mode::Batch: return MPT_USTRING("batch"); break;
|
||||
case Mode::Render: return MPT_USTRING("render"); break;
|
||||
}
|
||||
return MPT_USTRING("");
|
||||
}
|
||||
|
||||
inline const std::int32_t default_low = -2;
|
||||
inline const std::int32_t default_high = -1;
|
||||
|
||||
enum verbosity : std::int8_t {
|
||||
verbosity_shortversion = -1,
|
||||
verbosity_hidden = 0,
|
||||
verbosity_normal = 1,
|
||||
verbosity_verbose = 2,
|
||||
};
|
||||
|
||||
struct commandlineflags {
|
||||
|
||||
Mode mode = Mode::UI;
|
||||
std::int32_t ui_redraw_interval = default_high;
|
||||
mpt::ustring driver = MPT_USTRING("");
|
||||
mpt::ustring device = MPT_USTRING("");
|
||||
std::int32_t buffer = default_high;
|
||||
std::int32_t period = default_high;
|
||||
std::int32_t samplerate = MPT_OS_DJGPP ? 44100 : 48000;
|
||||
std::int32_t channels = 2;
|
||||
std::int32_t gain = 0;
|
||||
std::int32_t separation = 100;
|
||||
std::int32_t filtertaps = 8;
|
||||
std::int32_t ramping = -1; // ramping strength : -1:default 0:off 1 2 3 4 5 // roughly milliseconds
|
||||
std::int32_t tempo = 0;
|
||||
std::int32_t pitch = 0;
|
||||
std::int32_t dither = 1;
|
||||
std::int32_t repeatcount = 0;
|
||||
std::int32_t subsong = -1;
|
||||
std::map<std::string, std::string> ctls = {};
|
||||
double seek_target = 0.0;
|
||||
double end_time = 0.0;
|
||||
bool quiet = false;
|
||||
verbosity banner = verbosity_normal;
|
||||
bool verbose = false;
|
||||
bool assume_terminal = false;
|
||||
int terminal_width = -1;
|
||||
int terminal_height = -1;
|
||||
bool show_details = true;
|
||||
bool show_message = false;
|
||||
bool show_ui = true;
|
||||
bool show_progress = true;
|
||||
bool show_meters = true;
|
||||
bool show_channel_meters = false;
|
||||
bool show_pattern = false;
|
||||
bool use_float = MPT_OS_DJGPP ? false : mpt::float_traits<float>::is_hard && mpt::float_traits<float>::is_ieee754_binary;
|
||||
bool use_stdout = false;
|
||||
bool randomize = false;
|
||||
bool shuffle = false;
|
||||
bool restart = false;
|
||||
std::size_t playlist_index = 0;
|
||||
std::vector<mpt::native_path> filenames = {};
|
||||
mpt::native_path output_filename = MPT_NATIVE_PATH("");
|
||||
mpt::native_path output_extension = MPT_NATIVE_PATH("auto");
|
||||
bool force_overwrite = false;
|
||||
bool paused = false;
|
||||
mpt::ustring warnings = MPT_USTRING("");
|
||||
|
||||
void apply_default_buffer_sizes() {
|
||||
if ( ui_redraw_interval == default_high ) {
|
||||
ui_redraw_interval = 50;
|
||||
} else if ( ui_redraw_interval == default_low ) {
|
||||
ui_redraw_interval = 10;
|
||||
}
|
||||
if ( buffer == default_high ) {
|
||||
buffer = 250;
|
||||
} else if ( buffer == default_low ) {
|
||||
buffer = 50;
|
||||
}
|
||||
if ( period == default_high ) {
|
||||
period = 50;
|
||||
} else if ( period == default_low ) {
|
||||
period = 10;
|
||||
}
|
||||
}
|
||||
|
||||
void check_and_sanitize() {
|
||||
bool canUI = true;
|
||||
bool canProgress = true;
|
||||
if ( !assume_terminal ) {
|
||||
#if MPT_OS_WINDOWS
|
||||
canUI = IsTerminal( 0 ) ? true : false;
|
||||
canProgress = IsTerminal( 2 ) ? true : false;
|
||||
#else // !MPT_OS_WINDOWS
|
||||
canUI = isatty( STDIN_FILENO ) ? true : false;
|
||||
canProgress = isatty( STDERR_FILENO ) ? true : false;
|
||||
#endif // MPT_OS_WINDOWS
|
||||
}
|
||||
query_terminal_size( terminal_width, terminal_height );
|
||||
if ( filenames.size() == 0 ) {
|
||||
throw args_nofiles_exception();
|
||||
}
|
||||
if ( use_stdout && ( device != commandlineflags().device || !output_filename.empty() ) ) {
|
||||
throw args_error_exception();
|
||||
}
|
||||
if ( !output_filename.empty() && ( device != commandlineflags().device || use_stdout ) ) {
|
||||
throw args_error_exception();
|
||||
}
|
||||
for ( const auto & filename : filenames ) {
|
||||
if ( filename == MPT_NATIVE_PATH("-") ) {
|
||||
canUI = false;
|
||||
}
|
||||
}
|
||||
show_ui = canUI;
|
||||
if ( !canProgress ) {
|
||||
show_progress = false;
|
||||
}
|
||||
if ( !canUI || !canProgress ) {
|
||||
show_meters = false;
|
||||
}
|
||||
if ( mode == Mode::None ) {
|
||||
if ( canUI ) {
|
||||
mode = Mode::UI;
|
||||
} else {
|
||||
mode = Mode::Batch;
|
||||
}
|
||||
}
|
||||
if ( mode == Mode::UI && !canUI ) {
|
||||
throw args_error_exception();
|
||||
}
|
||||
if ( show_progress && !canProgress ) {
|
||||
throw args_error_exception();
|
||||
}
|
||||
switch ( mode ) {
|
||||
case Mode::None:
|
||||
throw args_error_exception();
|
||||
break;
|
||||
case Mode::Probe:
|
||||
show_ui = false;
|
||||
show_progress = false;
|
||||
show_meters = false;
|
||||
show_channel_meters = false;
|
||||
show_pattern = false;
|
||||
break;
|
||||
case Mode::Info:
|
||||
show_ui = false;
|
||||
show_progress = false;
|
||||
show_meters = false;
|
||||
show_channel_meters = false;
|
||||
show_pattern = false;
|
||||
break;
|
||||
case Mode::UI:
|
||||
break;
|
||||
case Mode::Batch:
|
||||
show_meters = false;
|
||||
show_channel_meters = false;
|
||||
show_pattern = false;
|
||||
break;
|
||||
case Mode::Render:
|
||||
show_meters = false;
|
||||
show_channel_meters = false;
|
||||
show_pattern = false;
|
||||
show_ui = false;
|
||||
break;
|
||||
}
|
||||
if ( quiet ) {
|
||||
verbose = false;
|
||||
show_ui = false;
|
||||
show_details = false;
|
||||
show_progress = false;
|
||||
show_channel_meters = false;
|
||||
}
|
||||
if ( verbose ) {
|
||||
show_details = true;
|
||||
}
|
||||
if ( channels != 1 && channels != 2 && channels != 4 ) {
|
||||
channels = commandlineflags().channels;
|
||||
}
|
||||
if ( samplerate < 0 ) {
|
||||
samplerate = commandlineflags().samplerate;
|
||||
}
|
||||
if ( output_extension == MPT_NATIVE_PATH("auto") ) {
|
||||
output_extension = MPT_NATIVE_PATH("");
|
||||
}
|
||||
if ( mode != Mode::Render && !output_extension.empty() ) {
|
||||
throw args_error_exception();
|
||||
}
|
||||
if ( mode == Mode::Render && !output_filename.empty() ) {
|
||||
throw args_error_exception();
|
||||
}
|
||||
if ( mode != Mode::Render && !output_filename.empty() ) {
|
||||
output_extension = get_extension( output_filename );
|
||||
}
|
||||
if ( output_extension.empty() ) {
|
||||
output_extension = MPT_NATIVE_PATH("wav");
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template < typename Tsample > Tsample convert_sample_to( float val );
|
||||
template < > float convert_sample_to( float val ) {
|
||||
return val;
|
||||
}
|
||||
template < > std::int16_t convert_sample_to( float val ) {
|
||||
std::int32_t tmp = static_cast<std::int32_t>( val * 32768.0f );
|
||||
tmp = std::min( tmp, std::int32_t( 32767 ) );
|
||||
tmp = std::max( tmp, std::int32_t( -32768 ) );
|
||||
return static_cast<std::int16_t>( tmp );
|
||||
}
|
||||
|
||||
class write_buffers_interface {
|
||||
public:
|
||||
virtual ~write_buffers_interface() {
|
||||
return;
|
||||
}
|
||||
virtual void write_metadata( std::map<mpt::ustring, mpt::ustring> metadata ) {
|
||||
(void)metadata;
|
||||
return;
|
||||
}
|
||||
virtual void write_updated_metadata( std::map<mpt::ustring, mpt::ustring> metadata ) {
|
||||
(void)metadata;
|
||||
return;
|
||||
}
|
||||
virtual void write( const std::vector<float*> buffers, std::size_t frames ) = 0;
|
||||
virtual void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) = 0;
|
||||
virtual bool pause() {
|
||||
return false;
|
||||
}
|
||||
virtual bool unpause() {
|
||||
return false;
|
||||
}
|
||||
virtual bool sleep( int /*ms*/ ) {
|
||||
return false;
|
||||
}
|
||||
virtual bool is_dummy() const {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class write_buffers_polling_wrapper : public write_buffers_interface {
|
||||
protected:
|
||||
std::size_t channels;
|
||||
std::size_t sampleQueueMaxFrames;
|
||||
std::deque<float> sampleQueue;
|
||||
protected:
|
||||
virtual ~write_buffers_polling_wrapper() {
|
||||
return;
|
||||
}
|
||||
protected:
|
||||
write_buffers_polling_wrapper( const commandlineflags & flags )
|
||||
: channels(flags.channels)
|
||||
, sampleQueueMaxFrames(0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void set_queue_size_frames( std::size_t frames ) {
|
||||
sampleQueueMaxFrames = frames;
|
||||
}
|
||||
template < typename Tsample >
|
||||
Tsample pop_queue() {
|
||||
float val = 0.0f;
|
||||
if ( !sampleQueue.empty() ) {
|
||||
val = sampleQueue.front();
|
||||
sampleQueue.pop_front();
|
||||
}
|
||||
return convert_sample_to<Tsample>( val );
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleQueue.push_back( buffers[channel][frame] );
|
||||
}
|
||||
while ( sampleQueue.size() >= sampleQueueMaxFrames * channels ) {
|
||||
while ( !forward_queue() ) {
|
||||
sleep( 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleQueue.push_back( buffers[channel][frame] * (1.0f/32768.0f) );
|
||||
}
|
||||
while ( sampleQueue.size() >= sampleQueueMaxFrames * channels ) {
|
||||
while ( !forward_queue() ) {
|
||||
sleep( 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual bool forward_queue() = 0;
|
||||
bool sleep( int ms ) override = 0;
|
||||
};
|
||||
|
||||
class write_buffers_polling_wrapper_int : public write_buffers_interface {
|
||||
protected:
|
||||
std::size_t channels;
|
||||
std::size_t sampleQueueMaxFrames;
|
||||
std::deque<std::int16_t> sampleQueue;
|
||||
protected:
|
||||
virtual ~write_buffers_polling_wrapper_int() {
|
||||
return;
|
||||
}
|
||||
protected:
|
||||
write_buffers_polling_wrapper_int( const commandlineflags & flags )
|
||||
: channels(flags.channels)
|
||||
, sampleQueueMaxFrames(0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void set_queue_size_frames( std::size_t frames ) {
|
||||
sampleQueueMaxFrames = frames;
|
||||
}
|
||||
std::int16_t pop_queue() {
|
||||
std::int16_t val = 0;
|
||||
if ( !sampleQueue.empty() ) {
|
||||
val = sampleQueue.front();
|
||||
sampleQueue.pop_front();
|
||||
}
|
||||
return val;
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleQueue.push_back( convert_sample_to<std::int16_t>( buffers[channel][frame] ) );
|
||||
}
|
||||
while ( sampleQueue.size() >= sampleQueueMaxFrames * channels ) {
|
||||
while ( !forward_queue() ) {
|
||||
sleep( 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleQueue.push_back( buffers[channel][frame] );
|
||||
}
|
||||
while ( sampleQueue.size() >= sampleQueueMaxFrames * channels ) {
|
||||
while ( !forward_queue() ) {
|
||||
sleep( 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual bool forward_queue() = 0;
|
||||
bool sleep( int ms ) override = 0;
|
||||
};
|
||||
|
||||
class void_audio_stream : public write_buffers_interface {
|
||||
public:
|
||||
virtual ~void_audio_stream() {
|
||||
return;
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
(void)buffers;
|
||||
(void)frames;
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
(void)buffers;
|
||||
(void)frames;
|
||||
}
|
||||
bool is_dummy() const override {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class file_audio_stream_base : public write_buffers_interface {
|
||||
protected:
|
||||
file_audio_stream_base() {
|
||||
return;
|
||||
}
|
||||
public:
|
||||
void write_metadata( std::map<mpt::ustring, mpt::ustring> metadata ) override {
|
||||
(void)metadata;
|
||||
return;
|
||||
}
|
||||
void write_updated_metadata( std::map<mpt::ustring, mpt::ustring> metadata ) override {
|
||||
(void)metadata;
|
||||
return;
|
||||
}
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override = 0;
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override = 0;
|
||||
virtual ~file_audio_stream_base() {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // OPENMPT123_HPP
|
||||
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* openmpt123_allegro42.hpp
|
||||
* ------------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_ALLEGRO42_HPP
|
||||
#define OPENMPT123_ALLEGRO42_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#if defined(MPT_WITH_ALLEGRO42)
|
||||
|
||||
#if defined(__GNUC__) && !defined(__clang__) && !defined(_MSC_VER)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
#pragma GCC diagnostic ignored "-Wfloat-conversion"
|
||||
#pragma GCC diagnostic ignored "-Wdouble-promotion"
|
||||
#endif
|
||||
#include <allegro.h>
|
||||
#if defined(__GNUC__) && !defined(__clang__) && !defined(_MSC_VER)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
inline constexpr auto allegro42_encoding = mpt::logical_encoding::locale;
|
||||
|
||||
struct allegro42_exception : public exception {
|
||||
static mpt::ustring error_to_string() {
|
||||
try {
|
||||
return mpt::transcode<mpt::ustring>( allegro42_encoding, std::string( allegro_error ) );
|
||||
} catch ( const std::bad_alloc & ) {
|
||||
return mpt::ustring();
|
||||
}
|
||||
}
|
||||
allegro42_exception()
|
||||
: exception( MPT_USTRING("Allegro-4.2: ") + error_to_string() )
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class allegro42_raii {
|
||||
public:
|
||||
allegro42_raii() {
|
||||
if ( allegro_init() != 0 ) {
|
||||
throw allegro42_exception();
|
||||
}
|
||||
}
|
||||
~allegro42_raii() {
|
||||
allegro_exit();
|
||||
}
|
||||
};
|
||||
|
||||
class allegro42_sound_raii {
|
||||
public:
|
||||
allegro42_sound_raii() {
|
||||
if ( install_sound( DIGI_AUTODETECT, MIDI_NONE, NULL ) != 0 ) {
|
||||
throw allegro42_exception();
|
||||
}
|
||||
if ( digi_card == DIGI_NONE ) {
|
||||
remove_sound();
|
||||
throw exception( MPT_USTRING("no audio device found") );
|
||||
}
|
||||
}
|
||||
~allegro42_sound_raii() {
|
||||
remove_sound();
|
||||
}
|
||||
};
|
||||
|
||||
class allegro42_stream_raii : public write_buffers_polling_wrapper_int {
|
||||
private:
|
||||
allegro42_raii allegro;
|
||||
allegro42_sound_raii allegro_sound;
|
||||
AUDIOSTREAM * stream;
|
||||
std::size_t bits;
|
||||
std::size_t channels;
|
||||
std::uint32_t period_frames;
|
||||
private:
|
||||
std::uint32_t round_up_power2(std::uint32_t x)
|
||||
{
|
||||
std::uint32_t result = 1;
|
||||
while ( result < x ) {
|
||||
result *= 2;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public:
|
||||
allegro42_stream_raii( commandlineflags & flags, concat_stream<mpt::ustring> & log )
|
||||
: write_buffers_polling_wrapper_int(flags)
|
||||
, stream(NULL)
|
||||
, bits(16)
|
||||
, channels(flags.channels)
|
||||
, period_frames(1024)
|
||||
{
|
||||
if ( flags.use_float ) {
|
||||
throw exception( MPT_USTRING("floating point is unsupported") );
|
||||
}
|
||||
if ( ( flags.channels != 1 ) && ( flags.channels != 2 ) ) {
|
||||
throw exception( MPT_USTRING("only mono or stereo supported") );
|
||||
}
|
||||
if ( flags.buffer == default_high ) {
|
||||
flags.buffer = 1024 * 2 * 1000 / flags.samplerate;
|
||||
} else if ( flags.buffer == default_low ) {
|
||||
flags.buffer = 512 * 2 * 1000 / flags.samplerate;
|
||||
}
|
||||
if ( flags.period == default_high ) {
|
||||
flags.period = 1024 / 2 * 1000 / flags.samplerate;
|
||||
} else if ( flags.period == default_low ) {
|
||||
flags.period = 512 / 2 * 1000 / flags.samplerate;
|
||||
}
|
||||
flags.apply_default_buffer_sizes();
|
||||
period_frames = round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) );
|
||||
set_queue_size_frames( period_frames );
|
||||
if ( flags.verbose ) {
|
||||
log << MPT_USTRING("Allegro-4.2:") << lf;
|
||||
log << MPT_USTRING(" allegro samplerate: ") << get_mixer_frequency() << lf;
|
||||
log << MPT_USTRING(" latency: ") << flags.buffer << lf;
|
||||
log << MPT_USTRING(" period: ") << flags.period << lf;
|
||||
log << MPT_USTRING(" frames per buffer: ") << period_frames << lf;
|
||||
log << MPT_USTRING(" ui redraw: ") << flags.ui_redraw_interval << lf;
|
||||
}
|
||||
stream = play_audio_stream( period_frames, 16, ( flags.channels > 1 ) ? TRUE : FALSE, flags.samplerate, 255, 128 );
|
||||
if ( !stream ) {
|
||||
bits = 8;
|
||||
stream = play_audio_stream( period_frames, 8, ( flags.channels > 1 ) ? TRUE : FALSE, flags.samplerate, 255, 128 );
|
||||
if ( !stream ) {
|
||||
throw allegro42_exception();
|
||||
}
|
||||
}
|
||||
}
|
||||
~allegro42_stream_raii() {
|
||||
if ( stream ) {
|
||||
stop_audio_stream( stream );
|
||||
stream = NULL;
|
||||
}
|
||||
}
|
||||
public:
|
||||
bool forward_queue() override {
|
||||
void * p = get_audio_stream_buffer( stream );
|
||||
if ( !p ) {
|
||||
return false;
|
||||
}
|
||||
for ( std::size_t frame = 0; frame < period_frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
std::int16_t sample = pop_queue();
|
||||
if ( bits == 8 ) {
|
||||
std::uint8_t u8sample = ( static_cast<std::uint16_t>( sample ) + 0x8000u ) >> 8;
|
||||
std::memcpy( reinterpret_cast<unsigned char *>( p ) + ( ( ( frame * channels ) + channel ) * sizeof( std::uint8_t ) ), &u8sample, sizeof( std::uint8_t ) );
|
||||
} else {
|
||||
std::uint16_t u16sample = static_cast<std::uint16_t>( sample ) + 0x8000u;
|
||||
std::memcpy( reinterpret_cast<unsigned char *>( p ) + ( ( ( frame * channels ) + channel ) * sizeof( std::uint16_t ) ), &u16sample, sizeof( std::uint16_t ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
free_audio_stream_buffer( stream );
|
||||
return true;
|
||||
}
|
||||
bool unpause() override {
|
||||
voice_start( stream->voice );
|
||||
return true;
|
||||
}
|
||||
bool pause() override {
|
||||
voice_stop( stream->voice );
|
||||
return true;
|
||||
}
|
||||
bool sleep( int ms ) override {
|
||||
rest( ms );
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
inline std::vector<mpt::ustring> show_allegro42_devices( concat_stream<mpt::ustring> & /* log */ ) {
|
||||
string_concat_stream<mpt::ustring> device;
|
||||
device << MPT_USTRING("0") << MPT_USTRING(": Default Device");
|
||||
return { device.str() };
|
||||
}
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_WITH_ALLEGRO42
|
||||
|
||||
#endif // OPENMPT123_ALLEGRO42_HPP
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* openmpt123_config.hpp
|
||||
* ---------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_CONFIG_HPP
|
||||
#define OPENMPT123_CONFIG_HPP
|
||||
|
||||
#define MPT_INLINE_NS mpt_openmpt123
|
||||
|
||||
#define OPENMPT123_VERSION_STRING OPENMPT_API_VERSION_STRING
|
||||
|
||||
#endif // OPENMPT123_CONFIG_HPP
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* openmpt123_exception.hpp
|
||||
* ------------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_EXCEPTION_HPP
|
||||
#define OPENMPT123_EXCEPTION_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <libopenmpt/libopenmpt.hpp>
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
struct exception : public openmpt::exception {
|
||||
exception( const mpt::ustring & text ) : openmpt::exception(mpt::transcode<std::string>( mpt::common_encoding::utf8, text )) { }
|
||||
};
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // OPENMPT123_EXCEPTION_HPP
|
||||
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* openmpt123_flac.hpp
|
||||
* -------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_FLAC_HPP
|
||||
#define OPENMPT123_FLAC_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#if defined(MPT_WITH_FLAC)
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/saturate_round.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#if MPT_PLATFORM_MULTITHREADED && !defined(MPT_LIBCXX_QUIRK_NO_STD_THREAD)
|
||||
#include <thread>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && defined(__clang__) && defined(__c2__)
|
||||
#include <sys/types.h>
|
||||
#if __STDC__
|
||||
typedef _off_t off_t;
|
||||
#endif
|
||||
#endif
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wreserved-id-macro"
|
||||
#endif
|
||||
#include <FLAC/metadata.h>
|
||||
#include <FLAC/format.h>
|
||||
#include <FLAC/stream_encoder.h>
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
inline constexpr auto flac_encoding = mpt::common_encoding::utf8;
|
||||
|
||||
class flac_stream_raii : public file_audio_stream_base {
|
||||
private:
|
||||
commandlineflags flags;
|
||||
mpt::native_path filename;
|
||||
bool called_init;
|
||||
std::vector< std::pair< std::string, std::string > > tags;
|
||||
FLAC__StreamMetadata * flac_metadata[1];
|
||||
FLAC__StreamEncoder * encoder;
|
||||
std::vector<FLAC__int32> interleaved_buffer;
|
||||
void add_vorbiscomment_field( FLAC__StreamMetadata * vorbiscomment, const std::string & field, const std::string & value ) {
|
||||
if ( !value.empty() ) {
|
||||
FLAC__StreamMetadata_VorbisComment_Entry entry;
|
||||
FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair( &entry, field.c_str(), value.c_str() );
|
||||
FLAC__metadata_object_vorbiscomment_append_comment( vorbiscomment, entry, false );
|
||||
}
|
||||
}
|
||||
public:
|
||||
flac_stream_raii( const mpt::native_path & filename_, const commandlineflags & flags_, concat_stream<mpt::ustring> & /*log*/ ) : flags(flags_), filename(filename_), called_init(false), encoder(0) {
|
||||
flac_metadata[0] = 0;
|
||||
encoder = FLAC__stream_encoder_new();
|
||||
if ( !encoder ) {
|
||||
throw exception( MPT_USTRING("error creating flac encoder") );
|
||||
}
|
||||
FLAC__stream_encoder_set_channels( encoder, flags.channels );
|
||||
FLAC__stream_encoder_set_bits_per_sample( encoder, flags.use_float ? 24 : 16 );
|
||||
FLAC__stream_encoder_set_sample_rate( encoder, flags.samplerate );
|
||||
FLAC__stream_encoder_set_compression_level( encoder, 8 );
|
||||
#if (FLAC_API_VERSION_CURRENT >= 14) && MPT_PLATFORM_MULTITHREADED && !defined(MPT_LIBCXX_QUIRK_NO_STD_THREAD)
|
||||
std::uint32_t threads = static_cast<std::uint32_t>(std::max(std::thread::hardware_concurrency(), static_cast<unsigned int>(1)));
|
||||
// Work-around <https://github.com/xiph/flac/issues/823>.
|
||||
//FLAC__stream_encoder_set_num_threads( encoder, threads );
|
||||
while ( ( FLAC__stream_encoder_set_num_threads( encoder, threads ) == FLAC__STREAM_ENCODER_SET_NUM_THREADS_TOO_MANY_THREADS ) && ( threads > 1 ) ) {
|
||||
threads = ( ( threads > 256 ) ? 256 : ( threads - 1 ) );
|
||||
}
|
||||
#endif
|
||||
}
|
||||
~flac_stream_raii() {
|
||||
if ( encoder ) {
|
||||
FLAC__stream_encoder_finish( encoder );
|
||||
FLAC__stream_encoder_delete( encoder );
|
||||
encoder = 0;
|
||||
}
|
||||
if ( flac_metadata[0] ) {
|
||||
FLAC__metadata_object_delete( flac_metadata[0] );
|
||||
flac_metadata[0] = 0;
|
||||
}
|
||||
}
|
||||
void write_metadata( std::map<mpt::ustring, mpt::ustring> metadata ) override {
|
||||
if ( called_init ) {
|
||||
return;
|
||||
}
|
||||
tags.clear();
|
||||
tags.push_back( std::make_pair( "TITLE", mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("title") ] ) ) );
|
||||
tags.push_back( std::make_pair( "ARTIST", mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("artist") ] ) ) );
|
||||
tags.push_back( std::make_pair( "DATE", mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("date") ] ) ) );
|
||||
tags.push_back( std::make_pair( "COMMENT", mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("message") ] ) ) );
|
||||
if ( !metadata[ MPT_USTRING("type") ].empty() && !metadata[ MPT_USTRING("tracker") ].empty() ) {
|
||||
tags.push_back( std::make_pair( "SOURCEMEDIA", std::string() + "'" + mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("type") ] ) + "' tracked music file, made with '" + mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("tracker") ] ) + "', rendered with '" + mpt::transcode<std::string>( flac_encoding, get_encoder_tag() ) + "'" ) );
|
||||
} else if ( !metadata[ MPT_USTRING("type_long") ].empty() ) {
|
||||
tags.push_back( std::make_pair( "SOURCEMEDIA", std::string() + "'" + mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("type") ] ) + "' tracked music file, rendered with '" + mpt::transcode<std::string>( flac_encoding, get_encoder_tag() ) + "'" ) );
|
||||
} else if ( !metadata[ MPT_USTRING("tracker") ].empty() ) {
|
||||
tags.push_back( std::make_pair( "SOURCEMEDIA", std::string() + "tracked music file, made with '" + mpt::transcode<std::string>( flac_encoding, metadata[ MPT_USTRING("tracker") ] ) + "', rendered with '" + mpt::transcode<std::string>( flac_encoding, get_encoder_tag() ) + "'" ) );
|
||||
} else {
|
||||
tags.push_back( std::make_pair( "SOURCEMEDIA", std::string() + "tracked music file, rendered with '" + mpt::transcode<std::string>( flac_encoding, get_encoder_tag() ) + "'" ) );
|
||||
}
|
||||
tags.push_back( std::make_pair( "ENCODER", mpt::transcode<std::string>( flac_encoding, get_encoder_tag() ) ) );
|
||||
flac_metadata[0] = FLAC__metadata_object_new( FLAC__METADATA_TYPE_VORBIS_COMMENT );
|
||||
for ( std::vector< std::pair< std::string, std::string > >::iterator tag = tags.begin(); tag != tags.end(); ++tag ) {
|
||||
add_vorbiscomment_field( flac_metadata[0], tag->first, tag->second );
|
||||
}
|
||||
FLAC__stream_encoder_set_metadata( encoder, flac_metadata, 1 );
|
||||
}
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
if ( !called_init ) {
|
||||
#if MPT_OS_WINDOWS
|
||||
FLAC__stream_encoder_init_file( encoder, mpt::transcode<std::string>( flac_encoding, filename ).c_str(), NULL, 0 );
|
||||
#else
|
||||
FLAC__stream_encoder_init_file( encoder, filename.AsNative().c_str(), NULL, 0 );
|
||||
#endif
|
||||
called_init = true;
|
||||
}
|
||||
interleaved_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
float in = buffers[channel][frame];
|
||||
if ( in <= -1.0f ) {
|
||||
in = -1.0f;
|
||||
} else if ( in >= 1.0f ) {
|
||||
in = 1.0f;
|
||||
}
|
||||
FLAC__int32 out = mpt::saturate_round<FLAC__int32>( in * (1<<23) );
|
||||
out = std::max( 0 - (1<<23), out );
|
||||
out = std::min( out, 0 + (1<<23) - 1 );
|
||||
interleaved_buffer.push_back( out );
|
||||
}
|
||||
}
|
||||
FLAC__stream_encoder_process_interleaved( encoder, interleaved_buffer.data(), static_cast<unsigned int>( frames ) );
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
if ( !called_init ) {
|
||||
#if MPT_OS_WINDOWS
|
||||
FLAC__stream_encoder_init_file( encoder, mpt::transcode<std::string>( flac_encoding, filename ).c_str(), NULL, 0 );
|
||||
#else
|
||||
FLAC__stream_encoder_init_file( encoder, filename.AsNative().c_str(), NULL, 0 );
|
||||
#endif
|
||||
called_init = true;
|
||||
}
|
||||
interleaved_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
interleaved_buffer.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
FLAC__stream_encoder_process_interleaved( encoder, interleaved_buffer.data(), static_cast<unsigned int>( frames ) );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_WITH_FLAC
|
||||
|
||||
#endif // OPENMPT123_FLAC_HPP
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* openmpt123_mmio.hpp
|
||||
* -------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_MMIO_HPP
|
||||
#define OPENMPT123_MMIO_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if MPT_OS_WINDOWS && !MPT_OS_WINDOWS_WINRT
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
class mmio_stream_raii : public file_audio_stream_base {
|
||||
private:
|
||||
concat_stream<mpt::ustring> & log;
|
||||
commandlineflags flags;
|
||||
WAVEFORMATEX waveformatex;
|
||||
HMMIO mmio;
|
||||
MMCKINFO WAVE_chunk;
|
||||
MMCKINFO fmt__chunk;
|
||||
MMCKINFO data_chunk;
|
||||
MMIOINFO data_info;
|
||||
private:
|
||||
void CHECKED( HRESULT err ) {
|
||||
if ( err != 0 ) {
|
||||
throw exception( MPT_USTRING("error writing wave file") );
|
||||
}
|
||||
}
|
||||
void UNCHECKED( HRESULT err ) {
|
||||
if ( err != 0 ) {
|
||||
log << MPT_USTRING("error writing wave file") << lf;
|
||||
}
|
||||
}
|
||||
public:
|
||||
mmio_stream_raii( const mpt::native_path & filename, const commandlineflags & flags_, concat_stream<mpt::ustring> & log_ ) : log(log_), flags(flags_), mmio(NULL) {
|
||||
|
||||
ZeroMemory( &waveformatex, sizeof( WAVEFORMATEX ) );
|
||||
waveformatex.cbSize = 0;
|
||||
waveformatex.wFormatTag = flags.use_float ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
|
||||
waveformatex.nChannels = static_cast<WORD>( flags.channels );
|
||||
waveformatex.nSamplesPerSec = flags.samplerate;
|
||||
waveformatex.wBitsPerSample = flags.use_float ? 32 : 16;
|
||||
waveformatex.nBlockAlign = static_cast<WORD>( flags.channels * ( waveformatex.wBitsPerSample / 8 ) );
|
||||
waveformatex.nAvgBytesPerSec = waveformatex.nSamplesPerSec * waveformatex.nBlockAlign;
|
||||
|
||||
#if defined(UNICODE)
|
||||
wchar_t * tmp = _wcsdup( filename.AsNative().c_str() );
|
||||
mmio = mmioOpen( tmp, NULL, MMIO_ALLOCBUF | MMIO_READWRITE | MMIO_CREATE );
|
||||
free( tmp );
|
||||
tmp = 0;
|
||||
#else
|
||||
char * tmp = strdup( filename.AsNative().c_str() );
|
||||
mmio = mmioOpen( tmp, NULL, MMIO_ALLOCBUF | MMIO_READWRITE | MMIO_CREATE );
|
||||
free( tmp );
|
||||
tmp = 0;
|
||||
#endif
|
||||
|
||||
ZeroMemory( &WAVE_chunk, sizeof( MMCKINFO ) );
|
||||
WAVE_chunk.fccType = mmioFOURCC('W', 'A', 'V', 'E');
|
||||
CHECKED(mmioCreateChunk( mmio, &WAVE_chunk, MMIO_CREATERIFF ));
|
||||
|
||||
ZeroMemory( &fmt__chunk, sizeof( MMCKINFO ) );
|
||||
fmt__chunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
|
||||
fmt__chunk.cksize = sizeof( WAVEFORMATEX );
|
||||
CHECKED(mmioCreateChunk( mmio, &fmt__chunk, 0 ));
|
||||
|
||||
mmioWrite( mmio, (const char*)&waveformatex, sizeof( WAVEFORMATEX ) );
|
||||
|
||||
CHECKED(mmioAscend( mmio, &fmt__chunk, 0 ));
|
||||
|
||||
ZeroMemory( &data_chunk, sizeof( MMCKINFO ) );
|
||||
data_chunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
|
||||
data_chunk.cksize = 0;
|
||||
CHECKED(mmioCreateChunk( mmio, &data_chunk, 0 ));
|
||||
|
||||
ZeroMemory( &data_info, sizeof( MMIOINFO ) );
|
||||
CHECKED(mmioGetInfo( mmio, &data_info, 0 ));
|
||||
|
||||
}
|
||||
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
if ( data_info.pchEndWrite - data_info.pchNext < static_cast<long>( sizeof( float ) ) ) {
|
||||
data_info.dwFlags |= MMIO_DIRTY;
|
||||
CHECKED(mmioAdvance( mmio, &data_info, MMIO_WRITE ));
|
||||
}
|
||||
std::memcpy( data_info.pchNext, &( buffers[channel][frame] ), sizeof( float ) );
|
||||
data_info.pchNext += sizeof( float );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
if ( data_info.pchEndWrite - data_info.pchNext < static_cast<long>( sizeof( std::int16_t ) ) ) {
|
||||
data_info.dwFlags |= MMIO_DIRTY;
|
||||
CHECKED(mmioAdvance( mmio, &data_info, MMIO_WRITE ));
|
||||
}
|
||||
std::memcpy( data_info.pchNext, &( buffers[channel][frame] ), sizeof( std::int16_t ) );
|
||||
data_info.pchNext += sizeof( std::int16_t );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~mmio_stream_raii() {
|
||||
|
||||
data_info.dwFlags |= MMIO_DIRTY;
|
||||
UNCHECKED(mmioSetInfo( mmio, &data_info, 0 ));
|
||||
|
||||
UNCHECKED(mmioAscend( mmio, &data_chunk, 0 ));
|
||||
|
||||
UNCHECKED(mmioAscend( mmio, &WAVE_chunk, 0 ));
|
||||
|
||||
UNCHECKED(mmioClose( mmio, 0 ));
|
||||
mmio = NULL;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_OS_WINDOWS && !MPT_OS_WINDOWS_WINRT
|
||||
|
||||
#endif // OPENMPT123_MMIO_HPP
|
||||
@@ -0,0 +1,305 @@
|
||||
/*
|
||||
* openmpt123_portaudio.hpp
|
||||
* ------------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_PORTAUDIO_HPP
|
||||
#define OPENMPT123_PORTAUDIO_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if defined(MPT_WITH_PORTAUDIO)
|
||||
|
||||
#include <portaudio.h>
|
||||
#if defined(MPT_BUILD_MSVC) && MPT_COMPILER_MSVC && MPT_ARCH_X86
|
||||
extern "C" {
|
||||
void PaUtil_InitializeX86PlainConverters(void);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
inline constexpr auto portaudio_encoding = mpt::common_encoding::utf8;
|
||||
|
||||
struct portaudio_exception : public exception {
|
||||
portaudio_exception( PaError code ) : exception( MPT_USTRING("PortAudio: ") + mpt::transcode<mpt::ustring>( portaudio_encoding, Pa_GetErrorText( code ) ) ) { }
|
||||
};
|
||||
|
||||
typedef void (*PaUtilLogCallback ) (const char *log);
|
||||
#ifdef _MSC_VER
|
||||
extern "C" void PaUtil_SetDebugPrintFunction(PaUtilLogCallback cb);
|
||||
#endif
|
||||
|
||||
class portaudio_raii {
|
||||
private:
|
||||
concat_stream<mpt::ustring> & log;
|
||||
bool log_set;
|
||||
bool portaudio_initialized;
|
||||
static concat_stream<mpt::ustring> * portaudio_log_stream;
|
||||
private:
|
||||
static void portaudio_log_function( const char * log ) {
|
||||
if ( portaudio_log_stream ) {
|
||||
*portaudio_log_stream << MPT_USTRING("PortAudio: ") << mpt::transcode<mpt::ustring>( portaudio_encoding, log );
|
||||
}
|
||||
}
|
||||
protected:
|
||||
void check_portaudio_error( PaError e ) {
|
||||
if ( e > 0 ) {
|
||||
return;
|
||||
}
|
||||
if ( e == paNoError ) {
|
||||
return;
|
||||
}
|
||||
if ( e == paOutputUnderflowed ) {
|
||||
log << MPT_USTRING("PortAudio warning: ") << mpt::transcode<mpt::ustring>( portaudio_encoding, Pa_GetErrorText( e ) ) << lf;
|
||||
return;
|
||||
}
|
||||
throw portaudio_exception( e );
|
||||
}
|
||||
public:
|
||||
portaudio_raii( bool verbose, concat_stream<mpt::ustring> & log ) : log(log), log_set(false), portaudio_initialized(false) {
|
||||
if ( verbose ) {
|
||||
portaudio_log_stream = &log;
|
||||
} else {
|
||||
portaudio_log_stream = 0;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
PaUtil_SetDebugPrintFunction( portaudio_log_function );
|
||||
log_set = true;
|
||||
#endif
|
||||
#if defined(MPT_BUILD_MSVC) && MPT_COMPILER_MSVC && MPT_ARCH_X86
|
||||
PaUtil_InitializeX86PlainConverters();
|
||||
#endif
|
||||
check_portaudio_error( Pa_Initialize() );
|
||||
portaudio_initialized = true;
|
||||
if ( verbose ) {
|
||||
*portaudio_log_stream << lf;
|
||||
}
|
||||
}
|
||||
~portaudio_raii() {
|
||||
if ( portaudio_initialized ) {
|
||||
check_portaudio_error( Pa_Terminate() );
|
||||
portaudio_initialized = false;
|
||||
}
|
||||
if ( log_set ) {
|
||||
#ifdef _MSC_VER
|
||||
PaUtil_SetDebugPrintFunction( NULL );
|
||||
log_set = false;
|
||||
#endif
|
||||
}
|
||||
portaudio_log_stream = 0;
|
||||
}
|
||||
};
|
||||
|
||||
concat_stream<mpt::ustring> * portaudio_raii::portaudio_log_stream = 0;
|
||||
|
||||
class portaudio_stream_blocking_raii : public portaudio_raii, public write_buffers_interface {
|
||||
private:
|
||||
PaStream * stream;
|
||||
bool interleaved;
|
||||
std::size_t channels;
|
||||
std::vector<float> sampleBufFloat;
|
||||
std::vector<std::int16_t> sampleBufInt;
|
||||
public:
|
||||
portaudio_stream_blocking_raii( commandlineflags & flags, concat_stream<mpt::ustring> & log )
|
||||
: portaudio_raii(flags.verbose, log)
|
||||
, stream(NULL)
|
||||
, interleaved(false)
|
||||
, channels(flags.channels)
|
||||
{
|
||||
PaStreamParameters streamparameters;
|
||||
std::memset( &streamparameters, 0, sizeof(PaStreamParameters) );
|
||||
const int device = mpt::parse_or<int>( flags.device, -1 );
|
||||
streamparameters.device = ( device == -1 ) ? Pa_GetDefaultOutputDevice() : device;
|
||||
streamparameters.channelCount = flags.channels;
|
||||
streamparameters.sampleFormat = ( flags.use_float ? paFloat32 : paInt16 ) | paNonInterleaved;
|
||||
if ( flags.buffer == default_high ) {
|
||||
streamparameters.suggestedLatency = Pa_GetDeviceInfo( streamparameters.device )->defaultHighOutputLatency;
|
||||
flags.buffer = static_cast<std::int32_t>( Pa_GetDeviceInfo( streamparameters.device )->defaultHighOutputLatency * 1000.0 );
|
||||
} else if ( flags.buffer == default_low ) {
|
||||
streamparameters.suggestedLatency = Pa_GetDeviceInfo( streamparameters.device )->defaultLowOutputLatency;
|
||||
flags.buffer = static_cast<std::int32_t>( Pa_GetDeviceInfo( streamparameters.device )->defaultLowOutputLatency * 1000.0 );
|
||||
} else {
|
||||
streamparameters.suggestedLatency = flags.buffer * 0.001;
|
||||
}
|
||||
unsigned long framesperbuffer = 0;
|
||||
if ( flags.mode != Mode::UI ) {
|
||||
framesperbuffer = paFramesPerBufferUnspecified;
|
||||
flags.period = 50;
|
||||
flags.period = std::min( flags.period, flags.buffer / 3 );
|
||||
} else if ( flags.period == default_high ) {
|
||||
framesperbuffer = paFramesPerBufferUnspecified;
|
||||
flags.period = 50;
|
||||
flags.period = std::min( flags.period, flags.buffer / 3 );
|
||||
} else if ( flags.period == default_low ) {
|
||||
framesperbuffer = paFramesPerBufferUnspecified;
|
||||
flags.period = 10;
|
||||
flags.period = std::min( flags.period, flags.buffer / 3 );
|
||||
} else {
|
||||
framesperbuffer = flags.period * flags.samplerate / 1000;
|
||||
}
|
||||
if ( flags.period <= 0 ) {
|
||||
flags.period = 1;
|
||||
}
|
||||
flags.apply_default_buffer_sizes();
|
||||
if ( flags.verbose ) {
|
||||
log << MPT_USTRING("PortAudio:") << lf;
|
||||
log << MPT_USTRING(" device: ")
|
||||
<< streamparameters.device
|
||||
<< MPT_USTRING(" [ ") << mpt::transcode<mpt::ustring>( portaudio_encoding, Pa_GetHostApiInfo( Pa_GetDeviceInfo( streamparameters.device )->hostApi )->name ) << MPT_USTRING(" / ") << mpt::transcode<mpt::ustring>( portaudio_encoding, Pa_GetDeviceInfo( streamparameters.device )->name ) << MPT_USTRING(" ] ")
|
||||
<< lf;
|
||||
log << MPT_USTRING(" low latency: ") << Pa_GetDeviceInfo( streamparameters.device )->defaultLowOutputLatency << lf;
|
||||
log << MPT_USTRING(" high latency: ") << Pa_GetDeviceInfo( streamparameters.device )->defaultHighOutputLatency << lf;
|
||||
log << MPT_USTRING(" suggested latency: ") << streamparameters.suggestedLatency << lf;
|
||||
log << MPT_USTRING(" frames per buffer: ") << framesperbuffer << lf;
|
||||
log << MPT_USTRING(" ui redraw: ") << flags.period << lf;
|
||||
}
|
||||
PaError e = PaError();
|
||||
e = Pa_OpenStream( &stream, NULL, &streamparameters, flags.samplerate, framesperbuffer, ( flags.dither > 0 ) ? paNoFlag : paDitherOff, NULL, NULL );
|
||||
if ( e != paNoError ) {
|
||||
// Non-interleaved failed, try interleaved next.
|
||||
// This might help broken portaudio on MacOS X.
|
||||
streamparameters.sampleFormat &= ~paNonInterleaved;
|
||||
e = Pa_OpenStream( &stream, NULL, &streamparameters, flags.samplerate, framesperbuffer, ( flags.dither > 0 ) ? paNoFlag : paDitherOff, NULL, NULL );
|
||||
if ( e == paNoError ) {
|
||||
interleaved = true;
|
||||
}
|
||||
check_portaudio_error( e );
|
||||
}
|
||||
check_portaudio_error( Pa_StartStream( stream ) );
|
||||
if ( flags.verbose ) {
|
||||
log << MPT_USTRING(" channels: ") << streamparameters.channelCount << lf;
|
||||
log << MPT_USTRING(" sampleformat: ") << ( ( ( streamparameters.sampleFormat & ~paNonInterleaved ) == paFloat32 ) ? MPT_USTRING("paFloat32") : MPT_USTRING("paInt16") ) << lf;
|
||||
log << MPT_USTRING(" latency: ") << Pa_GetStreamInfo( stream )->outputLatency << lf;
|
||||
log << MPT_USTRING(" samplerate: ") << Pa_GetStreamInfo( stream )->sampleRate << lf;
|
||||
log << lf;
|
||||
}
|
||||
}
|
||||
~portaudio_stream_blocking_raii() {
|
||||
if ( stream ) {
|
||||
PaError stopped = Pa_IsStreamStopped( stream );
|
||||
check_portaudio_error( stopped );
|
||||
if ( !stopped ) {
|
||||
check_portaudio_error( Pa_StopStream( stream ) );
|
||||
}
|
||||
check_portaudio_error( Pa_CloseStream( stream ) );
|
||||
stream = NULL;
|
||||
}
|
||||
}
|
||||
private:
|
||||
template<typename Tsample>
|
||||
void write_frames( const Tsample * buffer, std::size_t frames ) {
|
||||
while ( frames > 0 ) {
|
||||
unsigned long chunk_frames = static_cast<unsigned long>( std::min( static_cast<std::uint64_t>( frames ), static_cast<std::uint64_t>( std::numeric_limits<unsigned long>::max() ) ) );
|
||||
check_portaudio_error( Pa_WriteStream( stream, buffer, chunk_frames ) );
|
||||
buffer += chunk_frames * channels;
|
||||
frames -= chunk_frames;
|
||||
}
|
||||
}
|
||||
template<typename Tsample>
|
||||
void write_frames( std::vector<Tsample*> buffers, std::size_t frames ) {
|
||||
while ( frames > 0 ) {
|
||||
unsigned long chunk_frames = static_cast<unsigned long>( std::min( static_cast<std::uint64_t>( frames ), static_cast<std::uint64_t>( std::numeric_limits<unsigned long>::max() ) ) );
|
||||
check_portaudio_error( Pa_WriteStream( stream, buffers.data(), chunk_frames ) );
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
buffers[channel] += chunk_frames;
|
||||
}
|
||||
frames -= chunk_frames;
|
||||
}
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
if ( interleaved ) {
|
||||
sampleBufFloat.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleBufFloat.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
write_frames( sampleBufFloat.data(), frames );
|
||||
} else {
|
||||
write_frames( buffers, frames );
|
||||
}
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
if ( interleaved ) {
|
||||
sampleBufInt.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleBufInt.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
write_frames( sampleBufInt.data(), frames );
|
||||
} else {
|
||||
write_frames( buffers, frames );
|
||||
}
|
||||
}
|
||||
bool unpause() override {
|
||||
check_portaudio_error( Pa_StartStream( stream ) );
|
||||
return true;
|
||||
}
|
||||
bool pause() override {
|
||||
check_portaudio_error( Pa_StopStream( stream ) );
|
||||
return true;
|
||||
}
|
||||
bool sleep( int ms ) override {
|
||||
Pa_Sleep( ms );
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#define portaudio_stream_raii portaudio_stream_blocking_raii
|
||||
|
||||
inline std::vector<mpt::ustring> show_portaudio_devices( concat_stream<mpt::ustring> & log ) {
|
||||
std::vector<mpt::ustring> devices;
|
||||
portaudio_raii portaudio( false, log );
|
||||
for ( PaDeviceIndex i = 0; i < Pa_GetDeviceCount(); ++i ) {
|
||||
if ( Pa_GetDeviceInfo( i ) && Pa_GetDeviceInfo( i )->maxOutputChannels > 0 ) {
|
||||
string_concat_stream<mpt::ustring> device;
|
||||
device << i << MPT_USTRING(": ");
|
||||
if ( Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi ) && Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi )->name ) {
|
||||
device << mpt::transcode<mpt::ustring>( portaudio_encoding, Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi )->name );
|
||||
} else {
|
||||
device << MPT_USTRING("Host API ") << Pa_GetDeviceInfo( i )->hostApi;
|
||||
}
|
||||
if ( Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi ) ) {
|
||||
if ( i == Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi )->defaultOutputDevice ) {
|
||||
device << MPT_USTRING(" (default)");
|
||||
}
|
||||
}
|
||||
device << MPT_USTRING(" - ");
|
||||
if ( Pa_GetDeviceInfo( i )->name ) {
|
||||
device << mpt::transcode<mpt::ustring>( portaudio_encoding, Pa_GetDeviceInfo( i )->name );
|
||||
} else {
|
||||
device << MPT_USTRING("Device ") << i;
|
||||
}
|
||||
device << MPT_USTRING(" (");
|
||||
device << MPT_USTRING("high latency: ") << Pa_GetDeviceInfo( i )->defaultHighOutputLatency;
|
||||
device << MPT_USTRING(", ");
|
||||
device << MPT_USTRING("low latency: ") << Pa_GetDeviceInfo( i )->defaultLowOutputLatency;
|
||||
device << MPT_USTRING(")");
|
||||
devices.push_back( device.str() );
|
||||
}
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
inline mpt::ustring show_portaudio_version() {
|
||||
string_concat_stream<mpt::ustring> log;
|
||||
log << mpt::transcode<mpt::ustring>( portaudio_encoding, Pa_GetVersionText() ) << MPT_USTRING(" (") << Pa_GetVersion() << MPT_USTRING(") <http://portaudio.com/>");
|
||||
return log.str();
|
||||
}
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_WITH_PORTAUDIO
|
||||
|
||||
#endif // OPENMPT123_PORTAUDIO_HPP
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* openmpt123_pulseaudio.hpp
|
||||
* -------------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_PULSEAUDIO_HPP
|
||||
#define OPENMPT123_PULSEAUDIO_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#if defined(MPT_WITH_PULSEAUDIO)
|
||||
|
||||
#include <pulse/pulseaudio.h>
|
||||
#include <pulse/simple.h>
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
inline constexpr auto pulseaudio_encoding = mpt::common_encoding::utf8;
|
||||
|
||||
struct pulseaudio_exception : public exception {
|
||||
static mpt::ustring error_to_string( int error ) {
|
||||
try {
|
||||
if ( error == 0 ) {
|
||||
return mpt::ustring();
|
||||
}
|
||||
string_concat_stream<mpt::ustring> e;
|
||||
const char * str = pa_strerror( error );
|
||||
if ( !str ) {
|
||||
e << MPT_USTRING("error=)") << error;
|
||||
return e.str();
|
||||
}
|
||||
if ( std::strlen(str) == 0 ) {
|
||||
e << MPT_USTRING("error=") << error;
|
||||
return e.str();
|
||||
}
|
||||
e << mpt::transcode<mpt::ustring>( pulseaudio_encoding, str ) << MPT_USTRING(" (error=") << error << MPT_USTRING(")");
|
||||
return e.str();
|
||||
} catch ( const std::bad_alloc & ) {
|
||||
return mpt::ustring();
|
||||
}
|
||||
}
|
||||
pulseaudio_exception( int error ) : exception( MPT_USTRING("PulseAudio: ") + error_to_string( error ) ) { }
|
||||
};
|
||||
|
||||
class pulseaudio_stream_raii : public write_buffers_interface {
|
||||
private:
|
||||
pa_simple * stream;
|
||||
std::size_t channels;
|
||||
std::size_t sampleSize;
|
||||
std::vector<float> sampleBufFloat;
|
||||
std::vector<std::int16_t> sampleBufInt;
|
||||
public:
|
||||
pulseaudio_stream_raii( commandlineflags & flags, concat_stream<mpt::ustring> & /* log */ )
|
||||
: stream(NULL)
|
||||
, channels(flags.channels)
|
||||
, sampleSize(flags.use_float ? sizeof( float ) : sizeof( std::int16_t ))
|
||||
{
|
||||
int error = 0;
|
||||
pa_sample_spec ss;
|
||||
std::memset( &ss, 0, sizeof( pa_sample_spec ) );
|
||||
ss.format = ( flags.use_float ? PA_SAMPLE_FLOAT32 : PA_SAMPLE_S16NE );
|
||||
ss.rate = flags.samplerate;
|
||||
ss.channels = static_cast<std::uint8_t>( flags.channels );
|
||||
pa_buffer_attr ba;
|
||||
std::memset( &ba, 0, sizeof( pa_buffer_attr ) );
|
||||
bool use_ba = false;
|
||||
if ( flags.buffer != default_high && flags.buffer != default_low ) {
|
||||
use_ba = true;
|
||||
ba.maxlength = static_cast<std::uint32_t>( channels * sampleSize * ( flags.buffer * flags.samplerate / 1000 ) );
|
||||
} else {
|
||||
ba.maxlength = static_cast<std::uint32_t>(-1);
|
||||
}
|
||||
if ( flags.period != default_high && flags.period != default_low ) {
|
||||
use_ba = true;
|
||||
ba.minreq = static_cast<std::uint32_t>( channels * sampleSize * ( flags.period * flags.samplerate / 1000 ) );
|
||||
if ( ba.maxlength != static_cast<std::uint32_t>(-1) ) {
|
||||
ba.tlength = ba.maxlength - ba.minreq;
|
||||
ba.prebuf = ba.tlength;
|
||||
} else {
|
||||
ba.tlength = static_cast<std::uint32_t>(-1);
|
||||
ba.prebuf = static_cast<std::uint32_t>(-1);
|
||||
}
|
||||
} else {
|
||||
ba.minreq = static_cast<std::uint32_t>(-1);
|
||||
ba.tlength = static_cast<std::uint32_t>(-1);
|
||||
ba.prebuf = static_cast<std::uint32_t>(-1);
|
||||
}
|
||||
ba.fragsize = 0;
|
||||
flags.apply_default_buffer_sizes();
|
||||
sampleBufFloat.resize( channels * ( flags.ui_redraw_interval * flags.samplerate / 1000 ) );
|
||||
sampleBufInt.resize( channels * ( flags.ui_redraw_interval * flags.samplerate / 1000 ) );
|
||||
stream = pa_simple_new( NULL, "openmpt123", PA_STREAM_PLAYBACK, NULL, "openmpt123", &ss, NULL, ( use_ba ? &ba : NULL ), &error );
|
||||
if ( !stream ) {
|
||||
throw pulseaudio_exception( error );
|
||||
}
|
||||
}
|
||||
~pulseaudio_stream_raii() {
|
||||
int error = 0;
|
||||
if ( stream ) {
|
||||
error = 0;
|
||||
if ( pa_simple_drain( stream, &error ) < 0 ) {
|
||||
// throw pulseaudio_exception( error );
|
||||
}
|
||||
pa_simple_free( stream );
|
||||
stream = NULL;
|
||||
}
|
||||
}
|
||||
private:
|
||||
template<typename Tsample>
|
||||
void write_frames( const Tsample * buffer, std::size_t frames ) {
|
||||
int error = 0;
|
||||
if ( pa_simple_write( stream, buffer, frames * channels * sampleSize, &error ) < 0 ) {
|
||||
throw pulseaudio_exception( error );
|
||||
}
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
sampleBufFloat.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleBufFloat.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
write_frames( sampleBufFloat.data(), frames );
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
sampleBufInt.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleBufInt.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
write_frames( sampleBufInt.data(), frames );
|
||||
}
|
||||
bool unpause() override {
|
||||
return true;
|
||||
}
|
||||
bool pause() override {
|
||||
int error = 0;
|
||||
error = 0;
|
||||
if ( pa_simple_drain( stream, &error ) < 0 ) {
|
||||
throw pulseaudio_exception( error );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool sleep( int ms ) override {
|
||||
pa_msleep( ms );
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
inline std::vector<mpt::ustring> show_pulseaudio_devices( concat_stream<mpt::ustring> & /* log */ ) {
|
||||
string_concat_stream<mpt::ustring> device;
|
||||
device << MPT_USTRING("0") << MPT_USTRING(": Default Device");
|
||||
return { device.str() };
|
||||
}
|
||||
|
||||
inline mpt::ustring show_pulseaudio_version() {
|
||||
string_concat_stream<mpt::ustring> log;
|
||||
log << MPT_USTRING("libpulse, libpulse-simple") << MPT_USTRING(" (headers ") << mpt::transcode<mpt::ustring>( pulseaudio_encoding, pa_get_headers_version() ) << MPT_USTRING(", API ") << PA_API_VERSION << MPT_USTRING(", PROTOCOL ") << PA_PROTOCOL_VERSION << MPT_USTRING(", library ") << mpt::transcode<mpt::ustring>( pulseaudio_encoding, ( pa_get_library_version() ? pa_get_library_version() : "unknown" ) ) << MPT_USTRING(") <https://www.freedesktop.org/wiki/Software/PulseAudio/>");
|
||||
return log.str();
|
||||
}
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_WITH_PULSEAUDIO
|
||||
|
||||
#endif // OPENMPT123_PULSEAUDIO_HPP
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* openmpt123_raw.hpp
|
||||
* ------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_RAW_HPP
|
||||
#define OPENMPT123_RAW_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
class raw_stream_raii : public file_audio_stream_base {
|
||||
private:
|
||||
commandlineflags flags;
|
||||
mpt::IO::ofstream file;
|
||||
std::vector<float> interleaved_float_buffer;
|
||||
std::vector<std::int16_t> interleaved_int_buffer;
|
||||
public:
|
||||
raw_stream_raii( const mpt::native_path & filename, const commandlineflags & flags_, concat_stream<mpt::ustring> & /*log*/ ) : flags(flags_), file(filename, std::ios::binary) {
|
||||
return;
|
||||
}
|
||||
~raw_stream_raii() {
|
||||
return;
|
||||
}
|
||||
void write_metadata( std::map<mpt::ustring, mpt::ustring> /* metadata */ ) override {
|
||||
return;
|
||||
}
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
interleaved_float_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
interleaved_float_buffer.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
file.write( reinterpret_cast<const char *>( interleaved_float_buffer.data() ), frames * buffers.size() * sizeof( float ) );
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
interleaved_int_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
interleaved_int_buffer.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
file.write( reinterpret_cast<const char *>( interleaved_int_buffer.data() ), frames * buffers.size() * sizeof( std::int16_t ) );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // OPENMPT123_RAW_HPP
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* openmpt123_sdl2.hpp
|
||||
* -------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_SDL2_HPP
|
||||
#define OPENMPT123_SDL2_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#if defined(MPT_WITH_SDL2)
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
|
||||
#pragma clang diagnostic ignored "-Wreserved-id-macro"
|
||||
#endif // __clang__
|
||||
#include <SDL.h>
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#endif // __clang__
|
||||
#ifdef main
|
||||
#undef main
|
||||
#endif
|
||||
#ifdef SDL_main
|
||||
#undef SDL_main
|
||||
#endif
|
||||
#if (SDL_COMPILEDVERSION < SDL_VERSIONNUM(2, 0, 4))
|
||||
MPT_WARNING("Support for SDL2 < 2.0.4 has been deprecated and will be removed in a future openmpt123 version.")
|
||||
#endif
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
inline constexpr auto sdl2_encoding = mpt::common_encoding::utf8;
|
||||
|
||||
struct sdl2_exception : public exception {
|
||||
private:
|
||||
static mpt::ustring text_from_code( int code ) {
|
||||
string_concat_stream<mpt::ustring> s;
|
||||
s << code;
|
||||
return s.str();
|
||||
}
|
||||
public:
|
||||
sdl2_exception( int code, const char * error ) : exception( MPT_USTRING("SDL2: ") + text_from_code( code ) + MPT_USTRING(" (") + mpt::transcode<mpt::ustring>( sdl2_encoding, error ? std::string(error) : std::string("NULL") ) + MPT_USTRING(")") ) { }
|
||||
};
|
||||
|
||||
static void check_sdl2_error( int e ) {
|
||||
if ( e < 0 ) {
|
||||
throw sdl2_exception( e, SDL_GetError() );
|
||||
}
|
||||
}
|
||||
|
||||
class sdl2_raii {
|
||||
public:
|
||||
sdl2_raii( Uint32 flags ) {
|
||||
check_sdl2_error( SDL_Init( flags ) );
|
||||
}
|
||||
~sdl2_raii() {
|
||||
SDL_Quit();
|
||||
}
|
||||
};
|
||||
|
||||
class sdl2_stream_raii : public write_buffers_interface {
|
||||
private:
|
||||
concat_stream<mpt::ustring> & log;
|
||||
sdl2_raii sdl2;
|
||||
int dev;
|
||||
std::size_t channels;
|
||||
bool use_float;
|
||||
std::size_t sampleQueueMaxFrames;
|
||||
std::vector<float> sampleBufFloat;
|
||||
std::vector<std::int16_t> sampleBufInt;
|
||||
protected:
|
||||
std::uint32_t round_up_power2(std::uint32_t x)
|
||||
{
|
||||
std::uint32_t result = 1;
|
||||
while ( result < x ) {
|
||||
result *= 2;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public:
|
||||
sdl2_stream_raii( commandlineflags & flags, concat_stream<mpt::ustring> & log_ )
|
||||
: log(log_)
|
||||
, sdl2( SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER | SDL_INIT_AUDIO )
|
||||
, dev(-1)
|
||||
, channels(flags.channels)
|
||||
, use_float(flags.use_float)
|
||||
, sampleQueueMaxFrames(0)
|
||||
{
|
||||
if ( flags.buffer == default_high ) {
|
||||
flags.buffer = 160;
|
||||
} else if ( flags.buffer == default_low ) {
|
||||
flags.buffer = 80;
|
||||
}
|
||||
if ( flags.period == default_high ) {
|
||||
flags.period = 20;
|
||||
} else if ( flags.period == default_low ) {
|
||||
flags.period = 10;
|
||||
}
|
||||
flags.apply_default_buffer_sizes();
|
||||
SDL_AudioSpec audiospec;
|
||||
std::memset( &audiospec, 0, sizeof( SDL_AudioSpec ) );
|
||||
audiospec.freq = flags.samplerate;
|
||||
audiospec.format = ( flags.use_float ? AUDIO_F32SYS : AUDIO_S16SYS );
|
||||
audiospec.channels = static_cast<Uint8>( flags.channels );
|
||||
audiospec.silence = 0;
|
||||
audiospec.samples = static_cast<Uint16>( round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) ) );
|
||||
audiospec.size = static_cast<Uint32>( audiospec.samples * audiospec.channels * ( flags.use_float ? sizeof( float ) : sizeof( std::int16_t ) ) );
|
||||
audiospec.callback = NULL;
|
||||
audiospec.userdata = NULL;
|
||||
if ( flags.verbose ) {
|
||||
log << MPT_USTRING("SDL2:") << lf;
|
||||
log << MPT_USTRING(" latency: ") << ( audiospec.samples * 2.0 / flags.samplerate ) << MPT_USTRING(" (2 * ") << audiospec.samples << MPT_USTRING(")") << lf;
|
||||
log << lf;
|
||||
}
|
||||
sampleQueueMaxFrames = round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) );
|
||||
SDL_AudioSpec audiospec_obtained;
|
||||
std::memset( &audiospec_obtained, 0, sizeof( SDL_AudioSpec ) );
|
||||
std::memcpy( &audiospec_obtained, &audiospec, sizeof( SDL_AudioSpec ) );
|
||||
dev = SDL_OpenAudioDevice( NULL, 0, &audiospec, &audiospec_obtained, 0 );
|
||||
if ( dev < 0 ) {
|
||||
check_sdl2_error( dev );
|
||||
} else if ( dev == 0 ) {
|
||||
check_sdl2_error( -1 );
|
||||
}
|
||||
SDL_PauseAudioDevice( dev, 0 );
|
||||
}
|
||||
~sdl2_stream_raii() {
|
||||
SDL_PauseAudioDevice( dev, 1 );
|
||||
SDL_CloseAudioDevice( dev );
|
||||
}
|
||||
private:
|
||||
std::size_t get_num_writeable_frames() {
|
||||
std::size_t num_queued_frames = SDL_GetQueuedAudioSize( dev ) / ( use_float ? sizeof( float ) : sizeof( std::int16_t ) ) / channels;
|
||||
if ( num_queued_frames > sampleQueueMaxFrames ) {
|
||||
return 0;
|
||||
}
|
||||
return sampleQueueMaxFrames - num_queued_frames;
|
||||
}
|
||||
template<typename Tsample>
|
||||
void write_frames( const Tsample * buffer, std::size_t frames ) {
|
||||
while ( frames > 0 ) {
|
||||
std::size_t chunk_frames = std::min( frames, get_num_writeable_frames() );
|
||||
if ( chunk_frames > 0 ) {
|
||||
check_sdl2_error( SDL_QueueAudio( dev, buffer, static_cast<Uint32>( chunk_frames * channels * ( use_float ? sizeof( float ) : sizeof( std::int16_t ) ) ) ) );
|
||||
frames -= chunk_frames;
|
||||
buffer += chunk_frames * channels;
|
||||
} else {
|
||||
SDL_Delay( 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
sampleBufFloat.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleBufFloat.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
write_frames( sampleBufFloat.data(), frames );
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
sampleBufInt.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < channels; ++channel ) {
|
||||
sampleBufInt.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
write_frames( sampleBufInt.data(), frames );
|
||||
}
|
||||
bool pause() override {
|
||||
SDL_PauseAudioDevice( dev, 1 );
|
||||
return true;
|
||||
}
|
||||
bool unpause() override {
|
||||
SDL_PauseAudioDevice( dev, 0 );
|
||||
return true;
|
||||
}
|
||||
bool sleep( int ms ) override {
|
||||
SDL_Delay( ms );
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
inline std::vector<mpt::ustring> show_sdl2_devices( concat_stream<mpt::ustring> & /* log */ ) {
|
||||
std::vector<mpt::ustring> devices;
|
||||
std::size_t device_index = 0;
|
||||
sdl2_raii sdl2( SDL_INIT_NOPARACHUTE | SDL_INIT_AUDIO );
|
||||
for ( int driver = 0; driver < SDL_GetNumAudioDrivers(); ++driver ) {
|
||||
const char * driver_name = SDL_GetAudioDriver( driver );
|
||||
if ( !driver_name ) {
|
||||
continue;
|
||||
}
|
||||
if ( std::string( driver_name ).empty() ) {
|
||||
continue;
|
||||
}
|
||||
if ( SDL_AudioInit( driver_name ) < 0 ) {
|
||||
continue;
|
||||
}
|
||||
for ( int device = 0; device < SDL_GetNumAudioDevices( 0 ); ++device ) {
|
||||
string_concat_stream<mpt::ustring> dev;
|
||||
const char * device_name = SDL_GetAudioDeviceName( device, 0 );
|
||||
if ( !device_name ) {
|
||||
continue;
|
||||
}
|
||||
if ( std::string( device_name ).empty() ) {
|
||||
continue;
|
||||
}
|
||||
dev << device_index << MPT_USTRING(": ") << mpt::transcode<mpt::ustring>( sdl2_encoding, driver_name ) << MPT_USTRING(" - ") << mpt::transcode<mpt::ustring>( sdl2_encoding, device_name );
|
||||
device_index++;
|
||||
devices.push_back( dev.str() );
|
||||
}
|
||||
SDL_AudioQuit();
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
inline mpt::ustring show_sdl2_version() {
|
||||
string_concat_stream<mpt::ustring> log;
|
||||
log << MPT_USTRING("libSDL2 ");
|
||||
SDL_version sdlver;
|
||||
std::memset(&sdlver, 0, sizeof(SDL_version));
|
||||
SDL_GetVersion(&sdlver);
|
||||
log << static_cast<int>(sdlver.major) << MPT_USTRING(".") << static_cast<int>(sdlver.minor) << MPT_USTRING(".") << static_cast<int>(sdlver.patch);
|
||||
const char* revision = SDL_GetRevision();
|
||||
if (revision) {
|
||||
log << MPT_USTRING(" (") << mpt::transcode<mpt::ustring>(sdl2_encoding, revision) << MPT_USTRING(")");
|
||||
}
|
||||
log << MPT_USTRING(", ");
|
||||
std::memset(&sdlver, 0, sizeof(SDL_version));
|
||||
SDL_VERSION(&sdlver);
|
||||
log << MPT_USTRING("API: ") << static_cast<int>(sdlver.major) << MPT_USTRING(".") << static_cast<int>(sdlver.minor) << MPT_USTRING(".") << static_cast<int>(sdlver.patch);
|
||||
log << MPT_USTRING(" <https://libsdl.org/>");
|
||||
return log.str();
|
||||
}
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_WITH_SDL2
|
||||
|
||||
#endif // OPENMPT123_SDL2_HPP
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* openmpt123_sndfile.hpp
|
||||
* ----------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_SNDFILE_HPP
|
||||
#define OPENMPT123_SNDFILE_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#if defined(MPT_WITH_SNDFILE)
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#include <sndfile.h>
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
inline constexpr auto sndfile_encoding = mpt::common_encoding::utf8;
|
||||
|
||||
class sndfile_stream_raii : public file_audio_stream_base {
|
||||
private:
|
||||
commandlineflags flags;
|
||||
concat_stream<mpt::ustring> & log;
|
||||
SNDFILE * sndfile;
|
||||
std::vector<float> interleaved_float_buffer;
|
||||
std::vector<std::int16_t> interleaved_int_buffer;
|
||||
private:
|
||||
enum match_mode_enum {
|
||||
match_print,
|
||||
match_recurse,
|
||||
match_exact,
|
||||
match_better,
|
||||
match_any
|
||||
};
|
||||
mpt::ustring match_mode_to_string( match_mode_enum match_mode ) {
|
||||
switch ( match_mode ) {
|
||||
case match_print : return MPT_USTRING("print") ; break;
|
||||
case match_recurse: return MPT_USTRING("recurse"); break;
|
||||
case match_exact : return MPT_USTRING("exact") ; break;
|
||||
case match_better : return MPT_USTRING("better") ; break;
|
||||
case match_any : return MPT_USTRING("any") ; break;
|
||||
}
|
||||
return MPT_USTRING("");
|
||||
}
|
||||
int matched_result( int format, const SF_FORMAT_INFO & format_info, const SF_FORMAT_INFO & subformat_info, match_mode_enum match_mode ) {
|
||||
if ( flags.verbose ) {
|
||||
log << MPT_USTRING("sndfile: using format '")
|
||||
<< mpt::transcode<mpt::ustring>( sndfile_encoding, format_info.name ) << MPT_USTRING(" (") << mpt::transcode<mpt::ustring>( sndfile_encoding, format_info.extension ) << MPT_USTRING(")") << MPT_USTRING(" / ") << mpt::transcode<mpt::ustring>( sndfile_encoding, subformat_info.name )
|
||||
<< MPT_USTRING("', ")
|
||||
<< MPT_USTRING("match: ") << match_mode_to_string( match_mode )
|
||||
<< lf;
|
||||
}
|
||||
return format;
|
||||
}
|
||||
int find_format( const mpt::native_path & extension, match_mode_enum match_mode ) {
|
||||
|
||||
if ( match_mode == match_recurse ) {
|
||||
int result = 0;
|
||||
result = find_format( extension, match_exact );
|
||||
if ( result ) {
|
||||
return result;
|
||||
}
|
||||
result = find_format( extension, match_better );
|
||||
if ( result ) {
|
||||
return result;
|
||||
}
|
||||
result = find_format( extension, match_any );
|
||||
if ( result ) {
|
||||
return result;
|
||||
}
|
||||
if ( result ) {
|
||||
return result;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int major_count;
|
||||
sf_command( 0, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof( int ) );
|
||||
for ( int m = 0; m < major_count; m++ ) {
|
||||
|
||||
SF_FORMAT_INFO format_info;
|
||||
format_info.format = m;
|
||||
sf_command( 0, SFC_GET_FORMAT_MAJOR, &format_info, sizeof( SF_FORMAT_INFO ) );
|
||||
|
||||
int subtype_count;
|
||||
sf_command( 0, SFC_GET_FORMAT_SUBTYPE_COUNT, &subtype_count, sizeof( int ) );
|
||||
for ( int s = 0; s < subtype_count; s++ ) {
|
||||
|
||||
SF_FORMAT_INFO subformat_info;
|
||||
subformat_info.format = s;
|
||||
sf_command( 0, SFC_GET_FORMAT_SUBTYPE, &subformat_info, sizeof( SF_FORMAT_INFO ) );
|
||||
int format = ( format_info.format & SF_FORMAT_TYPEMASK ) | ( subformat_info.format & SF_FORMAT_SUBMASK );
|
||||
|
||||
SF_INFO sfinfo;
|
||||
std::memset( &sfinfo, 0, sizeof( SF_INFO ) );
|
||||
sfinfo.channels = flags.channels;
|
||||
sfinfo.format = format;
|
||||
if ( sf_format_check( &sfinfo ) ) {
|
||||
|
||||
switch ( match_mode ) {
|
||||
case match_print:
|
||||
log << MPT_USTRING("sndfile: ")
|
||||
<< mpt::transcode<mpt::ustring>( sndfile_encoding, ( format_info.name ? format_info.name : "" ) ) << MPT_USTRING(" (.") << mpt::transcode<mpt::ustring>( sndfile_encoding, ( subformat_info.extension ? subformat_info.extension : format_info.extension ? format_info.extension : "" ) ) << MPT_USTRING(")")
|
||||
<< MPT_USTRING(" / ")
|
||||
<< mpt::transcode<mpt::ustring>( sndfile_encoding, ( subformat_info.name ? subformat_info.name : "" ) )
|
||||
<< MPT_USTRING(" [")
|
||||
<< mpt::format<mpt::ustring>::hex0<8>( format )
|
||||
<< MPT_USTRING("]")
|
||||
<< lf;
|
||||
break;
|
||||
case match_recurse:
|
||||
break;
|
||||
case match_exact:
|
||||
if ( subformat_info.extension && ( mpt::transcode<std::string>( sndfile_encoding, extension ) == subformat_info.extension ) ) {
|
||||
if ( flags.use_float && ( subformat_info.format == SF_FORMAT_FLOAT ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
} else if ( !flags.use_float && ( subformat_info.format == SF_FORMAT_PCM_16 ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
}
|
||||
}
|
||||
if ( format_info.extension && ( mpt::transcode<std::string>( sndfile_encoding, extension ) == format_info.extension ) ) {
|
||||
if ( flags.use_float && ( subformat_info.format == SF_FORMAT_FLOAT ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
} else if ( !flags.use_float && ( subformat_info.format == SF_FORMAT_PCM_16 ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case match_better:
|
||||
if ( subformat_info.extension && ( mpt::transcode<std::string>( sndfile_encoding, extension ) == subformat_info.extension ) ) {
|
||||
if ( flags.use_float && ( subformat_info.format == SF_FORMAT_FLOAT || subformat_info.format == SF_FORMAT_DOUBLE ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
} else if ( !flags.use_float && ( subformat_info.format == SF_FORMAT_PCM_16 || subformat_info.format == SF_FORMAT_PCM_24 || subformat_info.format == SF_FORMAT_PCM_32 ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
}
|
||||
}
|
||||
if ( format_info.extension && ( mpt::transcode<std::string>( sndfile_encoding, extension ) == format_info.extension ) ) {
|
||||
if ( flags.use_float && ( subformat_info.format == SF_FORMAT_FLOAT || subformat_info.format == SF_FORMAT_DOUBLE ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
} else if ( !flags.use_float && ( subformat_info.format == SF_FORMAT_PCM_16 || subformat_info.format == SF_FORMAT_PCM_24 || subformat_info.format == SF_FORMAT_PCM_32 ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case match_any:
|
||||
if ( subformat_info.extension && ( mpt::transcode<std::string>( sndfile_encoding, extension ) == subformat_info.extension ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
}
|
||||
if ( format_info.extension && ( mpt::transcode<std::string>( sndfile_encoding, extension ) == format_info.extension ) ) {
|
||||
return matched_result( format, format_info, subformat_info, match_mode );
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
void write_metadata_field( int str_type, const std::string & str ) {
|
||||
if ( !str.empty() ) {
|
||||
sf_set_string( sndfile, str_type, str.c_str() );
|
||||
}
|
||||
}
|
||||
public:
|
||||
sndfile_stream_raii( const mpt::native_path & filename, const commandlineflags & flags_, concat_stream<mpt::ustring> & log_ ) : flags(flags_), log(log_), sndfile(0) {
|
||||
if ( flags.verbose ) {
|
||||
find_format( MPT_NATIVE_PATH(""), match_print );
|
||||
log << lf;
|
||||
}
|
||||
int format = find_format( flags.output_extension, match_recurse );
|
||||
if ( !format ) {
|
||||
throw exception( MPT_USTRING("unknown file type") );
|
||||
}
|
||||
SF_INFO info;
|
||||
std::memset( &info, 0, sizeof( SF_INFO ) );
|
||||
info.samplerate = flags.samplerate;
|
||||
info.channels = flags.channels;
|
||||
info.format = format;
|
||||
#if MPT_OS_WINDOWS && defined(UNICODE)
|
||||
sndfile = sf_wchar_open( filename.AsNative().c_str(), SFM_WRITE, &info );
|
||||
#else
|
||||
sndfile = sf_open( filename.AsNative().c_str(), SFM_WRITE, &info );
|
||||
#endif
|
||||
}
|
||||
~sndfile_stream_raii() {
|
||||
sf_close( sndfile );
|
||||
sndfile = 0;
|
||||
}
|
||||
void write_metadata( std::map<mpt::ustring, mpt::ustring> metadata ) override {
|
||||
write_metadata_field( SF_STR_TITLE, mpt::transcode<std::string>( sndfile_encoding, metadata[ MPT_USTRING("title") ] ) );
|
||||
write_metadata_field( SF_STR_ARTIST, mpt::transcode<std::string>( sndfile_encoding, metadata[ MPT_USTRING("artist") ] ) );
|
||||
write_metadata_field( SF_STR_DATE, mpt::transcode<std::string>( sndfile_encoding, metadata[ MPT_USTRING("date") ] ) );
|
||||
write_metadata_field( SF_STR_COMMENT, mpt::transcode<std::string>( sndfile_encoding, metadata[ MPT_USTRING("message") ] ) );
|
||||
write_metadata_field( SF_STR_SOFTWARE, mpt::transcode<std::string>( sndfile_encoding, append_software_tag( metadata[ MPT_USTRING("tracker") ] ) ) );
|
||||
}
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
interleaved_float_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
interleaved_float_buffer.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
sf_writef_float( sndfile, interleaved_float_buffer.data(), frames );
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
interleaved_int_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
interleaved_int_buffer.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
sf_writef_short( sndfile, interleaved_int_buffer.data(), frames );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_WITH_SNDFILE
|
||||
|
||||
#endif // OPENMPT123_SNDFILE_HPP
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* openmpt123_stdio.hpp
|
||||
* --------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_STDIO_HPP
|
||||
#define OPENMPT123_STDIO_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
|
||||
#include "openmpt123_exception.hpp"
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#elif MPT_OS_WINDOWS
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
enum class FILE_mode {
|
||||
text,
|
||||
binary,
|
||||
};
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
|
||||
class FILE_mode_guard {
|
||||
private:
|
||||
FILE * file;
|
||||
int old_mode;
|
||||
public:
|
||||
FILE_mode_guard( FILE * file, FILE_mode new_mode )
|
||||
: file(file)
|
||||
, old_mode(-1)
|
||||
{
|
||||
switch (new_mode) {
|
||||
case FILE_mode::text:
|
||||
fflush( file );
|
||||
old_mode = setmode( fileno( file ), O_TEXT );
|
||||
if ( old_mode == -1 ) {
|
||||
throw exception( MPT_USTRING("failed to set TEXT mode on file descriptor") );
|
||||
}
|
||||
break;
|
||||
case FILE_mode::binary:
|
||||
fflush( file );
|
||||
old_mode = setmode( fileno( file ), O_BINARY );
|
||||
if ( old_mode == -1 ) {
|
||||
throw exception( MPT_USTRING("failed to set binary mode on file descriptor") );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
FILE_mode_guard( const FILE_mode_guard & ) = delete;
|
||||
FILE_mode_guard( FILE_mode_guard && ) = default;
|
||||
FILE_mode_guard & operator=( const FILE_mode_guard & ) = delete;
|
||||
FILE_mode_guard & operator=( FILE_mode_guard && ) = default;
|
||||
~FILE_mode_guard() {
|
||||
if ( old_mode != -1 ) {
|
||||
fflush( file );
|
||||
old_mode = setmode( fileno( file ), old_mode );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#elif MPT_OS_WINDOWS
|
||||
|
||||
class FILE_mode_guard {
|
||||
private:
|
||||
FILE * file;
|
||||
int old_mode;
|
||||
public:
|
||||
FILE_mode_guard( FILE * file, FILE_mode new_mode )
|
||||
: file(file)
|
||||
, old_mode(-1)
|
||||
{
|
||||
switch (new_mode) {
|
||||
case FILE_mode::text:
|
||||
fflush( file );
|
||||
#if defined(UNICODE) && MPT_LIBC_MS_AT_LEAST(MPT_LIBC_MS_VER_UCRT)
|
||||
old_mode = _setmode( _fileno( file ), _O_U8TEXT );
|
||||
#else
|
||||
old_mode = _setmode( _fileno( file ), _O_TEXT );
|
||||
#endif
|
||||
if ( old_mode == -1 ) {
|
||||
throw exception( MPT_USTRING("failed to set TEXT mode on file descriptor") );
|
||||
}
|
||||
break;
|
||||
case FILE_mode::binary:
|
||||
fflush( file );
|
||||
old_mode = _setmode( _fileno( file ), _O_BINARY );
|
||||
if ( old_mode == -1 ) {
|
||||
throw exception( MPT_USTRING("failed to set binary mode on file descriptor") );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
FILE_mode_guard( const FILE_mode_guard & ) = delete;
|
||||
FILE_mode_guard( FILE_mode_guard && ) = default;
|
||||
FILE_mode_guard & operator=( const FILE_mode_guard & ) = delete;
|
||||
FILE_mode_guard & operator=( FILE_mode_guard && ) = default;
|
||||
~FILE_mode_guard() {
|
||||
if ( old_mode != -1 ) {
|
||||
fflush( file );
|
||||
old_mode = _setmode( _fileno( file ), old_mode );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
class FILE_mode_guard {
|
||||
public:
|
||||
FILE_mode_guard( FILE * /* file */, FILE_mode /* new_mode */ ) {
|
||||
return;
|
||||
}
|
||||
FILE_mode_guard( const FILE_mode_guard & ) = delete;
|
||||
FILE_mode_guard( FILE_mode_guard && ) = default;
|
||||
FILE_mode_guard & operator=( const FILE_mode_guard & ) = delete;
|
||||
FILE_mode_guard & operator=( FILE_mode_guard && ) = default;
|
||||
~FILE_mode_guard() = default;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // OPENMPT123_STDIO_HPP
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* openmpt123_stdout.hpp
|
||||
* ---------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_STDOUT_HPP
|
||||
#define OPENMPT123_STDOUT_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
class stdout_stream_raii : public write_buffers_interface {
|
||||
private:
|
||||
std::vector<float> interleaved_float_buffer;
|
||||
std::vector<std::int16_t> interleaved_int_buffer;
|
||||
public:
|
||||
stdout_stream_raii() {
|
||||
return;
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
interleaved_float_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
interleaved_float_buffer.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
std::cout.write( reinterpret_cast<const char *>( interleaved_float_buffer.data() ), interleaved_float_buffer.size() * sizeof( float ) );
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
interleaved_int_buffer.clear();
|
||||
for ( std::size_t frame = 0; frame < frames; frame++ ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
|
||||
interleaved_int_buffer.push_back( buffers[channel][frame] );
|
||||
}
|
||||
}
|
||||
std::cout.write( reinterpret_cast<const char *>( interleaved_int_buffer.data() ), interleaved_int_buffer.size() * sizeof( std::int16_t ) );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // OPENMPT123_STDOUT_HPP
|
||||
@@ -0,0 +1,525 @@
|
||||
/*
|
||||
* openmpt123_terminal.hpp
|
||||
* -----------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_TERMINAL_HPP
|
||||
#define OPENMPT123_TERMINAL_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/simple.hpp"
|
||||
#include "mpt/parse/parse.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
#include <conio.h>
|
||||
#include <dpmi.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#elif MPT_OS_WINDOWS
|
||||
#include <conio.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#include <stdio.h>
|
||||
#if MPT_LIBC_MINGW
|
||||
#include <string.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/poll.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
template <typename Tstring>
|
||||
struct concat_stream {
|
||||
virtual concat_stream & append( Tstring str ) = 0;
|
||||
virtual ~concat_stream() = default;
|
||||
inline concat_stream<Tstring> & operator<<( concat_stream<Tstring> & (*func)( concat_stream<Tstring> & s ) ) {
|
||||
return func( *this );
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tstring>
|
||||
inline concat_stream<Tstring> & lf( concat_stream<Tstring> & s ) {
|
||||
return s.append( Tstring(1, mpt::char_constants<typename Tstring::value_type>::lf) );
|
||||
}
|
||||
|
||||
template <typename T, typename Tstring>
|
||||
inline concat_stream<Tstring> & operator<<( concat_stream<Tstring> & s, const T & val ) {
|
||||
return s.append( mpt::default_formatter::template format<Tstring>( val ) );
|
||||
}
|
||||
|
||||
template <typename Tstring>
|
||||
struct string_concat_stream
|
||||
: public concat_stream<Tstring>
|
||||
{
|
||||
private:
|
||||
Tstring m_str;
|
||||
public:
|
||||
inline void str( Tstring s ) {
|
||||
m_str = std::move( s );
|
||||
}
|
||||
inline concat_stream<Tstring> & append( Tstring s ) override {
|
||||
m_str += std::move( s );
|
||||
return *this;
|
||||
}
|
||||
inline Tstring str() const {
|
||||
return m_str;
|
||||
}
|
||||
~string_concat_stream() override = default;
|
||||
};
|
||||
|
||||
|
||||
#if MPT_OS_WINDOWS
|
||||
inline std::optional<DWORD> StdHandleFromFd( int fd ) {
|
||||
std::optional<DWORD> stdHandle;
|
||||
if ( fd == _fileno( stdin ) ) {
|
||||
stdHandle = STD_INPUT_HANDLE;
|
||||
} else if ( fd == _fileno( stdout ) ) {
|
||||
stdHandle = STD_OUTPUT_HANDLE;
|
||||
} else if ( fd == _fileno( stderr ) ) {
|
||||
stdHandle = STD_ERROR_HANDLE;
|
||||
}
|
||||
return stdHandle;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
inline bool IsConsole( DWORD stdHandle ) {
|
||||
HANDLE hStd = GetStdHandle( stdHandle );
|
||||
if ( ( hStd == NULL ) || ( hStd == INVALID_HANDLE_VALUE ) ) {
|
||||
return false;
|
||||
}
|
||||
DWORD mode = 0;
|
||||
return GetConsoleMode( hStd, &mode ) != FALSE;
|
||||
}
|
||||
#endif // MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
|
||||
inline bool IsTerminal( int fd ) {
|
||||
#if MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
if ( !_isatty( fd ) ) {
|
||||
return false;
|
||||
}
|
||||
std::optional<DWORD> stdHandle = StdHandleFromFd( fd );
|
||||
if ( !stdHandle ) {
|
||||
return false;
|
||||
}
|
||||
return IsConsole( *stdHandle );
|
||||
#else
|
||||
return isatty( fd ) ? true : false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
class textout : public string_concat_stream<mpt::ustring> {
|
||||
protected:
|
||||
textout() = default;
|
||||
public:
|
||||
virtual ~textout() = default;
|
||||
protected:
|
||||
mpt::ustring pop() {
|
||||
mpt::ustring text = str();
|
||||
str( mpt::ustring() );
|
||||
return text;
|
||||
}
|
||||
public:
|
||||
virtual void writeout() = 0;
|
||||
virtual void cursor_up( std::size_t lines ) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class textout_dummy : public textout {
|
||||
public:
|
||||
textout_dummy() = default;
|
||||
~textout_dummy() override {
|
||||
static_cast<void>( pop() );
|
||||
}
|
||||
public:
|
||||
void writeout() override {
|
||||
static_cast<void>( pop() );
|
||||
}
|
||||
void cursor_up( std::size_t lines ) override {
|
||||
static_cast<void>( lines );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
enum class textout_destination {
|
||||
destination_stdout,
|
||||
destination_stderr,
|
||||
};
|
||||
|
||||
class textout_backend {
|
||||
protected:
|
||||
textout_backend() = default;
|
||||
public:
|
||||
virtual ~textout_backend() = default;
|
||||
public:
|
||||
virtual void write( const mpt::ustring & text ) = 0;
|
||||
virtual void cursor_up(std::size_t lines) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class textout_ostream : public textout_backend {
|
||||
private:
|
||||
std::ostream & s;
|
||||
#if MPT_OS_DJGPP
|
||||
mpt::common_encoding codepage;
|
||||
#endif
|
||||
public:
|
||||
textout_ostream( std::ostream & s_ )
|
||||
: s(s_)
|
||||
#if MPT_OS_DJGPP
|
||||
, codepage(mpt::common_encoding::cp437)
|
||||
#endif
|
||||
{
|
||||
#if MPT_OS_DJGPP
|
||||
codepage = mpt::djgpp_get_locale_encoding();
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
~textout_ostream() override = default;
|
||||
public:
|
||||
void write( const mpt::ustring & text ) override {
|
||||
if ( text.length() > 0 ) {
|
||||
#if MPT_OS_DJGPP
|
||||
s << mpt::transcode<std::string>( codepage, text );
|
||||
#elif MPT_OS_EMSCRIPTEN
|
||||
s << mpt::transcode<std::string>( mpt::common_encoding::utf8, text ) ;
|
||||
#else
|
||||
s << mpt::transcode<std::string>( mpt::logical_encoding::locale, text );
|
||||
#endif
|
||||
s.flush();
|
||||
}
|
||||
}
|
||||
void cursor_up( std::size_t lines ) override {
|
||||
s.flush();
|
||||
for ( std::size_t line = 0; line < lines; ++line ) {
|
||||
s << std::string("\x1b[1A");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#if MPT_OS_WINDOWS && defined(UNICODE)
|
||||
|
||||
class textout_wostream : public textout_backend {
|
||||
private:
|
||||
std::wostream & s;
|
||||
public:
|
||||
textout_wostream( std::wostream & s_ )
|
||||
: s(s_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
~textout_wostream() override = default;
|
||||
public:
|
||||
void write( const mpt::ustring & text ) override {
|
||||
if ( text.length() > 0 ) {
|
||||
s << mpt::transcode<std::wstring>( text );
|
||||
s.flush();
|
||||
}
|
||||
}
|
||||
void cursor_up( std::size_t lines ) override {
|
||||
s.flush();
|
||||
for ( std::size_t line = 0; line < lines; ++line ) {
|
||||
s << std::wstring(L"\x1b[1A");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // MPT_OS_WINDOWS && UNICODE
|
||||
|
||||
#if MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
|
||||
class textout_ostream_console : public textout_backend {
|
||||
private:
|
||||
#if defined(UNICODE)
|
||||
std::wostream & s;
|
||||
#else
|
||||
std::ostream & s;
|
||||
#endif
|
||||
HANDLE handle;
|
||||
bool console;
|
||||
public:
|
||||
#if defined(UNICODE)
|
||||
textout_ostream_console( std::wostream & s_, DWORD stdHandle_ )
|
||||
#else
|
||||
textout_ostream_console( std::ostream & s_, DWORD stdHandle_ )
|
||||
#endif
|
||||
: s(s_)
|
||||
, handle(GetStdHandle( stdHandle_ ))
|
||||
, console(IsConsole( stdHandle_ ))
|
||||
{
|
||||
return;
|
||||
}
|
||||
~textout_ostream_console() override = default;
|
||||
public:
|
||||
void write( const mpt::ustring & text ) override {
|
||||
if ( text.length() > 0 ) {
|
||||
if ( console ) {
|
||||
DWORD chars_written = 0;
|
||||
#if defined(UNICODE)
|
||||
std::wstring wtext = mpt::transcode<std::wstring>( text );
|
||||
WriteConsole( handle, wtext.data(), static_cast<DWORD>( wtext.size() ), &chars_written, NULL );
|
||||
#else
|
||||
std::string ltext = mpt::transcode<std::string>( mpt::logical_encoding::locale, text );
|
||||
WriteConsole( handle, ltext.data(), static_cast<DWORD>( ltext.size() ), &chars_written, NULL );
|
||||
#endif
|
||||
} else {
|
||||
#if defined(UNICODE)
|
||||
s << mpt::transcode<std::wstring>( text );
|
||||
#else
|
||||
s << mpt::transcode<std::string>( mpt::logical_encoding::locale, text );
|
||||
#endif
|
||||
s.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
void cursor_up( std::size_t lines ) override {
|
||||
if ( console ) {
|
||||
s.flush();
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
ZeroMemory( &csbi, sizeof( CONSOLE_SCREEN_BUFFER_INFO ) );
|
||||
COORD coord_cursor = COORD();
|
||||
if ( GetConsoleScreenBufferInfo( handle, &csbi ) != FALSE ) {
|
||||
coord_cursor = csbi.dwCursorPosition;
|
||||
coord_cursor.X = 1;
|
||||
coord_cursor.Y -= static_cast<SHORT>( lines );
|
||||
SetConsoleCursorPosition( handle, coord_cursor );
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
|
||||
|
||||
|
||||
template <textout_destination dest>
|
||||
class textout_wrapper : public textout {
|
||||
private:
|
||||
#if MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
#if defined(UNICODE)
|
||||
textout_ostream_console out{ dest == textout_destination::destination_stdout ? std::wcout : std::wclog, dest == textout_destination::destination_stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE };
|
||||
#else
|
||||
textout_ostream_console out{ dest == textout_destination::destination_stdout ? std::cout : std::clog, dest == textout_destination::destination_stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE };
|
||||
#endif
|
||||
#elif MPT_OS_WINDOWS
|
||||
#if defined(UNICODE)
|
||||
textout_wostream out{ dest == textout_destination::destination_stdout ? std::wcout : std::wclog };
|
||||
#else
|
||||
textout_ostream out{ dest == textout_destination::destination_stdout ? std::cout : std::clog };
|
||||
#endif
|
||||
#else
|
||||
textout_ostream out{ dest == textout_destination::destination_stdout ? std::cout : std::clog };
|
||||
#endif
|
||||
public:
|
||||
textout_wrapper() = default;
|
||||
~textout_wrapper() override {
|
||||
out.write( pop() );
|
||||
}
|
||||
public:
|
||||
void writeout() override {
|
||||
out.write( pop() );
|
||||
}
|
||||
void cursor_up(std::size_t lines) override {
|
||||
out.cursor_up( lines );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
inline void query_terminal_size( int & terminal_width, int & terminal_height ) {
|
||||
#if MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
HANDLE hStdOutput = GetStdHandle( STD_OUTPUT_HANDLE );
|
||||
if ( ( hStdOutput != NULL ) && ( hStdOutput != INVALID_HANDLE_VALUE ) ) {
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
ZeroMemory( &csbi, sizeof( CONSOLE_SCREEN_BUFFER_INFO ) );
|
||||
if ( GetConsoleScreenBufferInfo( hStdOutput, &csbi ) != FALSE ) {
|
||||
if ( terminal_width <= 0 ) {
|
||||
terminal_width = std::min( static_cast<int>( 1 + csbi.srWindow.Right - csbi.srWindow.Left ), static_cast<int>( csbi.dwSize.X ) );
|
||||
}
|
||||
if ( terminal_height <= 0 ) {
|
||||
terminal_height = std::min( static_cast<int>( 1 + csbi.srWindow.Bottom - csbi.srWindow.Top ), static_cast<int>( csbi.dwSize.Y ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
#else // !(MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10))
|
||||
if ( isatty( STDERR_FILENO ) ) {
|
||||
if ( terminal_width <= 0 ) {
|
||||
const char * env_columns = std::getenv( "COLUMNS" );
|
||||
if ( env_columns ) {
|
||||
int tmp = mpt::parse_or<int>( env_columns, 0 );
|
||||
if ( tmp > 0 ) {
|
||||
terminal_width = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( terminal_height <= 0 ) {
|
||||
const char * env_rows = std::getenv( "ROWS" );
|
||||
if ( env_rows ) {
|
||||
int tmp = mpt::parse_or<int>( env_rows, 0 );
|
||||
if ( tmp > 0 ) {
|
||||
terminal_height = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
#if defined(TIOCGWINSZ)
|
||||
struct winsize ts;
|
||||
if ( ioctl( STDERR_FILENO, TIOCGWINSZ, &ts ) >= 0 ) {
|
||||
if ( terminal_width <= 0 ) {
|
||||
terminal_width = ts.ws_col;
|
||||
}
|
||||
if ( terminal_height <= 0 ) {
|
||||
terminal_height = ts.ws_row;
|
||||
}
|
||||
}
|
||||
#elif defined(TIOCGSIZE)
|
||||
struct ttysize ts;
|
||||
if ( ioctl( STDERR_FILENO, TIOCGSIZE, &ts ) >= 0 ) {
|
||||
if ( terminal_width <= 0 ) {
|
||||
terminal_width = ts.ts_cols;
|
||||
}
|
||||
if ( terminal_height <= 0 ) {
|
||||
terminal_height = ts.ts_rows;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif // MPT_OS_WINDOWS && !MPT_WINRT_BEFORE(MPT_WIN_10)
|
||||
#if MPT_OS_DJGPP
|
||||
if ( terminal_width <= 0 ) {
|
||||
terminal_width = 80;
|
||||
}
|
||||
if ( terminal_height <= 0 ) {
|
||||
terminal_height = 25;
|
||||
}
|
||||
#else
|
||||
if ( terminal_width <= 0 ) {
|
||||
terminal_width = 72;
|
||||
}
|
||||
if ( terminal_height <= 0 ) {
|
||||
terminal_height = 23;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#if MPT_OS_WINDOWS
|
||||
|
||||
class terminal_ui_guard {
|
||||
public:
|
||||
terminal_ui_guard() = default;
|
||||
terminal_ui_guard( const terminal_ui_guard & ) = delete;
|
||||
terminal_ui_guard( terminal_ui_guard && ) = default;
|
||||
terminal_ui_guard & operator=( const terminal_ui_guard & ) = delete;
|
||||
terminal_ui_guard & operator=( terminal_ui_guard && ) = default;
|
||||
~terminal_ui_guard() = default;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
class terminal_ui_guard {
|
||||
private:
|
||||
bool changed = false;
|
||||
termios saved_attributes;
|
||||
public:
|
||||
terminal_ui_guard() {
|
||||
if ( !isatty( STDIN_FILENO ) ) {
|
||||
return;
|
||||
}
|
||||
tcgetattr( STDIN_FILENO, &saved_attributes );
|
||||
termios tattr = saved_attributes;
|
||||
tattr.c_lflag &= ~( ICANON | ECHO );
|
||||
tattr.c_cc[VMIN] = 1;
|
||||
tattr.c_cc[VTIME] = 0;
|
||||
tcsetattr( STDIN_FILENO, TCSAFLUSH, &tattr );
|
||||
changed = true;
|
||||
}
|
||||
terminal_ui_guard( const terminal_ui_guard & ) = delete;
|
||||
terminal_ui_guard( terminal_ui_guard && ) = default;
|
||||
terminal_ui_guard & operator=( const terminal_ui_guard & ) = delete;
|
||||
terminal_ui_guard & operator=( terminal_ui_guard && ) = default;
|
||||
~terminal_ui_guard() {
|
||||
if ( changed ) {
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
class terminal_input {
|
||||
public:
|
||||
static inline bool is_input_available() {
|
||||
#if MPT_OS_DJGPP
|
||||
return kbhit() ? true : false;
|
||||
#elif MPT_OS_WINDOWS && defined( UNICODE ) && !MPT_OS_WINDOWS_WINRT
|
||||
return _kbhit() ? true : false;
|
||||
#elif MPT_OS_WINDOWS
|
||||
return _kbhit() ? true : false;
|
||||
#else
|
||||
pollfd pollfds;
|
||||
pollfds.fd = STDIN_FILENO;
|
||||
pollfds.events = POLLIN;
|
||||
poll(&pollfds, 1, 0);
|
||||
if ( !( pollfds.revents & POLLIN ) ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
static inline std::optional<int> read_input_char() {
|
||||
#if MPT_OS_DJGPP
|
||||
int c = getch();
|
||||
return static_cast<int>( c );
|
||||
#elif MPT_OS_WINDOWS && defined( UNICODE ) && !MPT_OS_WINDOWS_WINRT
|
||||
wint_t c = _getwch();
|
||||
return static_cast<int>( c );
|
||||
#elif MPT_OS_WINDOWS
|
||||
int c = _getch();
|
||||
return static_cast<int>( c );
|
||||
#else
|
||||
char c = 0;
|
||||
if ( read( STDIN_FILENO, &c, 1 ) != 1 ) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<int>( c );
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // OPENMPT123_TERMINAL_HPP
|
||||
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* openmpt123_waveout.hpp
|
||||
* ------------------------
|
||||
* Purpose: libopenmpt command line player
|
||||
* Notes : (currently none)
|
||||
* Authors: OpenMPT Devs
|
||||
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENMPT123_WAVEOUT_HPP
|
||||
#define OPENMPT123_WAVEOUT_HPP
|
||||
|
||||
#include "openmpt123_config.hpp"
|
||||
#include "openmpt123.hpp"
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if MPT_OS_WINDOWS && !MPT_OS_WINDOWS_WINRT
|
||||
|
||||
namespace openmpt123 {
|
||||
|
||||
struct waveout_exception : public exception {
|
||||
waveout_exception() : exception( MPT_USTRING("WaveOut: ") + MPT_USTRING("waveout") ) { }
|
||||
};
|
||||
|
||||
class waveout_stream_raii : public write_buffers_interface {
|
||||
private:
|
||||
HWAVEOUT waveout;
|
||||
std::size_t num_channels;
|
||||
std::size_t num_chunks;
|
||||
std::size_t frames_per_chunk;
|
||||
std::size_t bytes_per_chunk;
|
||||
std::vector<WAVEHDR> waveheaders;
|
||||
std::vector<std::vector<char> > wavebuffers;
|
||||
std::deque<char> byte_queue;
|
||||
public:
|
||||
waveout_stream_raii( commandlineflags & flags, concat_stream<mpt::ustring> & /* log */ )
|
||||
: waveout(NULL)
|
||||
, num_channels(0)
|
||||
, num_chunks(0)
|
||||
, frames_per_chunk(0)
|
||||
, bytes_per_chunk(0)
|
||||
{
|
||||
if ( flags.buffer == default_high ) {
|
||||
flags.buffer = 150;
|
||||
} else if ( flags.buffer == default_low ) {
|
||||
flags.buffer = 50;
|
||||
}
|
||||
if ( flags.period == default_high ) {
|
||||
flags.period = 30;
|
||||
} else if ( flags.period == default_low ) {
|
||||
flags.period = 10;
|
||||
}
|
||||
flags.apply_default_buffer_sizes();
|
||||
WAVEFORMATEX wfx;
|
||||
ZeroMemory( &wfx, sizeof( wfx ) );
|
||||
wfx.wFormatTag = flags.use_float ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
|
||||
wfx.nChannels = static_cast<WORD>( flags.channels );
|
||||
wfx.nSamplesPerSec = flags.samplerate;
|
||||
wfx.wBitsPerSample = flags.use_float ? 32 : 16;
|
||||
wfx.nBlockAlign = ( wfx.wBitsPerSample / 8 ) * wfx.nChannels;
|
||||
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
|
||||
wfx.cbSize = 0;
|
||||
const int device = mpt::parse_or<int>( flags.device, -1 );
|
||||
waveOutOpen( &waveout, ( device == -1 ) ? WAVE_MAPPER : device, &wfx, 0, 0, CALLBACK_NULL );
|
||||
num_channels = flags.channels;
|
||||
std::size_t frames_per_buffer = flags.samplerate * flags.buffer / 1000;
|
||||
num_chunks = ( flags.buffer + flags.period - 1 ) / flags.period;
|
||||
if ( num_chunks < 2 ) {
|
||||
num_chunks = 2;
|
||||
}
|
||||
frames_per_chunk = ( frames_per_buffer + num_chunks - 1 ) / num_chunks;
|
||||
bytes_per_chunk = wfx.nBlockAlign * frames_per_chunk;
|
||||
waveheaders.resize( num_chunks );
|
||||
wavebuffers.resize( num_chunks );
|
||||
for ( std::size_t i = 0; i < num_chunks; ++i ) {
|
||||
wavebuffers[i].resize( bytes_per_chunk );
|
||||
waveheaders[i] = WAVEHDR();
|
||||
waveheaders[i].lpData = wavebuffers[i].data();
|
||||
waveheaders[i].dwBufferLength = static_cast<DWORD>( wavebuffers[i].size() );
|
||||
waveheaders[i].dwFlags = 0;
|
||||
waveOutPrepareHeader( waveout, &waveheaders[i], sizeof( WAVEHDR ) );
|
||||
}
|
||||
}
|
||||
~waveout_stream_raii() {
|
||||
if ( waveout ) {
|
||||
write_or_wait( true );
|
||||
drain();
|
||||
waveOutReset( waveout );
|
||||
for ( std::size_t i = 0; i < num_chunks; ++i ) {
|
||||
waveheaders[i].dwBufferLength = static_cast<DWORD>( wavebuffers[i].size() );
|
||||
waveOutUnprepareHeader( waveout, &waveheaders[i], sizeof( WAVEHDR ) );
|
||||
}
|
||||
wavebuffers.clear();
|
||||
waveheaders.clear();
|
||||
frames_per_chunk = 0;
|
||||
num_chunks = 0;
|
||||
waveOutClose( waveout );
|
||||
waveout = NULL;
|
||||
}
|
||||
}
|
||||
private:
|
||||
void drain() {
|
||||
std::size_t empty_chunks = 0;
|
||||
while ( empty_chunks != num_chunks ) {
|
||||
empty_chunks = 0;
|
||||
for ( std::size_t chunk = 0; chunk < num_chunks; ++chunk ) {
|
||||
DWORD flags = waveheaders[chunk].dwFlags;
|
||||
if ( !(flags & WHDR_INQUEUE) || (flags & WHDR_DONE) ) {
|
||||
empty_chunks++;
|
||||
}
|
||||
}
|
||||
if ( empty_chunks != num_chunks ) {
|
||||
Sleep( 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
std::size_t wait_for_empty_chunk() {
|
||||
while ( true ) {
|
||||
for ( std::size_t chunk = 0; chunk < num_chunks; ++chunk ) {
|
||||
DWORD flags = waveheaders[chunk].dwFlags;
|
||||
if ( !(flags & WHDR_INQUEUE) || (flags & WHDR_DONE) ) {
|
||||
return chunk;
|
||||
}
|
||||
}
|
||||
Sleep( 1 );
|
||||
}
|
||||
}
|
||||
void write_chunk() {
|
||||
std::size_t chunk = wait_for_empty_chunk();
|
||||
std::size_t chunk_bytes = std::min( byte_queue.size(), bytes_per_chunk );
|
||||
waveheaders[chunk].dwBufferLength = static_cast<DWORD>( chunk_bytes );
|
||||
for ( std::size_t byte = 0; byte < chunk_bytes; ++byte ) {
|
||||
wavebuffers[chunk][byte] = byte_queue.front();
|
||||
byte_queue.pop_front();
|
||||
}
|
||||
waveOutWrite( waveout, &waveheaders[chunk], sizeof( WAVEHDR ) );
|
||||
}
|
||||
void write_or_wait( bool flush = false ) {
|
||||
while ( byte_queue.size() >= bytes_per_chunk ) {
|
||||
write_chunk();
|
||||
}
|
||||
if ( flush && !byte_queue.empty() ) {
|
||||
write_chunk();
|
||||
}
|
||||
}
|
||||
template < typename Tsample >
|
||||
void write_buffers( const std::vector<Tsample*> buffers, std::size_t frames ) {
|
||||
for ( std::size_t frame = 0; frame < frames; ++frame ) {
|
||||
for ( std::size_t channel = 0; channel < buffers.size(); ++channel ) {
|
||||
Tsample val = buffers[channel][frame];
|
||||
char buf[ sizeof( Tsample ) ];
|
||||
std::memcpy( buf, &val, sizeof( Tsample ) );
|
||||
std::copy( buf, buf + sizeof( Tsample ), std::back_inserter( byte_queue ) );
|
||||
}
|
||||
}
|
||||
write_or_wait();
|
||||
}
|
||||
public:
|
||||
void write( const std::vector<float*> buffers, std::size_t frames ) override {
|
||||
write_buffers( buffers, frames );
|
||||
}
|
||||
void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
|
||||
write_buffers( buffers, frames );
|
||||
}
|
||||
bool pause() override {
|
||||
waveOutPause( waveout );
|
||||
return true;
|
||||
}
|
||||
bool unpause() override {
|
||||
waveOutRestart( waveout );
|
||||
return true;
|
||||
}
|
||||
bool sleep( int ms ) override {
|
||||
Sleep( ms );
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
inline std::vector<mpt::ustring> show_waveout_devices( concat_stream<mpt::ustring> & /*log*/ ) {
|
||||
std::vector<mpt::ustring> devices;
|
||||
for ( UINT i = 0; i < waveOutGetNumDevs(); ++i ) {
|
||||
string_concat_stream<mpt::ustring> device;
|
||||
device << i << MPT_USTRING(": ");
|
||||
WAVEOUTCAPS caps;
|
||||
ZeroMemory( &caps, sizeof( caps ) );
|
||||
waveOutGetDevCaps( i, &caps, sizeof( caps ) );
|
||||
#if defined(UNICODE)
|
||||
device << mpt::transcode<mpt::ustring>( caps.szPname );
|
||||
#else
|
||||
device << mpt::transcode<mpt::ustring>( mpt::logical_encoding::locale, caps.szPname );
|
||||
#endif
|
||||
devices.push_back( device.str() );
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
} // namespace openmpt123
|
||||
|
||||
#endif // MPT_OS_WINDOWS && !MPT_OS_WINDOWS_WINRT
|
||||
|
||||
#endif // OPENMPT123_WAVEOUT_HPP
|
||||
Reference in new issue
Block a user