トップ «前の日記(2016-09-14) 最新 次の日記(2016-10-29)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|

2016-09-22 [macOS]Text To Speech

今回は、以前のアプリを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
    }
}

_ 【Cocoa練習帳】

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

トップ «前の日記(2016-09-14) 最新 次の日記(2016-10-29)» 編集