Аудио в приложении Swift случайно не соответствует яркости раздела изображения? - PullRequest
0 голосов
/ 19 марта 2020

У меня есть это приложение, которое берет изображение (в OpenCVWrapper) в оттенках серого и преобразует яркость указанного фрагмента c, который был затронут Apple Pencil, в звук. Темные области не издают никаких звуков, средние области создают среднюю высоту звука, а яркие области - высокую высоту звука.

Но по некоторым причинам код работает в некоторых областях изображения безупречно, а в других он почти полностью изменен.

Код ниже:

    //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
    }
}

Это изображение, которое он обрабатывает

В целом, оно не возвращает ошибок, но по какой-то причине не кажется работать.

...