MidiPlayerEngine.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // MidiPlayerEngine.h
  3. // MidiPlayer
  4. //
  5. // Created by Kyle on 2021/11/30.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import <AudioToolbox/AudioToolbox.h>
  9. #import "CAudioUnit.h"
  10. NS_ASSUME_NONNULL_BEGIN
  11. @protocol PlayerEngineDelegate <NSObject>
  12. /// 加载成功回调
  13. - (void)initPlayerEngineSuccess:(float)totalTime;
  14. /* 范围:[0,1],每10ms更新一次 */
  15. - (void)ProgressUpdated:(float)progress currentTime:(MusicTimeStamp)currentTime;
  16. /* 播放结束回调 */
  17. - (void)playEnd;
  18. /* 歌曲数据加载完,可返回歌曲总时长 */
  19. - (void)GetMusicTotalTime:(float)time;
  20. @end
  21. @interface MidiPlayerEngine : NSObject
  22. @property (nonatomic, strong) NSMutableArray *muteTrackArray; // 需要禁音的track
  23. @property (nonatomic, assign) float accompanyVolume; // 伴奏音量配置 默认0.2
  24. @property (nonatomic, assign) Float64 baseRate; // 基准rate
  25. // 主要乐器id
  26. @property (nonatomic, assign) NSInteger instrumentId; // 主乐器id
  27. @property (nonatomic, assign) UInt32 baseInstrumentTrack; // 主乐器轨道
  28. @property (nonatomic, strong) NSMutableArray *baseInstrumentArray; // 主乐器轨道
  29. // 定义汇报进度频率 默认10ms
  30. @property (nonatomic, assign) NSInteger reportTime;
  31. @property (nonatomic, strong) NSMutableArray *instrumentTrackNameArray; // 每个track 对应的名称
  32. /* 选段播放控制 */
  33. // 选段的开始时间
  34. @property (nonatomic, assign) MusicTimeStamp startTime;
  35. // 选段的结束时间
  36. @property (nonatomic, assign) MusicTimeStamp endTime;
  37. @property (nonatomic,weak)id<PlayerEngineDelegate> delegate;
  38. - (void)configSoundFilePath:(NSString *)filePath;
  39. /* 加载MIDI文件 */
  40. - (BOOL)loadMIDIFileWithString:(NSString *)filePath;
  41. /* 根据轨道名称获取单个轨道的编号*/
  42. - (NSInteger)getSingleTrackNumByName:(NSString *)trackName;
  43. /* 通过轨道名称获取对应的轨道*/
  44. - (NSMutableArray *)getTrackNumerFromName:(NSArray *)trackNameArray;
  45. /**根据乐器编号获取所在轨道*/
  46. - (NSMutableArray *)getTrackWithInstrumentId:(UInt32)instrumentId;
  47. /**设置对应乐器编号所在轨道的播放音量*/
  48. - (BOOL)volumeTrackVolumeWithInstrumentId:(UInt32)instrumentId volume:(float)volume;
  49. - (void)getAllTrackVolume;
  50. /* 播放 */
  51. - (void)playMIDIFile;
  52. /* 暂停 */
  53. - (void)stopPlayingMIDIFile;
  54. /* 清空加载的音乐数据 */
  55. - (void)cleanup;
  56. /* 获取当前音乐的播放时间,单位:秒 */
  57. - (double)musicTotalTime;
  58. /* 获取当前音乐的播放时间,单位:秒 */
  59. - (double)musicCurrentTime;
  60. /* 播放器当前状态 */
  61. - (BOOL)isPlayingFile;
  62. /* 设置播放速度,快播:(1,+∞),慢放:(0,1) */
  63. - (BOOL)setMusicPlayerSpeed:(double)speed;
  64. /* 设置播放进度 progress的设置范围[0,1] 精度会有影响*/
  65. - (void)setProgress:(double)progress;
  66. /* 设置播放的进度开始时间 */
  67. - (void)setProgressTime:(MusicTimeStamp)startTime;
  68. /* 屏蔽具体一种乐器的声音。注意:屏蔽操作需要在加载MIDI文件之前调用!!! */ //
  69. - (void)muteWithInstrument:(UInt8)instrument;
  70. /* 屏蔽鼓组。注意:屏蔽操作需要在加载MIDI文件之前调用!!! */
  71. - (void)muteWithDrum;
  72. /* 調節某一軌的音量 範圍:[0,1] */
  73. - (void)volume:(float)volume WithTrack:(UInt32)trackNum;
  74. /* 调整除了 trackNum 之外的 其他轨道的音量 范围[0,1]*/
  75. - (void)volumeOtherTrackExcept:(UInt32)trackNum withVolume:(float)volume;
  76. /* 屏蔽某些轨道的的声音 可在进行中操作 (用于原声和伴奏的切换)*/
  77. - (void)muteTrack:(BOOL)isMute WithTrack:(NSMutableArray *)trackArray;
  78. - (void)muteTrackWithTrackNameExclude:(NSMutableArray *)trackNameArray;
  79. // 恢复AUGraph
  80. - (void)resumeAUGraph;
  81. #pragma mark ------ 变调 调整hz
  82. // 转成442HZ播放
  83. - (void)transformTo442Hz;
  84. // hz调整 440 - 442。。。。。。。
  85. - (void)adjustPitchByHZ:(float)newHZ;
  86. #pragma mark ---- 自动循环功能和播放时同步节拍器暂未实现
  87. // 是否重复播放
  88. @property (nonatomic, assign) BOOL isLooping;
  89. // 是否伴随节拍器
  90. @property (nonatomic, getter=isClickTrackEnabled) BOOL clickTrackEnabled;
  91. // 设置自动循环播放
  92. - (void)startPlaybackLoop;
  93. /// Starts playback of the music sequence from the specified position.
  94. /// @param position The MusicTimeStamp to begin playback from.
  95. - (void)startPlaybackFromPosition:(MusicTimeStamp)position;
  96. /**
  97. * Resumes playback of the music sequence from the MusicTimeStamp that the player last stopped at.
  98. */
  99. - (void)resumePlayback;
  100. /**
  101. * Stops playback of the music seuqenece.
  102. */
  103. - (void)stopPlayback;
  104. @end
  105. NS_ASSUME_NONNULL_END