Steven 2 лет назад
Родитель
Сommit
f7c8919892

+ 50 - 28
KulexiuForStudent/KulexiuForStudent/Module/Widget/Controller/ToneTuningViewController.m

@@ -14,7 +14,6 @@
 #import <AVFoundation/AVFoundation.h>
 #import "TunerSettingView.h"
 #import "KSChoosePicker.h"
-//#import "KSGaugeView.h"
 
 @interface ToneTuningViewController ()<TunerDelegate>
 
@@ -49,6 +48,9 @@
 
 @property (nonatomic, strong) NSArray *picthDescArray;
 
+// A4 频率
+@property (nonatomic, assign) NSInteger A4Frequence;
+
 @end
 
 @implementation ToneTuningViewController
@@ -64,6 +66,7 @@
 
 - (void)configDefault {
     self.chooseFrequence = 440;
+    self.A4Frequence = 440;
     self.isTransfer = NO;
 }
 
@@ -94,17 +97,12 @@
     }];
 
     [self.view addSubview:self.bodyView];
-    self.bodyView.A4Frequence = self.chooseFrequence;
+    self.bodyView.A4Frequence = self.A4Frequence;
     [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.mas_equalTo(self.view);
         make.top.mas_equalTo(self.navView.mas_bottom);
         make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin);
     }];
-//    KSGaugeView *gaugeView = [[KSGaugeView alloc] init];
-//    [self.bodyView.plateView addSubview:gaugeView];
-//    [gaugeView mas_makeConstraints:^(MASConstraintMaker *make) {
-//        make.top.bottom.left.right.mas_equalTo(self.bodyView.plateView);
-//    }];
     [self.bodyView.plateView addSubview:self.plateView];
     [self.plateView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.bottom.left.right.mas_equalTo(self.bodyView.plateView);
@@ -179,29 +177,29 @@
         
     }
     else {
-//        if (fabs(output.distance) > 1) {
-            self.plateView.gaugeView.value = (NSInteger)output.distance;
-        self.bodyView.distance = (NSInteger)output.distance;
-//        }
-        
-//        [self.plateView.gaugeView setValue:output.distance animated:YES];
         
+        TunerOutput *transferOutput = [tuner getTransferPitchWithA4Frequency:self.A4Frequence toneChangeRate:self.toneChangeRate output:output];
+
+        self.plateView.gaugeView.value = (NSInteger)transferOutput.distance;
+        self.bodyView.distance = (NSInteger)transferOutput.distance;
         
         // 调整频率后
-        
+
         if (self.isTransfer == NO) {
+            
             self.bodyView.nomalPitch.text = output.pitch;
             self.bodyView.pitchFrequenceLabel.text = [NSString stringWithFormat:@"%@%zd:%.0fHz", output.pitch, output.octave, output.frequency];
             self.bodyView.preFrequence.text = [NSString stringWithFormat:@"%@%zd",output.prePitch, output.preOctave];
             self.bodyView.nextFrequence.text = [NSString stringWithFormat:@"%@%zd",output.nextPitch, output.nextOctave];
         }
         else {
-            self.bodyView.transferNomalPitch.text = output.pitch;
+            
+            self.bodyView.transferNomalPitch.text = transferOutput.pitch;
             self.bodyView.transferDesc.text = self.settingView.transferPicthDesc;
-//            self.bodyView.transferPitch.text = 
-            self.bodyView.pitchFrequenceLabel.text = [NSString stringWithFormat:@"%@%zd:%.0fHz", output.pitch, output.octave, output.frequency];
-            self.bodyView.preFrequence.text = [NSString stringWithFormat:@"%@%zd",output.prePitch, output.preOctave];
-            self.bodyView.nextFrequence.text = [NSString stringWithFormat:@"%@%zd",output.nextPitch, output.nextOctave];
+            self.bodyView.transferPitch.text = [NSString stringWithFormat:@"%@",transferOutput.transferPitch];
+            self.bodyView.pitchFrequenceLabel.text = [NSString stringWithFormat:@"%@%zd:%.0fHz", transferOutput.transferPitch, transferOutput.transferOctave, transferOutput.frequency];
+            self.bodyView.preFrequence.text = [NSString stringWithFormat:@"%@%zd",transferOutput.prePitch, transferOutput.preOctave];
+            self.bodyView.nextFrequence.text = [NSString stringWithFormat:@"%@%zd",transferOutput.nextPitch, transferOutput.nextOctave];
             
         }
     }
@@ -231,9 +229,14 @@
             break;
         case TUNINGACTION_FREQUENCE: // 调整频率
         {
-            self.chooseFrequence = frequence;
+            self.A4Frequence = frequence;
             self.settingView.A4Frequence = frequence;
             self.tuner.A4Frequence = frequence;
+            self.forkView.A4Frequence = frequence;
+            if (self.bodyView.isPlaying) {
+                CGFloat changefrequence = frequence / 440.0 * self.chooseFrequence;
+                [self.forkManager changeFrequenceWithFrequence:changefrequence];
+            }
         }
             break;
         case TUNINGACTION_PLAY:      // 播放音叉
@@ -252,7 +255,9 @@
 }
 
 - (void)startForkPlay {
-    [self.forkManager setupWithFrequence:self.chooseFrequence];
+    
+    CGFloat changefrequence = self.A4Frequence / 440.0 * self.chooseFrequence;
+    [self.forkManager setupWithFrequence:changefrequence];
 }
 
 - (void)stopForkPlay {
@@ -273,13 +278,24 @@
         _forkView = [TuningForkSettingView shareInstance];
         _forkView.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight);
         MJWeakSelf;
-        [_forkView forkSettingAction:^(NSInteger frequence) {
+        [_forkView forkSettingAction:^(NSInteger frequence, BOOL isSure) {
             NSLog(@"----- frequence %zd", frequence);
+            
             weakSelf.chooseFrequence = frequence;
-            if (weakSelf.forkManager.isPlay) {
-                [weakSelf.forkManager changeFrequenceWithFrequence:frequence];
+            if (weakSelf.bodyView.isPlaying) {
+                CGFloat changefrequence = frequence / 440.0 * self.A4Frequence;
+                [weakSelf.forkManager changeFrequenceWithFrequence:changefrequence];
+            }
+            else {
+                if (isSure) {
+                    [weakSelf stopForkPlay];
+                }
+                else {
+                    [weakSelf startForkPlay];
+                }
             }
         }];
+
     }
     return _forkView;
 }
@@ -294,7 +310,7 @@
 - (TunerSettingView *)settingView {
     if (!_settingView) {
         _settingView = [TunerSettingView shareInstance];
-        _settingView.A4Frequence = self.chooseFrequence;
+        _settingView.A4Frequence = self.A4Frequence;
         _settingView.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight);
         MJWeakSelf;
         [_settingView tunerSettingAction:^(TUNETSETTING_ACTION action, NSInteger toneChangeRate, NSInteger frequence) {
@@ -312,6 +328,7 @@
         self.instrumentPicker = [[KSChoosePicker alloc] initWithTitle:@"" sourceData:self.instrumentArray chooseReturnWithBlock:^(NSString * _Nonnull returnValue, NSInteger chooseIndex) {
             weakSelf.settingView.instrumentLabel.text = returnValue;
             NSInteger pitchRate = [self.picthRateArray[chooseIndex] integerValue];
+            weakSelf.A4Frequence = frequence;
             weakSelf.settingView.toneChangeRate = pitchRate;
             weakSelf.settingView.transferPicthDesc = self.picthDescArray[chooseIndex];
         } cancel:^{
@@ -321,10 +338,15 @@
     }
     else if (action == TUNETSETTING_ACTION_SURE) {
         // 设置成功
-        self.chooseFrequence = frequence;
+        self.A4Frequence = frequence;
         self.bodyView.A4Frequence = frequence;
         self.toneChangeRate = toneChangeRate;
         self.tuner.A4Frequence = frequence;
+        self.forkView.A4Frequence = frequence;
+        if (self.bodyView.isPlaying) {
+            CGFloat changefrequence = frequence / 440.0 * self.chooseFrequence;
+            [self.forkManager changeFrequenceWithFrequence:changefrequence];
+        }
     }
 }
 
@@ -337,14 +359,14 @@
 
 - (NSArray *)picthRateArray {
     if (!_picthRateArray) {
-        _picthRateArray = @[@0,@1,@8,@1,@0,@-12,@1,@0,@-5,@1,@0,@1];
+        _picthRateArray = @[@0,@-2,@-9,@-2,@0,@-12,@-2,@0,@5,@-2,@0,@-2];
     }
     return _picthRateArray;
 }
 
 - (NSArray *)picthDescArray {
     if (!_picthDescArray) {
-        _picthDescArray = @[@"C ins.",@"B ins.",@"E ins.",@"B ins.",@"C ins.",@"C调大调 ins.",@"B ins.",@"C ins.",@"F ins.",@"C ins.",@"B ins."];
+        _picthDescArray = @[@"C ins.",@"B ins.",@"E ins.",@"B ins.",@"C ins.",@"C调大调 ins.",@"B ins.",@"C ins.",@"F ins.",@"C ins.",@"B ins."];
     }
     return _picthDescArray;
 }

+ 55 - 13
KulexiuForStudent/KulexiuForStudent/Module/Widget/Model/TuningFunction/Tuner.swift

@@ -65,23 +65,25 @@ private let frequencies: [Float] = [
     @objc public fileprivate(set) var prePitch: String = ""
     
     /**
-     The octave of the interpreted  mid pitch.
+     The octave of the interpreted  next pitch.
      */
-    @objc public fileprivate(set) var midOctave: Int = 0
+    @objc public fileprivate(set) var nextOctave: Int = 0
+    
     /**
-     The interpreted mid pitch of the microphone audio.
+     The interpreted next pitch of the microphone audio.
      */
-    @objc public fileprivate(set) var midPitch: String = ""
+    @objc public fileprivate(set) var nextPitch: String = ""
     
     /**
-     The octave of the interpreted  next pitch.
+     The octave of the interpreted  transfer pitch.
      */
-    @objc public fileprivate(set) var nextOctave: Int = 0
+    @objc public fileprivate(set) var transferOctave: Int = 0
     
     /**
-     The interpreted next pitch of the microphone audio.
+     The interpreted transfer pitch of the microphone audio.
      */
-    @objc public fileprivate(set) var nextPitch: String = ""
+    @objc public fileprivate(set) var transferPitch: String = ""
+    
     /**
      The difference between the frequency of the interpreted pitch and the actual
      frequency of the microphone audio.
@@ -250,15 +252,55 @@ private let frequencies: [Float] = [
         
         output.preOctave = (index) / 12
         output.prePitch = String(format: "%@", sharps[(index) % sharps.count], flats[(index) % flats.count])
-        output.midOctave = (index+1) / 12
-        output.midPitch = String(format: "%@", sharps[(index+1) % sharps.count], flats[(index+1) % flats.count])
+
         output.nextOctave = (index+2) / 12
         output.nextPitch = String(format: "%@", sharps[(index+2) % sharps.count], flats[(index+2) % flats.count])
         return output
     }
     
-//    @objc public func getTransferPitch(frequency: Float, toneChangeRate: Int)-> {
-//       
-//    }
+    @objc public func getTransferPitch(A4Frequency: Float, toneChangeRate: Int, output: TunerOutput)->TunerOutput {
+        let newOutput = TunerOutput()
+        // 将收到的频率转成 440hz频率后对比
+        var norm = output.frequency / A4Frequency * 440;
+        while norm > frequencies[frequencies.count - 1] {
+            norm = norm / 2.0
+        }
+        while norm < frequencies[0] {
+            norm = norm * 2.0
+        }
+        
+        var i = -1
+        var min = Float.infinity
+        for n in 0...frequencies.count-1 {
+            let diff = frequencies[n] - norm
+            if abs(diff) < abs(min) {
+                min = diff
+                i = n
+            }
+        }
+        
+        var index = i-1+toneChangeRate
+        if index <= 0 {
+            index = 0
+        } else if index+2 >= frequencies.count-1 {
+            index = frequencies.count-2
+        }
+        
+        newOutput.octave = output.octave
+        newOutput.frequency = output.frequency
+        newOutput.amplitude = output.amplitude
+        newOutput.distance = output.frequency / A4Frequency * 440 - frequencies[i]
+        newOutput.pitch = output.pitch
+        
+        newOutput.preOctave = (index) / 12
+        newOutput.prePitch = String(format: "%@", sharps[(index) % sharps.count], flats[(index) % flats.count])
+        
+        newOutput.nextOctave = (index+2) / 12
+        newOutput.nextPitch = String(format: "%@", sharps[(index+2) % sharps.count], flats[(index+2) % flats.count])
+        // 移调之后的显示
+        newOutput.transferOctave = (index+1) / 12
+        newOutput.transferPitch = String(format: "%@", sharps[(index+1) % sharps.count], flats[(index+1) % flats.count])
+        return newOutput
+    }
     
 }

+ 9 - 9
KulexiuForStudent/KulexiuForStudent/Module/Widget/View/toneTuning/ToneTuningBodyView.xib

@@ -44,10 +44,10 @@
                     </constraints>
                 </view>
                 <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9aU-LL-Lsq">
-                    <rect key="frame" x="160" y="204" width="70" height="70"/>
+                    <rect key="frame" x="155" y="194" width="80" height="80"/>
                     <subviews>
                         <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="0¢" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="D7p-nE-fr1">
-                            <rect key="frame" x="15.666666666666657" y="19.666666666666657" width="39" height="31"/>
+                            <rect key="frame" x="20.666666666666657" y="24.666666666666657" width="39" height="31"/>
                             <fontDescription key="fontDescription" name="DINAlternate-Bold" family="DIN Alternate" pointSize="26"/>
                             <color key="textColor" red="0.0" green="1" blue="0.81176470588235294" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                             <nil key="highlightedColor"/>
@@ -55,14 +55,14 @@
                     </subviews>
                     <color key="backgroundColor" red="0.062745098039215685" green="0.074509803921568626" blue="0.094117647058823528" alpha="1" colorSpace="calibratedRGB"/>
                     <constraints>
-                        <constraint firstAttribute="height" constant="70" id="EVn-sM-7ft"/>
+                        <constraint firstAttribute="height" constant="80" id="EVn-sM-7ft"/>
                         <constraint firstItem="D7p-nE-fr1" firstAttribute="centerY" secondItem="9aU-LL-Lsq" secondAttribute="centerY" id="YyQ-A4-OY3"/>
-                        <constraint firstAttribute="width" constant="70" id="gV3-KZ-iAV"/>
+                        <constraint firstAttribute="width" constant="80" id="gV3-KZ-iAV"/>
                         <constraint firstItem="D7p-nE-fr1" firstAttribute="centerX" secondItem="9aU-LL-Lsq" secondAttribute="centerX" id="rVT-76-xxU"/>
                     </constraints>
                     <userDefinedRuntimeAttributes>
                         <userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
-                            <real key="value" value="35"/>
+                            <real key="value" value="40"/>
                         </userDefinedRuntimeAttribute>
                     </userDefinedRuntimeAttributes>
                 </view>
@@ -290,7 +290,7 @@
                     </constraints>
                 </imageView>
                 <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5sT-Eb-2rJ">
-                    <rect key="frame" x="75" y="254" width="0.0" height="14"/>
+                    <rect key="frame" x="65" y="254" width="0.0" height="14"/>
                     <constraints>
                         <constraint firstAttribute="height" constant="14" id="g2f-fr-DrN"/>
                     </constraints>
@@ -299,7 +299,7 @@
                     <nil key="highlightedColor"/>
                 </label>
                 <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ke2-CX-K7G">
-                    <rect key="frame" x="315" y="254" width="0.0" height="14"/>
+                    <rect key="frame" x="325" y="254" width="0.0" height="14"/>
                     <constraints>
                         <constraint firstAttribute="height" constant="14" id="Csw-Dx-b3r"/>
                     </constraints>
@@ -340,7 +340,7 @@
                 <constraint firstItem="p0z-qL-bMm" firstAttribute="top" secondItem="7Em-FC-3EL" secondAttribute="bottom" constant="12" id="4SR-6A-ipm"/>
                 <constraint firstItem="p0z-qL-bMm" firstAttribute="leading" secondItem="Tkl-Cw-Mdz" secondAttribute="trailing" id="5EB-Vw-dj2"/>
                 <constraint firstItem="Cai-ev-v5E" firstAttribute="leading" secondItem="v4Z-dA-iqI" secondAttribute="trailing" constant="22" id="6b8-ki-CY9"/>
-                <constraint firstItem="Ke2-CX-K7G" firstAttribute="trailing" secondItem="Zrx-Pe-2NT" secondAttribute="trailing" constant="-50" id="6hN-re-nBS"/>
+                <constraint firstItem="Ke2-CX-K7G" firstAttribute="trailing" secondItem="Zrx-Pe-2NT" secondAttribute="trailing" constant="-40" id="6hN-re-nBS"/>
                 <constraint firstItem="7Em-FC-3EL" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="14" id="7ZV-OX-B1b"/>
                 <constraint firstItem="Cai-ev-v5E" firstAttribute="centerY" secondItem="v4Z-dA-iqI" secondAttribute="centerY" id="8Eb-SN-txF"/>
                 <constraint firstItem="IXi-hL-in1" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="Au1-eF-R25"/>
@@ -371,7 +371,7 @@
                 <constraint firstItem="7Em-FC-3EL" firstAttribute="bottom" secondItem="ZeX-qM-LJD" secondAttribute="bottom" id="coV-Qi-RkQ"/>
                 <constraint firstItem="Ux8-2g-RqK" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="22" id="cwN-DO-wkV"/>
                 <constraint firstItem="VJU-LE-Pdq" firstAttribute="bottom" secondItem="uwR-ra-cDH" secondAttribute="bottom" id="e0m-sL-feH"/>
-                <constraint firstItem="5sT-Eb-2rJ" firstAttribute="leading" secondItem="Zrx-Pe-2NT" secondAttribute="leading" constant="50" id="eBI-aU-v4Z"/>
+                <constraint firstItem="5sT-Eb-2rJ" firstAttribute="leading" secondItem="Zrx-Pe-2NT" secondAttribute="leading" constant="40" id="eBI-aU-v4Z"/>
                 <constraint firstItem="Ux8-2g-RqK" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="10" id="ev8-FX-ve4"/>
                 <constraint firstItem="VJU-LE-Pdq" firstAttribute="leading" secondItem="uwR-ra-cDH" secondAttribute="leading" id="hAr-OA-5ce"/>
                 <constraint firstItem="5sT-Eb-2rJ" firstAttribute="top" secondItem="Zrx-Pe-2NT" secondAttribute="bottom" id="ixH-ow-czJ"/>

+ 1 - 1
KulexiuForStudent/KulexiuForStudent/Module/Widget/View/toneTuning/TuningForkSettingView.h

@@ -7,7 +7,7 @@
 
 #import <UIKit/UIKit.h>
 
-typedef void(^ForkSettingCallback)(NSInteger frequence);
+typedef void(^ForkSettingCallback)(NSInteger frequence, BOOL isSure);
 
 NS_ASSUME_NONNULL_BEGIN
 

+ 36 - 1
KulexiuForStudent/KulexiuForStudent/Module/Widget/View/toneTuning/TuningForkSettingView.m

@@ -71,6 +71,14 @@
 - (void)showView {
     [[NSObject getKeyWindow] addSubview:self];
     [self setPopAnimation];
+    if (self.callback) {
+        NSInteger index = self.musicalScale * 12 + self.chooseIndex;
+        if (self.frequenceArray.count > index) {
+            NSInteger frequence = [self.frequenceArray[index] integerValue];
+            frequence = (NSInteger)(frequence * _A4Frequence / 440.0);
+            self.callback(frequence,NO);
+        }
+    }
 }
 
 - (void)removeView {
@@ -152,6 +160,14 @@
     UIImageView *chooseImageView = self.imageArray[index];
     [chooseImageView setImage:[UIImage imageNamed:@"fork_pitchChoosed"]];
     self.chooseIndex = index;
+    if (self.callback) {
+        NSInteger index = self.musicalScale * 12 + self.chooseIndex;
+        if (self.frequenceArray.count > index) {
+            NSInteger frequence = [self.frequenceArray[index] integerValue];
+//            frequence = (NSInteger)(frequence * _A4Frequence / 440.0);
+            self.callback(frequence,NO);
+        }
+    }
 }
 
 
@@ -162,6 +178,15 @@
         return;
     }
     self.musicalScale++;
+    if (self.callback) {
+        // 获取频率
+        NSInteger index = self.musicalScale * 12 + self.chooseIndex;
+        if (self.frequenceArray.count > index) {
+            NSInteger frequence = [self.frequenceArray[index] integerValue];
+//            frequence = (NSInteger)(frequence * _A4Frequence / 440.0);
+            self.callback(frequence,NO);
+        }
+    }
 }
 // 音阶-
 - (IBAction)musicalScaleMinus:(id)sender {
@@ -169,6 +194,15 @@
         return;
     }
     self.musicalScale--;
+    if (self.callback) {
+        // 获取频率
+        NSInteger index = self.musicalScale * 12 + self.chooseIndex;
+        if (self.frequenceArray.count > index) {
+            NSInteger frequence = [self.frequenceArray[index] integerValue];
+//            frequence = (NSInteger)(frequence * _A4Frequence / 440.0);
+            self.callback(frequence,NO);
+        }
+    }
 }
 
 // 设置保存
@@ -178,7 +212,8 @@
         NSInteger index = self.musicalScale * 12 + self.chooseIndex;
         if (self.frequenceArray.count > index) {
             NSInteger frequence = [self.frequenceArray[index] integerValue];
-            self.callback(frequence);
+//            frequence = (NSInteger)(frequence * _A4Frequence / 440.0);
+            self.callback(frequence,YES);
         }
     }
     [self removeView];