我正在制作一个 iPhone 应用程序,它允许用户设计一个音频过滤器并在一些录制的声音上对其进行测试。我尝试执行以下操作:
问题如下:只要我将数据从一个缓冲区复制到另一个缓冲区,然后将其写入第二个音频文件,一切正常。但是一旦我尝试对样本执行任何类型的操作(比如将它们除以 2),结果只是随机噪声。这让我怀疑我没有正确解释音频数据的值,但我已经尝试了五天,但我就是不明白。如果您知道如何访问和操作单个音频样本,请帮助我,我将不胜感激! 谢谢!
这是稍后将执行过滤的代码(现在它应该只是将所有音频样本除以 2);
OSStatus status = noErr;
UInt32 propertySizeDataPacketCount;
UInt32 writabilityDataPacketCount;
UInt32 numberOfPackets;
UInt32 propertySizeMaxPacketSize;
UInt32 writabilityMaxPacketSize;
UInt32 maxPacketSize;
UInt32 numberOfBytesRead;
UInt32 numberOfBytesToWrite;
UInt32 propertySizeDataByteCount;
SInt64 currentPacket;
double x0;
double x1;
status = AudioFileOpenURL(audioFiles->recordedFile,
kAudioFileReadPermission,
kAudioFileAIFFType,
&audioFiles->inputFile);
status = AudioFileOpenURL(audioFiles->filteredFile,
kAudioFileReadWritePermission,
kAudioFileAIFFType,
&audioFiles->outputFile);
status = AudioFileGetPropertyInfo(audioFiles->inputFile,
kAudioFilePropertyAudioDataPacketCount,
&propertySizeDataPacketCount,
&writabilityDataPacketCount);
status = AudioFileGetProperty(audioFiles->inputFile,
kAudioFilePropertyAudioDataPacketCount,
&propertySizeDataPacketCount,
&numberOfPackets);
status = AudioFileGetPropertyInfo (audioFiles->inputFile,
kAudioFilePropertyMaximumPacketSize,
&propertySizeMaxPacketSize,
&writabilityMaxPacketSize);
status = AudioFileGetProperty(audioFiles->inputFile,
kAudioFilePropertyMaximumPacketSize,
&propertySizeMaxPacketSize,
&maxPacketSize);
SInt16 *inputBuffer = (SInt16 *)malloc(numberOfPackets * maxPacketSize);
SInt16 *outputBuffer = (SInt16 *)malloc(numberOfPackets * maxPacketSize);
currentPacket = 0;
status = AudioFileReadPackets(audioFiles->inputFile,
false, &numberOfBytesRead,
NULL,
currentPacket,
&numberOfPackets,
inputBuffer);
for (int i = 0; i < numberOfPackets; i++) {
x0 = (double)inputBuffer[i];
x1 = 0.5 * x0; //This is supposed to reduce the value of the sample by half
//x1 = x0; //This just copies the value of the sample and works fine
outputBuffer[i] = (SInt16)x1;
}
numberOfBytesToWrite = numberOfBytesRead;
currentPacket = 0;
status = AudioFileWritePackets(audioFiles->outputFile,
false,
numberOfBytesToWrite,
NULL,
currentPacket,
&numberOfPackets,
outputBuffer);
status = AudioFileClose(audioFiles->inputFile);
status = AudioFileClose(audioFiles->outputFile);
我使用以下代码创建音频文件:
#import "AudioFiles.h"
#define SAMPLE_RATE 44100
#define FRAMES_PER_PACKET 1
#define CHANNELS_PER_FRAME 1
#define BYTES_PER_FRAME 2
#define BYTES_PER_PACKET 2
#define BITS_PER_CHANNEL 16
@implementation AudioFiles
-(void)setupAudioFormat:(AudioStreamBasicDescription *)format {
format->mSampleRate = SAMPLE_RATE;
format->mFormatID = kAudioFormatLinearPCM;
format->mFramesPerPacket = FRAMES_PER_PACKET;
format->mChannelsPerFrame = CHANNELS_PER_FRAME;
format->mBytesPerFrame = BYTES_PER_FRAME;
format->mBytesPerPacket = BYTES_PER_PACKET;
format->mBitsPerChannel = BITS_PER_CHANNEL;
format->mReserved = 0;
format->mFormatFlags = kLinearPCMFormatFlagIsBigEndian |
kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
}
- (id)init
{
self = [super init];
if (self) {
char path[256];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *recordedFilePath = [docsDir stringByAppendingPathComponent:@"/recordedAudio.aiff"];
[recordedFilePath getCString:path maxLength:sizeof(path) encoding:NSUTF8StringEncoding];
recordedFile = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8 *)path, strlen(path), false);
recordedFileURL = [NSURL fileURLWithPath:recordedFilePath];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *filteredFilePath = [docsDir stringByAppendingPathComponent:@"/filteredAudio.aiff"];
[filteredFilePath getCString:path maxLength:sizeof(path) encoding:NSUTF8StringEncoding];
filteredFile = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8 *)path, strlen(path), false);
filteredFileURL = [NSURL fileURLWithPath:filteredFilePath];
AudioStreamBasicDescription audioFileFormat;
[self setupAudioFormat:&audioFileFormat];
OSStatus status = noErr;
status = AudioFileCreateWithURL(recordedFile,
kAudioFileAIFFType,
&audioFileFormat,
kAudioFileFlags_EraseFile,
&inputFile);
status = AudioFileCreateWithURL(filteredFile,
kAudioFileAIFFType,
&audioFileFormat,
kAudioFileFlags_EraseFile,
&outputFile);
}
return self;
}
@end
对于录音,我使用具有以下设置的 AVAudioRecorder:
NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 8000.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithBool:YES],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:YES], AVLinearPCMIsNonInterleaved,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFiles->recordedFileURL settings:recordSettings error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
最佳答案
您的输入数据是 BigEndian,但您假设它是 LittleEndian。
处理此问题的一种方法是:
SInt16 inVal = OSSwapBigToHostInt16(inputBuffer[i]);
SInt16 outVal = inVal / 2;
outputBuffer[i] = OSSwapHostToBigInt16(outVal);
关于ios - 如何访问音频文件中的样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8400716/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru