MetronomeManager.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // MetronomeManager.m
  3. // KulexiuSchoolStudent
  4. //
  5. // Created by 王智 on 2023/7/24.
  6. //
  7. #import "MetronomeManager.h"
  8. #import <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. [self notiferInterruption];
  55. }
  56. - (void)notiferInterruption {
  57. [[NSNotificationCenter defaultCenter] postNotificationName:ERROR_NOTIFICATION object:nil];
  58. }
  59. - (void)startPlay {
  60. if (self.isPlaying == NO) {
  61. // 重置
  62. self.currentNo = -1;
  63. // int bpm = self.speed * [self getRate];
  64. [self.player playRhythmAction:self.speed rhythmType:self.rhythmType beatNumber:self.beatNumber];
  65. // 开始播放
  66. [self.timer setFireDate:[NSDate distantPast]];
  67. }
  68. self.isPlaying = YES;
  69. }
  70. - (void)stopPlay {
  71. if (self.isPlaying) {
  72. self.isPlaying = NO;
  73. [self.timer setFireDate:[NSDate distantFuture]];//暂停计时器
  74. self.currentNo = -1;
  75. [self.player stopPlay];
  76. }
  77. }
  78. - (void)setSpeed:(int)speed {
  79. _speed = speed;
  80. int rateFloat = speed;
  81. self.timerLength = 60.0 / (1.0 * rateFloat) / ([self getRate]);
  82. NSLog(@"---- %f",self.self.timerLength);
  83. [self resetTimerPlay];
  84. if (self.isPlaying) {
  85. [self stopPlay];
  86. [self startPlay];
  87. }
  88. }
  89. - (CGFloat)getRate {
  90. return 1.0;
  91. }
  92. - (void)resetTimerPlay {
  93. if (_timer) {
  94. [self resetTimer];
  95. MJWeakSelf;
  96. _timer = [NSTimer scheduledTimerWithTimeInterval:self.timerLength repeats:YES block:^(NSTimer * _Nonnull timer) {
  97. [weakSelf timerAction];
  98. }];
  99. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  100. [_timer setFireDate:[NSDate distantFuture]];
  101. }
  102. }
  103. - (NSTimer *)timer{
  104. if (!_timer) {
  105. MJWeakSelf;
  106. _timer = [NSTimer scheduledTimerWithTimeInterval:self.timerLength repeats:YES block:^(NSTimer * _Nonnull timer) {
  107. [weakSelf timerAction];
  108. }];
  109. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  110. [_timer setFireDate:[NSDate distantFuture]];
  111. }
  112. return _timer;
  113. }
  114. - (void)timerAction {
  115. [self updateStep];
  116. }
  117. - (void)updateStep {
  118. NSLog(@"---- current no ------ %d ", self.currentNo);
  119. self.currentNo++;
  120. // 通知刷新
  121. if (self.delegate && [self.delegate respondsToSelector:@selector(updateSpotHightState:)]) {
  122. [self.delegate updateSpotHightState:self.currentNo];
  123. }
  124. }
  125. #pragma mark -- 重置定时器
  126. - (void)resetTimer{
  127. [self.timer setFireDate:[NSDate distantPast]];
  128. [_timer invalidate];
  129. _timer = nil;
  130. }
  131. - (void)removeAll{
  132. if (_timer) {
  133. [_timer invalidate];
  134. _timer = nil;
  135. }
  136. }
  137. - (void)createPlayer {
  138. self.player = [[KSMetronomePlayer alloc] init];
  139. self.player.delegate = self;
  140. }
  141. - (void)freePlayer {
  142. [self removeAll];
  143. if (self.player.isPlaying) {
  144. [self.player stopPlay];
  145. }
  146. _player = nil;
  147. }
  148. - (NSString *)getTypeString {
  149. return [NSString stringWithFormat:@"%zd", self.beatNumber];
  150. }
  151. - (void)beatChooseCallback:(BeatChangeCallback)callback {
  152. 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) {
  153. self.lastChooseIndex = chooseIndex;
  154. NSInteger beatNumber = [returnValue integerValue];
  155. self.beatNumber = beatNumber;
  156. [self changeBeat];
  157. [self resetRyhthm];
  158. if (callback) {
  159. callback(beatNumber);
  160. }
  161. } cancel:^{
  162. }];
  163. [pickerView showPicker];
  164. }
  165. - (void)resetRyhthm {
  166. self.rhythmType = RHYTHM_TYPE_ONE_QUARTERNOTES;
  167. }
  168. - (void)rhythmChooseCallback:(RhythmChangeCallback)callback {
  169. RhythmChooseView *chooseView = [RhythmChooseView shareInstance];
  170. [chooseView configWithRhythmChooseViewWithPreChoose:METRONOME_MANAGER.rhythmType displayView:[NSObject getKeyWindow]];
  171. [chooseView chooseCallback:^(RHYTHM_TYPE type) {
  172. self.rhythmType = type;
  173. [self changeBeat];
  174. if (callback) {
  175. callback(type);
  176. }
  177. }];
  178. }
  179. - (void)changeBeat {
  180. self.timerLength = 60.0 / (1.0 * self.speed) / ([self getRate]);
  181. [self resetTimerPlay];
  182. [self stopPlay];
  183. }
  184. - (NSString *)imageWithType:(RHYTHM_TYPE)type {
  185. NSInteger index = type;
  186. 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"];
  187. return array[index];
  188. }
  189. - (void)resetDefaultConfig {
  190. [self configDefault];
  191. }
  192. @end