Iphone 如何反转音频文件?
我想在iOS上反转现有的音频文件(例如WAV、CAF等)。对如何实现这一目标有何建议?开源库?WAV文件只包含原始PCM样本。如果你按相反的顺序播放,你会得到相反的声音。(还有)WAV文件只包含原始PCM样本。如果你按相反的顺序播放,你会得到相反的声音。(还有)使用,我能够读取录制的音频样本。下面的代码片段展示了如何打开输入文件,并以相反的顺序写入输出。不过,您应该注意,所有数据都是在内存中读取的Iphone 如何反转音频文件?,iphone,ios,audio,Iphone,Ios,Audio,我想在iOS上反转现有的音频文件(例如WAV、CAF等)。对如何实现这一目标有何建议?开源库?WAV文件只包含原始PCM样本。如果你按相反的顺序播放,你会得到相反的声音。(还有)WAV文件只包含原始PCM样本。如果你按相反的顺序播放,你会得到相反的声音。(还有)使用,我能够读取录制的音频样本。下面的代码片段展示了如何打开输入文件,并以相反的顺序写入输出。不过,您应该注意,所有数据都是在内存中读取的 SF_INFO info_read; info_read.format = 0; SNDFILE*
SF_INFO info_read;
info_read.format = 0;
SNDFILE* sndfilein = sf_open("my_input_file.caf", SFM_READ, &info_read);
SF_INFO info_write;
info_write.format = info.format;
info_write.channels = info.channels;
info_write.frames = info.frames;
info_write.samplerate = info.samplerate;
info_write.sections = info.sections;
info_write.seekable = info.seekable;
SNDFILE* sndfileout = sf_open("my_output_file.caf", SFM_RDWR, &info_write);
int* buf = new int[8000 * 30];
int framesRead = sf_readf_int(sndfilein, buf, info.frames);
for (int i = 0; i < info.frames; i++)
sf_writef_int(sndfileout, &buf[info.frames - i], 1);
delete[] buf;
sf_close(sndfilein);
sf_close(sndfileout);
SF\u信息读取;
info_read.format=0;
SNDFILE*sndfilein=sf_open(“my_input_file.caf”、SFM_READ和info_READ”);
SF_信息写入;
info_write.format=info.format;
info_write.channels=info.channels;
info_write.frames=info.frames;
info_write.samplerate=info.samplerate;
info_write.sections=info.sections;
info_write.seekable=info.seekable;
SNDFILE*sndfileout=sf_open(“my_output_file.caf”、SFM_RDWR和info_write”);
int*buf=新int[8000*30];
int framesRead=sf_readf_int(sndfilein,buf,info.frames);
对于(int i=0;i
使用,我能够读取录制的音频样本。下面的代码片段展示了如何打开输入文件,并以相反的顺序写入输出。不过,您应该注意,所有数据都是在内存中读取的
SF_INFO info_read;
info_read.format = 0;
SNDFILE* sndfilein = sf_open("my_input_file.caf", SFM_READ, &info_read);
SF_INFO info_write;
info_write.format = info.format;
info_write.channels = info.channels;
info_write.frames = info.frames;
info_write.samplerate = info.samplerate;
info_write.sections = info.sections;
info_write.seekable = info.seekable;
SNDFILE* sndfileout = sf_open("my_output_file.caf", SFM_RDWR, &info_write);
int* buf = new int[8000 * 30];
int framesRead = sf_readf_int(sndfilein, buf, info.frames);
for (int i = 0; i < info.frames; i++)
sf_writef_int(sndfileout, &buf[info.frames - i], 1);
delete[] buf;
sf_close(sndfilein);
sf_close(sndfileout);
SF\u信息读取;
info_read.format=0;
SNDFILE*sndfilein=sf_open(“my_input_file.caf”、SFM_READ和info_READ”);
SF_信息写入;
info_write.format=info.format;
info_write.channels=info.channels;
info_write.frames=info.frames;
info_write.samplerate=info.samplerate;
info_write.sections=info.sections;
info_write.seekable=info.seekable;
SNDFILE*sndfileout=sf_open(“my_output_file.caf”、SFM_RDWR和info_write”);
int*buf=新int[8000*30];
int framesRead=sf_readf_int(sndfilein,buf,info.frames);
对于(int i=0;i
我开发了一个示例应用程序,它记录用户所说的内容并反向播放。我使用CoreAudio实现了这一点。链接到应用程序代码
因为每个样本的大小为16位(2字节)(单通道)。您可以通过从录制结束时开始并向后读取,将每个样本复制到不同的缓冲区,一次加载一个样本。当您到达数据的开头时,您已反转数据,播放将被反转
// set up output file
AudioFileID outputAudioFile;
AudioStreamBasicDescription myPCMFormat;
myPCMFormat.mSampleRate = 16000.00;
myPCMFormat.mFormatID = kAudioFormatLinearPCM ;
myPCMFormat.mFormatFlags = kAudioFormatFlagsCanonical;
myPCMFormat.mChannelsPerFrame = 1;
myPCMFormat.mFramesPerPacket = 1;
myPCMFormat.mBitsPerChannel = 16;
myPCMFormat.mBytesPerPacket = 2;
myPCMFormat.mBytesPerFrame = 2;
AudioFileCreateWithURL((__bridge CFURLRef)self.flippedAudioUrl,
kAudioFileCAFType,
&myPCMFormat,
kAudioFileFlags_EraseFile,
&outputAudioFile);
// set up input file
AudioFileID inputAudioFile;
OSStatus theErr = noErr;
UInt64 fileDataSize = 0;
AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);
theErr = AudioFileOpenURL((__bridge CFURLRef)self.recordedAudioUrl, kAudioFileReadPermission, 0, &inputAudioFile);
thePropertySize = sizeof(fileDataSize);
theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);
UInt32 dataSize = fileDataSize;
void* theData = malloc(dataSize);
//Read data into buffer
UInt32 readPoint = dataSize;
UInt32 writePoint = 0;
while( readPoint > 0 )
{
UInt32 bytesToRead = 2;
AudioFileReadBytes( inputAudioFile, false, readPoint, &bytesToRead, theData );
AudioFileWriteBytes( outputAudioFile, false, writePoint, &bytesToRead, theData );
writePoint += 2;
readPoint -= 2;
}
free(theData);
AudioFileClose(inputAudioFile);
AudioFileClose(outputAudioFile);
我已经开发了一个示例应用程序,它记录用户所说的内容并反向播放。我使用CoreAudio实现了这一点。链接到应用程序代码 因为每个样本的大小为16位(2字节)(单通道)。您可以通过从录制结束时开始并向后读取,将每个样本复制到不同的缓冲区,一次加载一个样本。当您到达数据的开头时,您已反转数据,播放将被反转
// set up output file
AudioFileID outputAudioFile;
AudioStreamBasicDescription myPCMFormat;
myPCMFormat.mSampleRate = 16000.00;
myPCMFormat.mFormatID = kAudioFormatLinearPCM ;
myPCMFormat.mFormatFlags = kAudioFormatFlagsCanonical;
myPCMFormat.mChannelsPerFrame = 1;
myPCMFormat.mFramesPerPacket = 1;
myPCMFormat.mBitsPerChannel = 16;
myPCMFormat.mBytesPerPacket = 2;
myPCMFormat.mBytesPerFrame = 2;
AudioFileCreateWithURL((__bridge CFURLRef)self.flippedAudioUrl,
kAudioFileCAFType,
&myPCMFormat,
kAudioFileFlags_EraseFile,
&outputAudioFile);
// set up input file
AudioFileID inputAudioFile;
OSStatus theErr = noErr;
UInt64 fileDataSize = 0;
AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);
theErr = AudioFileOpenURL((__bridge CFURLRef)self.recordedAudioUrl, kAudioFileReadPermission, 0, &inputAudioFile);
thePropertySize = sizeof(fileDataSize);
theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);
UInt32 dataSize = fileDataSize;
void* theData = malloc(dataSize);
//Read data into buffer
UInt32 readPoint = dataSize;
UInt32 writePoint = 0;
while( readPoint > 0 )
{
UInt32 bytesToRead = 2;
AudioFileReadBytes( inputAudioFile, false, readPoint, &bytesToRead, theData );
AudioFileWriteBytes( outputAudioFile, false, writePoint, &bytesToRead, theData );
writePoint += 2;
readPoint -= 2;
}
free(theData);
AudioFileClose(inputAudioFile);
AudioFileClose(outputAudioFile);
Kiran在Swift 2.2中的回答:
let forwardAudioURL: NSURL = ... // wherever your original audio is
let reversedAudioURL: NSURL = ... // wherever you want the reversed file to go
// Load forward audio into originalAudioFile
var originalAudioFile: AudioFileID = nil
let possibleError1 = AudioFileOpenURL(forwardAudioURL,
AudioFilePermissions.ReadPermission,
0,
&originalAudioFile)
// Load the size in bytes of the original audio into originalAudioSize variable
var originalAudioSize: Int64 = 0
var propertySize: UInt32 = 8
let possibleError2 = AudioFileGetProperty(originalAudioFile,
kAudioFilePropertyAudioDataByteCount,
&propertySize,
&originalAudioSize)
if possibleError1 != 0 || possibleError2 != 0 {
// Handle errors if you want
}
// Set up file that the reversed audio will be loaded into
var reversedAudioFile: AudioFileID = nil
var format = AudioStreamBasicDescription()
format.mSampleRate = 16000
format.mFormatID = kAudioFormatLinearPCM
format.mChannelsPerFrame = 1
format.mFramesPerPacket = 1
format.mBitsPerChannel = 16
format.mBytesPerPacket = 2
format.mBytesPerFrame = 2
AudioFileCreateWithURL(reversedAudioURL,
kAudioFileCAFType,
&format,
AudioFileFlags.EraseFile,
&reversedAudioFile)
// Read data into the reversedAudioFile
var readPoint: Int64 = originalAudioSize
var writePoint: Int64 = 0
var buffer: Int16 = 0
while readPoint > 0 {
var bytesToRead: UInt32 = 2;
AudioFileReadBytes(originalAudioFile,
false,
readPoint,
&bytesToRead,
&buffer)
AudioFileWriteBytes(reversedAudioFile,
false,
writePoint,
&bytesToRead,
&buffer)
writePoint += 2
readPoint -= 2
}
AudioFileClose(originalAudioFile)
AudioFileClose(reversedAudioFile)
Kiran在Swift 2.2中的回答:
let forwardAudioURL: NSURL = ... // wherever your original audio is
let reversedAudioURL: NSURL = ... // wherever you want the reversed file to go
// Load forward audio into originalAudioFile
var originalAudioFile: AudioFileID = nil
let possibleError1 = AudioFileOpenURL(forwardAudioURL,
AudioFilePermissions.ReadPermission,
0,
&originalAudioFile)
// Load the size in bytes of the original audio into originalAudioSize variable
var originalAudioSize: Int64 = 0
var propertySize: UInt32 = 8
let possibleError2 = AudioFileGetProperty(originalAudioFile,
kAudioFilePropertyAudioDataByteCount,
&propertySize,
&originalAudioSize)
if possibleError1 != 0 || possibleError2 != 0 {
// Handle errors if you want
}
// Set up file that the reversed audio will be loaded into
var reversedAudioFile: AudioFileID = nil
var format = AudioStreamBasicDescription()
format.mSampleRate = 16000
format.mFormatID = kAudioFormatLinearPCM
format.mChannelsPerFrame = 1
format.mFramesPerPacket = 1
format.mBitsPerChannel = 16
format.mBytesPerPacket = 2
format.mBytesPerFrame = 2
AudioFileCreateWithURL(reversedAudioURL,
kAudioFileCAFType,
&format,
AudioFileFlags.EraseFile,
&reversedAudioFile)
// Read data into the reversedAudioFile
var readPoint: Int64 = originalAudioSize
var writePoint: Int64 = 0
var buffer: Int16 = 0
while readPoint > 0 {
var bytesToRead: UInt32 = 2;
AudioFileReadBytes(originalAudioFile,
false,
readPoint,
&bytesToRead,
&buffer)
AudioFileWriteBytes(reversedAudioFile,
false,
writePoint,
&bytesToRead,
&buffer)
writePoint += 2
readPoint -= 2
}
AudioFileClose(originalAudioFile)
AudioFileClose(reversedAudioFile)
我认为PCM样本只是一个不同的声音?PCM样本是模拟波形的一个数字样本(声音最终只是一个波形…如果你向后播放波形,你会得到相反的声音),请参见中的图形。每个离散水平是一个样本。WAV文件只是这些示例的一个数组(加上一点元数据)。反转阵列,将其馈送到WAV播放机,就完成了。我认为PCM样本只是一个不同的声音?PCM样本是模拟波形的一个数字样本(声音最终只是一个波形…如果你向后播放波形,你会得到反转的声音)。请参见中的图。每个离散水平是一个样本。WAV文件只是这些示例的一个数组(加上一点元数据)。反转阵列,将其馈送到WAV播放机,就完成了。有没有办法用.m4a文件而不是.caf文件来执行此操作?有没有办法用.m4a文件而不是.caf文件来执行此操作?我已经尝试了此代码,但播放速度变慢了,例如,1:45分钟的音频文件变为4:15分钟。有解决方案吗@mckennac4**注意:此代码不适用于任何其他格式。它必须是.wav音频文件!我的案例的速度比原始文件快。什么值会改变速度@mckennac4I尝试过这个代码,但是播放速度变慢了,比如,1:45分钟的音频文件变成了4:15分钟。有解决方案吗@mckennac4**注意:此代码不适用于任何其他格式。它必须是.wav音频文件!我的案例的速度比原始文件快。什么值会改变速度@McKennac4如果有帮助,这里有一个C#中关于如何反转.wav文件的教程:如果有帮助,这里有一个C#中关于如何反转.wav文件的教程: