xref: /MusicPlayer2/MusicPlayer2/IPlayerCore.h (revision 8959a120c2c1a8b30f887255062f37449f37dee8)
1 #pragma once
2 #include "SongInfo.h"
3 
4 struct MidiInfo
5 {
6     int midi_position;
7     int midi_length;
8     int speed;		//速度,bpm
9     int tempo;		//每个四分音符的微秒数
10     float ppqn;
11 };
12 
13 enum AudioInfoFlag
14 {
15     AF_LENGTH = 1,
16     AF_BITRATE = 2,
17     AF_TAG_INFO = 4,
18     AF_CHANNEL_INFO = 8,
19 
20     AF_ALL = AF_LENGTH | AF_BITRATE | AF_TAG_INFO | AF_CHANNEL_INFO
21 };
22 
23 enum PlayerCoreType
24 {
25     PT_BASS,
26     PT_MCI,
27     PT_FFMPEG,
28 };
29 
30 enum PlayingState       //正在播放标志
31 {
32     PS_STOPED,          //已停止
33     PS_PAUSED,          //已暂停
34     PS_PLAYING          //正在播放
35 };
36 
37 #define MAX_PLAY_SPEED 4.0f
38 #define MIN_PLAY_SPEED 0.1f
39 #define MAX_PLAY_PITCH 12
40 #define MIN_PLAY_PITCH -12
41 
42 //MP3编码参数
43 struct MP3EncodePara
44 {
45     int encode_type{ 0 };               // 0:CBR, 1:ABR, 2:VBR, (更改:<3:自定义参数>不再使用,但加载配置时保持兼容)
46     int cbr_bitrate{ 128 };             // CBR比特率
47     int abr_bitrate{ 128 };             // ABR比特率
48     int vbr_quality{ 4 };               // VBR质量(0~9)
49     bool joint_stereo{ true };
50     bool user_define_para{ false };     // 使用用户自定义参数(此项不保存,当ini中cmd_para非空时加载为true)
51     wstring cmd_para;                   // 命令行参数
52 };
53 
54 //wma 编码参数
55 struct WmaEncodePara
56 {
57     bool cbr{ true };			//true: CBR; false: VBR
58     int cbr_bitrate{ 64 };		//CBR比特率,单位kbps
59     int vbr_quality{ 75 };		//VBR质量
60 };
61 
62 //ogg编码参数
63 struct OggEncodePara
64 {
65     int encode_quality{ 3 };
66 };
67 
68 //Flac 编码参数
69 struct FlacEncodePara
70 {
71     int compression_level{ 8 }; //压缩级别,0~8,0:最小压缩,速度最快;8:最大压缩,速度最慢
72     bool user_define_para{ false }; //用户自定义编码参数
73     wstring cmd_para;		//命令行参数
74 };
75 
76 //转换格式时的输出编码格式
77 enum class EncodeFormat { WAV, MP3, WMA, OGG, FLAC };
78 
79 //格式转换错误代码
80 #define CONVERT_ERROR_FILE_CANNOT_OPEN (-1)	        //源文件无法读取
81 #define CONVERT_ERROR_ENCODE_CHANNEL_FAILED (-2)	//编码通道创建失败
82 #define CONVERT_ERROR_ENCODE_PARA_ERROR (-3)		//找不到编码器或编码器参数错误
83 #define CONVERT_ERROR_MIDI_NO_SF2 (-4)				//没有MIDI音色库
84 #define CONVERT_ERROR_WMA_NO_WMP9_OR_LATER (-5)     //没有安装 Windows Media Player 9 或更高版本。
85 #define CONVERT_ERROR_WMA_NO_SUPPORTED_ENCODER (-6) //无法找到可支持请求的采样格式和比特率的编解码器。
86 
87 
88 /////////////////////////////////////////////////////////////////////////////////////////////////
89 
90 class IPlayerCore
91 {
92 public:
~IPlayerCore()93     virtual ~IPlayerCore() {}
94 
95     virtual void InitCore() = 0;
96     virtual void UnInitCore() = 0;
97 
98     virtual std::wstring GetAudioType() = 0;    //获取音频格式的类型,如果返回空字符串,则会显示为文件的扩展名
99     virtual int GetChannels() = 0;      //获取声道数
100     virtual int GetFReq() = 0;          //获取采样频率,单位为Hz
101     virtual std::wstring GetSoundFontName() = 0;    //播放midi音乐时,获取midi音色库的名称
102 
103     virtual void Open(const wchar_t* file_path) = 0;
104     virtual void Close() = 0;
105     virtual void Play() = 0;
106     virtual void Pause() = 0;
107     virtual void Stop() = 0;
108     virtual void SetVolume(int volume) = 0;
109     virtual void SetSpeed(float speed) = 0;         //设置播放速度(1为原速)
110     virtual void SetPitch(int pitch) = 0;           //设置播放变调,半音为一个单位,[-12, 12],0为原调
111     virtual bool SongIsOver() = 0;                  //曲目是否播放完毕
112 
113     virtual int GetCurPosition() = 0;               //获取当前播放进度,单位为毫秒
114     virtual int GetSongLength() = 0;                //获取歌曲长度,单位为毫秒
115     virtual void SetCurPosition(int position) = 0;  //设置播放进度,单位为毫秒
116     // 获取打开的音频的长度、比特率和标签信息,flag用于指定获取哪些信息
117     virtual void GetAudioInfo(SongInfo& song_info, int flag = AF_LENGTH | AF_BITRATE | AF_TAG_INFO | AF_CHANNEL_INFO) = 0;
118     // 获取file_path属性写入song_info,flag用于指定获取哪些信息(需要支持并发且不影响当前播放)
119     // AF_LENGTH读取文件时长直接写入end_pos,AF_BITRATE读取比特率,AF_TAG_INFO读取标签/分级,AF_CHANNEL_INFO读取采样率/位深度/通道数
120     virtual void GetAudioInfo(const wchar_t* file_path, SongInfo& song_info, int flag = AF_LENGTH | AF_BITRATE | AF_TAG_INFO | AF_CHANNEL_INFO) = 0;
121 
122     /**
123      * @brief   音频编码的回调函数
124      * @param   int progress 转换的进度。0~100: 已完成的百分比; <0:出错,值为此文件中以CONVERT_ERROR_开头的宏定义
125      */
126     typedef void(*EncodeAudioProc)(int progress);
127 
128     /**
129      * @brief   在转换格式时对音频进行编码
130      * @param   SongInfo song_info 要转换的音频文件信息
131      * @param   const wstring & dest_file_path 输出的文件路径
132      * @param   EncodeFormat encode_type 转换的编码格式
133      * @param   int dest_freq 转换的采样频率。如果为0,则不转换采样频率
134      * @param   void * encode_para 编码参数。如果encode_type为ET_WAV,则encode_para应该被忽略;
135                 如果encode_type为ET_MP3,则encode_para为一个指向MP3EncodePara对象的指针;
136                 如果encode_type为ET_WMA,则encode_para为一个指向WmaEncodePara对象的指针;
137                 如果encode_type为ET_OGG,则encode_para为一个指向OggEncodePara对象的指针;
138      * @param   EncodeAudioProc proc 通知转换进度的回调函数
139      * @return  bool 转换成功返回true,否则返回false
140      */
141     virtual bool EncodeAudio(SongInfo song_info, const wstring& dest_file_path, EncodeFormat encode_format, void* encode_para, int dest_freq, EncodeAudioProc proc) = 0;
142     virtual bool InitEncoder() = 0;         //对编码器执行一些初始化工作,成功返回true,失败返回false
143     virtual void UnInitEncoder() = 0;       //对编码器执行一些清理工作
144     virtual bool IsFreqConvertAvailable() = 0;  //转换采样频率是否可用
145 
146     virtual bool IsMidi() = 0;
147     virtual bool IsMidiConnotPlay() = 0;
148     virtual MidiInfo GetMidiInfo() = 0;
149     virtual std::wstring GetMidiInnerLyric() = 0;
150     virtual bool MidiNoLyric() = 0;
151     virtual PlayingState GetPlayingState() = 0;
152 
153     virtual void ApplyEqualizer(int channel, int gain) = 0; //设置均衡器(channel为均衡器通道,取值为0~9,gain为增益,取值为-15~15)
154     virtual void SetReverb(int mix, int time) = 0;		//设置混响(mix为混响强度,取值为0~100,time为混响时间,取值为1~300,单位为10ms)
155     virtual void ClearReverb() = 0;			//关闭混响
156     virtual void GetFFTData(float fft_data[FFT_SAMPLE]) = 0;       //获取频谱分析数据
157 
158     virtual int GetErrorCode() = 0;                         //获取错误代码
159     virtual std::wstring GetErrorInfo(int error_code) = 0;  //根据错误代码获取错误信息
160     virtual std::wstring GetErrorInfo() = 0;  //获取错误信息
161 
162     virtual PlayerCoreType GetCoreType() = 0;
163 
IsVolumeFadingOut()164     virtual bool IsVolumeFadingOut() { return false; }    //是否处于音量淡出状态
165 };
166