2

This association on line 5 does not contribute to the success of the app. But I honestly do not understand what happens at that moment.

Overloads for ÜnsafeMutablePointer exist with partially matching parameter list: (RawPointer).

But what does it mean? Thanks

override func buffer(withCsound cs: CsoundObj) -> Data {
            let length    = Int(AKSettings.shared().numberOfChannels) *
                            Int(AKSettings.shared().samplesPerControlPeriod) * 4
            let num       = length / 4
            let floats    = UnsafeMutablePointer<Float>(malloc(length))

            /* The phase and amplitude are different for each line to get a nice
             * gimmick. */
            let phase     = (self.amplifier + 0.8) / 1.8

            for i in 0 ... num - 1 {
                /* The amplitude is placed within the for-loop because it can fade
                 * to a slightly different value during one plot refresh. */
                let amplitude = self.amplifier * self.amplitude

                /* It is incredibly important that `time` and `phase` aren't
                 * multiplied with the frequency or else it will bump at each
                 * frequency change. */
                var t = (time + Double(i) / Double(num) * self.frequency + phase)

                floats[i] = Float(sin(t * 2 * 3.14))

                /* It is multiplied with a "regular" 0.5 Hz sine to get both ends
                 * to fade out nicely. It's sort of a simplistic window function. */
                t = Double(i) / Double(num)
                floats[i] *= Float(sin(t * 1 * 3.14) * amplitude)
                floats[i] *= 1 - pow(1 - Float(i) / Float(num), 2.0)

                time += self.frequency / 44100 / 2

                /* Fade smoothly to the next frequency and amplitude. */
                self.frequency += (nextFrequency - self.frequency) / 44100.0 / 4.0
                self.amplitude += (nextAmplitude - self.amplitude) / 44100.0 / 2.0
            }

            /* We keep the time between 0 and 1 to make sure it never overflows /
             * loses the necessary precision. */
            time = fmod(time, 1.0)

            return Data(bytesNoCopy: UnsafeMutablePointer<UInt8>(floats), count: length, deallocator: .free)
        }
Jaap
  • 81,064
  • 34
  • 182
  • 193
Otávio Souza
  • 31
  • 1
  • 4

2 Answers2

2

You can find some similar articles searching with the error message.

For example: How to use UnsafeMutablePointer in Swift 3?

And you'd better read MIGRATING TO SWIFT 3, especially this article.


To be specific to your case.

Change this line:

let floats    = UnsafeMutablePointer<Float>(malloc(length))

to:

let rawBytes  = malloc(length)!
let floats    = rawBytes.assumingMemoryBound(to: Float.self)

And change the last line:

return Data(bytesNoCopy: UnsafeMutablePointer<UInt8>(floats), count: length, deallocator: .free)

to:

return Data(bytesNoCopy: rawBytes, count: length, deallocator: .free)

Option 2.

Change the line let floats = UnsafeMutablePointer<Float>(malloc(length)) to:

var data = Data(count: length)
data.withUnsafeMutableBytes {(floats: UnsafeMutablePointer<Float>) in

And change the last line to:

}
return data

(All lines between them are enclosed in the closure {(floats: UnsafeMutablePointer<Float>) in ...}.)

Community
  • 1
  • 1
OOPer
  • 47,149
  • 6
  • 107
  • 142
-2
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
var buf = unsafeBitCast(baseAddress, to: UnsafeMutablePointer<UInt8>.self)
Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171