2015-08-30 9 views
8

Sto sviluppando un'applicazione di effetti audio su OSX usando Swift e sono interessato ad integrare un effetto pitch-shift.Spostamento del pitch in tempo reale con AVAudioEngine usando Swift

Mi piacerebbe in tempo reale, per modificare il tono in su o in giù di un'ottava. Attualmente sto ricevendo solo un segnale secco.

Non sono sicuro che questo sia possibile e vorrei sapere se questo è possibile o qualsiasi aiuto o suggerimento che qualcuno possa avere.

codice corrente rilevante per il problema è il seguente:

import Cocoa 
import AVFoundation 


class ViewController: NSViewController { 
     var engine = AVAudioEngine() 
     var timePitch = AVAudioUnitTimePitch() 


    override func viewDidLoad() { 
     timePitch.pitch = 1200 

     // Setup engine and node instances 
     var mixer = engine.mainMixerNode 
     var input = engine.inputNode 
     var output = engine.outputNode 
     var format = input.inputFormatForBus(0) 
     var error:NSError? 

     engine.attachNode(timePitch) 

     engine.connect(input, to: timePitch, format: format) 
     engine.connect(timePitch, to: output, format: format) 

     engine.startAndReturnError(&error) 

     super.viewDidLoad() 
    } 

    override var representedObject: AnyObject? { 
     didSet { 
     // Update the view, if already loaded. 
     } 
    } 


} 

risposta

4
timePitch.pitch = -500 //Rude man voice 
timePitch.rate = 1.5 //In 1.5 times faster 

Controllare this tutorial. E direct link ad esempio dal tutorial per maggiori informazioni.

Esempio per Swift 2.0:

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    var engine: AVAudioEngine! 
    var player: AVAudioPlayerNode! 

    var file = AVAudioFile() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     engine = AVAudioEngine() 
     player = AVAudioPlayerNode() 
     player.volume = 1.0 

     let path = NSBundle.mainBundle().pathForResource("in", ofType: "caf")! 
     let url = NSURL.fileURLWithPath(path) 

     let file = try? AVAudioFile(forReading: url) 
     let buffer = AVAudioPCMBuffer(PCMFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length)) 
     do { 
      try file!.readIntoBuffer(buffer) 
     } catch _ { 
     } 

     let pitch = AVAudioUnitTimePitch() 

     // 
     pitch.pitch = -500 //Distortion 
     pitch.rate = 1.5 //Voice speed 
     // 

     engine.attachNode(player) 

     engine.attachNode(pitch) 

     engine.connect(player, to: pitch, format: buffer.format) 

     engine.connect(pitch, to: engine.mainMixerNode, format: buffer.format) 
     player.scheduleBuffer(buffer, atTime: nil, options: AVAudioPlayerNodeBufferOptions.Loops, completionHandler: nil) 

     engine.prepare() 
     do { 
      try engine.start() 
     } catch _ { 
     } 

     player.play() 

    } 
} 
+1

Grazie mille per la condivisione. –

0
timePitch.pitch = 1000 //Filtered Voice 
timePitch.rate = 1 //Normal rate 
+0

Sebbene questo codice possa rispondere alla domanda, fornire un contesto aggiuntivo riguardo a come e/o perché risolve il problema migliorerebbe il valore a lungo termine della risposta. –

+0

grazie per il tuo suggerimento @Donald duck. lo terrò a mente la prossima volta –

Problemi correlati