LoginSignup
0

More than 5 years have passed since last update.

[macOS]Text To Speech

Posted at

今回は、以前のアプリをSwift化するだけの内容だ。

テキストの内容を読み上げるが、それにディレイをかける。

import Cocoa
import AudioUnit
import AudioToolbox
import CoreAudioKit
 
class ViewController: NSViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        demo()
    }
    
    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    var auGraph: AUGraph? = nil
    
    func demo() {
        var inputNode: AUNode = 0
        var effectNode: AUNode = 0
        var outputNode: AUNode = 0
        
        NewAUGraph(&auGraph);
        
        var cd = AudioComponentDescription()
        cd.componentType = kAudioUnitType_Generator
        cd.componentSubType = kAudioUnitSubType_SpeechSynthesis
        cd.componentManufacturer = kAudioUnitManufacturer_Apple
        cd.componentFlags = 0
        cd.componentFlagsMask = 0
        
        AUGraphAddNode(auGraph!, &cd, &inputNode)
        
        cd.componentType = kAudioUnitType_Effect
        cd.componentSubType = kAudioUnitSubType_Delay
        AUGraphAddNode(auGraph!, &cd, &effectNode)
        
        cd.componentType = kAudioUnitType_Output
        cd.componentSubType = kAudioUnitSubType_DefaultOutput
        AUGraphAddNode(auGraph!, &cd, &outputNode)
        
        AUGraphConnectNodeInput(auGraph!, inputNode, 0, effectNode, 0)
        AUGraphConnectNodeInput(auGraph!, effectNode, 0, outputNode, 0)
        
        AUGraphOpen(auGraph!)
        AUGraphInitialize(auGraph!)
        
        var generateAudioUnit: AudioUnit? = nil
        AUGraphNodeInfo(auGraph!, inputNode, nil, &generateAudioUnit)
        var channel: SpeechChannel? = nil
        var sz: UInt32 = UInt32(MemoryLayout.size)
        AudioUnitGetProperty(generateAudioUnit!, kAudioUnitProperty_SpeechChannel, kAudioUnitScope_Global, 0, &channel, &sz)
        
        AUGraphStart(auGraph!)
        
        SpeakCFString(channel!, "Nice to meet you. It's nice to see you! Nice meeting you. I'm pleased to meet you. Please say hello to your family. I look forward to seeing you again. Yes. Let's get together soon." as NSString, nil)
    }
    
    func dispose() {
        AUGraphStop(auGraph!)
        AUGraphUninitialize(auGraph!)
        AUGraphClose(auGraph!)
        DisposeAUGraph(auGraph!)
        auGraph = nil
    }
}

関連情報
[Mac]談話と特殊効果(Text-to-Speech)

https://github.com/murakami/TextToSpeech

【Cocoa練習帳】
http://www.bitz.co.jp/weblog/

http://ameblo.jp/bitz/(ミラー・サイト)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0