Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MacOSX控制台应用程序可在不降低质量的情况下减慢或加速语音_Macos_Avaudioengine - Fatal编程技术网

MacOSX控制台应用程序可在不降低质量的情况下减慢或加速语音

MacOSX控制台应用程序可在不降低质量的情况下减慢或加速语音,macos,avaudioengine,Macos,Avaudioengine,我有一个只包含语音的.mp3文件,我想在保持相同清晰度的同时调整速度(较慢或较快)。没有花栗鼠!然后将修改后的文件写入磁盘。为了做到这一点,我尝试使用AVAudioEngine框架,但我是该框架的完整N00B。我发现,所有的例子都是为了修改音乐或录制声音,然后播放声音。我只是想转换到一个不同的速度,并有它吐出修改后的文件。到目前为止,我只知道这些,我在代码的注释中记录了我的困惑 import Foundation import AVFoundation import AVKit print(&

我有一个只包含语音的.mp3文件,我想在保持相同清晰度的同时调整速度(较慢或较快)。没有花栗鼠!然后将修改后的文件写入磁盘。为了做到这一点,我尝试使用AVAudioEngine框架,但我是该框架的完整N00B。我发现,所有的例子都是为了修改音乐或录制声音,然后播放声音。我只是想转换到一个不同的速度,并有它吐出修改后的文件。到目前为止,我只知道这些,我在代码的注释中记录了我的困惑

import Foundation
import AVFoundation
import AVKit

print("Change Voice Speed")

let engine = AVAudioEngine()
let speedControl = AVAudioUnitVarispeed()
let originalFile = URL(fileURLWithPath: "/Users/User/Desktop/speech.mp3")

do {
   // What do I do with the input file ?
   let file = try AVAudioFile(forReading: originalFile)
} catch let error as NSError {
    print("There's an error: \(error)")
}

speedControl.rate += 0.1
engine.attach(speedControl)
// Do I need to attach a AVAudioRecorder and record here ?

print("Write New File")

let outputFile = URL(fileURLWithPath: "/Users/User/Desktop/modifiedSpeech.mp3")


// Don't know what settings to put for a .mp3 file are. Also not
// sure if this actually writes the file to disk

do {
   try AVAudioFile(forWriting: outputFile, settings: <#T##[String : Any]#>)
} catch let error as NSError {
    print("There's an error: \(error)")
}

<代码>导入基础 进口AVF基金会 进口AVKit 打印(“更改语音速度”) 让发动机=AVAudioEngine() 设speedControl=AVAudioUnitVarispeed() 让originalFile=URL(fileURLWithPath:“/Users/User/Desktop/speech.mp3”) 做{ //如何处理输入文件? let file=尝试使用AVAudioFile(forReading:originalFile) }将let错误捕获为NSError{ 打印(“出现错误:\(错误)”) } speedControl.rate+=0.1 发动机。连接(速度控制) //我需要在这里附加一个音频录像机和录音吗? 打印(“写入新文件”) 让outputFile=URL(fileURLWithPath:“/Users/User/Desktop/modifiedSpeech.mp3”) //不知道要为.mp3文件设置什么。也不是 //确定这是否真的将文件写入磁盘 做{ 尝试AVAudioFile(用于写入:outputFile,设置:) }将let错误捕获为NSError{ 打印(“出现错误:\(错误)”) }
这是一个概念验证代码,可以加速立体声M4A文件并将其保存为WAVE文件。有最小的错误处理,并没有尝试处理不同的频道布局,但希望它应该让您开始:

let engine=AVAudioEngine()
//使用AVAUDIOUNITTIMEPOINT避免音调偏移
设timePitch=AVAudioUnitTimePitch()
让player=AVAudioPlayerNode()
发动机。连接(时距)
引擎。连接(播放器)
引擎连接(播放器,到:时间音高,格式:无)
engine.connect(时间间隔,到:engine.mainMixerNode,格式:nil)
//将文件速度提高1.5倍
TIMEPOITCH.rate=1.5
//使用512帧的块在手动渲染模式下运行引擎
让渲染化:AVAudioFrameCount=512
让renderFormat=AVAudioFormat(commonFormat:.pcmFormatInt16,sampleRate:44100.0,通道:2,交错:true)!
让renderBuffer=avaudiopcBuffer(pcmFormat:renderFormat,frameCapacity:renderSize)!
尝试engine.enableManualRenderingMode(.offline,格式:renderFormat,最大帧数:renderBuffer.frameCapacity)
尝试引擎启动()
player.play()
//渲染格式也是输出格式
让输出=尝试!AVAudioFile(用于写入:URL(fileURLWithPath:“/tmp/foo.wav”),设置:renderFormat.settings,commonFormat:renderFormat.commonFormat,交错:renderFormat.isInterleaved)
让file=试试!AVAudioFile(用于读取:URL(fileURLWithPath:“/tmp/test.m4a”))
//使用大小为“renderize”输出帧的缓冲区进行读取
让readSize=AVAudioFrameCount(浮点(渲染化)*timePitch.rate)
让readBuffer=avaudiopcbuffer(pcmFormat:file.processingFormat,frameCapacity:readSize)!
//处理文件
虽然是真的{
做{
//如果已读取所有帧,则处理完成
如果file.framePosition==file.length{
打破
}
尝试file.read(进入:readBuffer)
scheduleBuffer(readBuffer,completionHandler:nil)
让结果=尝试engine.renderofline(renderBuffer.frameCapacity,to:renderBuffer)
//尽量避免增加沉默
让expectedFrames=AVAudioFrameCount(Float(readBuffer.frameLength)/timePitch.rate)
如果预期帧