MetronomeManager.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // MetronomeManager.m
  3. // KulexiuSchoolStudent
  4. //
  5. // Created by 王智 on 2023/7/24.
  6. //
  7. #import "MetronomeManager.h"
  8. #import <KSToolLibrary/KSChoosePicker.h>
  9. #import "RhythmChooseView.h"
  10. @interface MetronomeManager ()<KSMetronomePlayerDelegate>
  11. /** 定时器 */
  12. @property (nonatomic, strong) NSTimer *timer;
  13. /** 定时器多少秒循环一次 */
  14. @property (nonatomic, assign) double timerLength;
  15. @property (nonatomic, assign) int currentNo;
  16. @property (nonatomic, assign) NSInteger lastChooseIndex;
  17. @end
  18. @implementation MetronomeManager
  19. + (instancetype)shareInstance {
  20. static MetronomeManager *manager = nil;
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. manager = [[MetronomeManager alloc] init];
  24. });
  25. return manager;
  26. }
  27. - (instancetype)init {
  28. self = [super init];
  29. if (self) {
  30. [self configDefault];
  31. }
  32. return self;
  33. }
  34. - (void)configDefault {
  35. self.speed = 90;
  36. self.rhythmType = RHYTHM_TYPE_ONE_QUARTERNOTES;
  37. self.timerLength = 60.0 / (1.0 * self.speed) / ([self getRate]);
  38. [self resetTimerPlay];
  39. [self createPlayer];
  40. self.playVolume = 1.0f;
  41. self.beatNumber = 4;
  42. self.lastChooseIndex = 4;
  43. }
  44. - (void)setPlayVolume:(float)playVolume {
  45. _playVolume = playVolume;
  46. self.player.volume = playVolume;
  47. }
  48. #pragma mark ---- delegate
  49. - (void)metronomePlayInterruption {
  50. if (self.isPlaying) {
  51. [self stopPlay];
  52. }
  53. }
  54. - (void)startPlay {
  55. if (self.isPlaying == NO) {
  56. // 重置
  57. self.currentNo = -1;
  58. // int bpm = self.speed * [self getRate];
  59. [self.player playRhythmAction:self.speed rhythmType:self.rhythmType beatNumber:self.beatNumber];
  60. // 开始播放
  61. [self.timer setFireDate:[NSDate distantPast]];
  62. }
  63. self.isPlaying = YES;
  64. }
  65. - (void)stopPlay {
  66. if (self.isPlaying) {
  67. self.isPlaying = NO;
  68. [self.timer setFireDate:[NSDate distantFuture]];//暂停计时器
  69. self.currentNo = -1;
  70. [self.player stopPlay];
  71. }
  72. }
  73. - (void)setSpeed:(int)speed {
  74. _speed = speed;
  75. int rateFloat = speed;
  76. self.timerLength = 60.0 / (1.0 * rateFloat) / ([self getRate]);
  77. NSLog(@"---- %f",self.self.timerLength);
  78. [self resetTimerPlay];
  79. if (self.isPlaying) {
  80. [self stopPlay];
  81. [self startPlay];
  82. }
  83. }
  84. - (CGFloat)getRate {
  85. return 1.0;
  86. }
  87. - (void)resetTimerPlay {
  88. if (_timer) {
  89. [self resetTimer];
  90. MJWeakSelf;
  91. _timer = [NSTimer scheduledTimerWithTimeInterval:self.timerLength repeats:YES block:^(NSTimer * _Nonnull timer) {
  92. [weakSelf timerAction];
  93. }];
  94. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  95. [_timer setFireDate:[NSDate distantFuture]];
  96. }
  97. }
  98. - (NSTimer *)timer{
  99. if (!_timer) {
  100. MJWeakSelf;
  101. _timer = [NSTimer scheduledTimerWithTimeInterval:self.timerLength repeats:YES block:^(NSTimer * _Nonnull timer) {
  102. [weakSelf timerAction];
  103. }];
  104. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  105. [_timer setFireDate:[NSDate distantFuture]];
  106. }
  107. return _timer;
  108. }
  109. - (void)timerAction {
  110. [self updateStep];
  111. }
  112. - (void)updateStep {
  113. NSLog(@"---- current no ------ %d ", self.currentNo);
  114. self.currentNo++;
  115. // 通知刷新
  116. if (self.delegate && [self.delegate respondsToSelector:@selector(updateSpotHightState:)]) {
  117. [self.delegate updateSpotHightState:self.currentNo];
  118. }
  119. }
  120. #pragma mark -- 重置定时器
  121. - (void)resetTimer{
  122. [self.timer setFireDate:[NSDate distantPast]];
  123. [_timer invalidate];
  124. _timer = nil;
  125. }
  126. - (void)removeAll{
  127. if (_timer) {
  128. [_timer invalidate];
  129. _timer = nil;
  130. }
  131. }
  132. - (void)createPlayer {
  133. self.player = [[KSMetronomePlayer alloc] init];
  134. self.player.delegate = self;
  135. }
  136. - (void)freePlayer {
  137. [self removeAll];
  138. if (self.player.isPlaying) {
  139. [self.player stopPlay];
  140. }
  141. _player = nil;
  142. }
  143. - (NSString *)getTypeString {
  144. return [NSString stringWithFormat:@"%zd", self.beatNumber];
  145. }
  146. - (void)beatChooseCallback:(BeatChangeCallback)callback {
  147. KSChoosePicker *pickerView = [[KSChoosePicker alloc] initWithTitle:@"节拍" sourceData:@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"] lastChooseIndex:self.lastChooseIndex sureButtonColor:HexRGB(0x1CACF1) chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) {
  148. self.lastChooseIndex = chooseIndex;
  149. NSInteger beatNumber = [returnValue integerValue];
  150. self.beatNumber = beatNumber;
  151. [self changeBeat];
  152. [self resetRyhthm];
  153. if (callback) {
  154. callback(beatNumber);
  155. }
  156. } cancel:^{
  157. }];
  158. [pickerView showPicker];
  159. }
  160. - (void)resetRyhthm {
  161. self.rhythmType = RHYTHM_TYPE_ONE_QUARTERNOTES;
  162. }
  163. - (void)rhythmChooseCallback:(RhythmChangeCallback)callback {
  164. RhythmChooseView *chooseView = [RhythmChooseView shareInstance];
  165. [chooseView configWithRhythmChooseViewWithPreChoose:METRONOME_MANAGER.rhythmType displayView:[NSObject getKeyWindow]];
  166. [chooseView chooseCallback:^(RHYTHM_TYPE type) {
  167. self.rhythmType = type;
  168. [self changeBeat];
  169. if (callback) {
  170. callback(type);
  171. }
  172. }];
  173. }
  174. - (void)changeBeat {
  175. self.timerLength = 60.0 / (1.0 * self.speed) / ([self getRate]);
  176. [self resetTimerPlay];
  177. [self stopPlay];
  178. }
  179. - (NSString *)imageWithType:(RHYTHM_TYPE)type {
  180. NSInteger index = type;
  181. NSArray *array = @[@"rhythm_quarterNote",@"rhythm_two_eighthNote",@"rhythm_quarter_riplet",@"rhythm_four_sixteenthNote",@"rhythm_quarter_and_eighth_riplet",@"rhythm_dottedEighth_and_sixteenthNote",@"rhythm_eighth_and_two_sixteenthNote"];
  182. return array[index];
  183. }
  184. - (void)resetDefaultConfig {
  185. [self configDefault];
  186. }
  187. @end