Swift app's audio don't match the brightness intensity of the image section randomly?

∥☆過路亽.° 提交于 2021-01-29 12:02:28

问题


I have this application that takes an image (under OpenCVWrapper) that is grayscale and turns the brightness of that specific section that's touched by the Apple Pencil into sound. The dark areas don't make any sound, the medium areas make a medium pitch, and the bright areas make a high pitch.

But for some reason the code works in some areas of the image flawlessly and in other it's almost reversed.

Code Below:

    //Produce Sound Based on Location of Touch Input
@objc func strokeAction(gesture: StrokeGestureRecognizer){
    if pencilStrokeRecognizer.state != .ended && pencilStrokeRecognizer.touchInImage == true {
        if !self.oscillator.isStarted {
            self.oscillator.start()
            self.oscillator2.start()
        }

        //Get pencil touch input location with respect to input image
        let x = pencilStrokeRecognizer.touchLocation.x
        let y = pencilStrokeRecognizer.touchLocation.y
        print("x:",x)
        print("y:",y)

        if self.unitCellMode == false {
            //Get Grayscale Value at Point User is Touching on Screen
            let grayVal = Double(OpenCVWrapper.getGrayVal(Int32(round(y)),Int32(round(x))))
            let grayUp = Double(OpenCVWrapper.getGrayVal(Int32(round(y - 33)),Int32(round(x))))


            //let grayN = (grayLeft + grayRight)/2
            let grayAvg = (grayVal + grayUp)/2
            //Set loudness of sound and
            //change frequency based on grayscale intensity value at the pixel
            self.oscillator.amplitude = 0.5

            print("grayVal:",grayVal )
            print("grayAvg:",grayAvg)



            if grayVal < 10000.0 {
                self.oscillator.amplitude = 0.0
                self.oscillator2.amplitude = 0.0
            } else if grayVal < 23000.0 && grayAvg < 15000.0 {
                //Discord
                self.oscillator.baseFrequency = grayVal/70.0
                self.oscillator2.amplitude = 0.0
            } else {
                //Harmony
                self.oscillator.amplitude = 0.3
                self.oscillator.baseFrequency = 440.0
                self.oscillator2.baseFrequency = 330.0

            }

        } else if x-18 >= 0 && y-15 >= 0 && x+18 < image.size.width && y+15 < image.size.height {

            //Atoms of center rhombus
            let atom1 = Double(OpenCVWrapper.getGrayVal(Int32(round(y)),Int32(round(x-18))))
            let atom2 = Double(OpenCVWrapper.getGrayVal(Int32(round(y)),Int32(round(x+18))))
            let atom3 = Double(OpenCVWrapper.getGrayVal(Int32(round(y-15)),Int32(round(x))))
            let atom4 = Double(OpenCVWrapper.getGrayVal(Int32(round(y+15)),Int32(round(x))))


            //Atoms of outer-most rhombus
            let atom5 = Double(OpenCVWrapper.getGrayVal(Int32(round(y)),Int32(round(x-39))))
            let atom6 = Double(OpenCVWrapper.getGrayVal(Int32(round(y)),Int32(round(x+39))))
            let atom7 = Double(OpenCVWrapper.getGrayVal(Int32(round(y-60)),Int32(round(x))))
            let atom8 = Double(OpenCVWrapper.getGrayVal(Int32(round(y+60)),Int32(round(x))))

            //Lone Pairs one
            let atom9 = Double(OpenCVWrapper.getGrayVal(Int32(round(y-24)),Int32(round(x-28))))
            let atom10 = Double(OpenCVWrapper.getGrayVal(Int32(round(y-24)),Int32(round(x+28))))
            let atom11 = Double(OpenCVWrapper.getGrayVal(Int32(round(y+24)),Int32(round(x-28))))
            let atom12 = Double(OpenCVWrapper.getGrayVal(Int32(round(y+24)),Int32(round(x+28))))

            //Lone Pairs two
            let atom13 = Double(OpenCVWrapper.getGrayVal(Int32(round(y-37)),Int32(round(x-11))))
            let atom14 = Double(OpenCVWrapper.getGrayVal(Int32(round(y-37)),Int32(round(x+11))))
            let atom15 = Double(OpenCVWrapper.getGrayVal(Int32(round(y+37)),Int32(round(x-11))))
            let atom16 = Double(OpenCVWrapper.getGrayVal(Int32(round(y+37)),Int32(round(x+11))))

            let grayVal = (atom1+atom2+atom3+atom4+atom5+atom6+atom7+atom8+atom9+atom10+atom11+atom12+atom13+atom14+atom15+atom16)/16

            self.oscillator.amplitude = 0.7
            self.oscillator.baseFrequency = grayVal/120.0
        }
    } else {
        //If touch input has ended, make oscillator quiet
        self.oscillator.amplitude = 0.0
        self.oscillator2.amplitude = 0.0
    }
}

This is the image it's processing

Overall it returns no errors but for some reason it doesn't seem to work.

来源:https://stackoverflow.com/questions/60749240/swift-apps-audio-dont-match-the-brightness-intensity-of-the-image-section-rand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!