gpt4 book ai didi

iphone - 在音频应用程序中使用诺卡因

转载 作者:可可西里 更新时间:2023-11-01 04:22:42 29 4
gpt4 key购买 nike

我正在构建一个 iPhone 应用程序,它通过以“caf”格式播放单独录制的吉他音符来生成随机吉他音乐。这些音符的持续时间从 3 秒到 11 秒不等,具体取决于延音量。

我最初使用 AVAudioPlayer 进行播放,在模拟器中以 120 bpm 播放 16 分音符,它唱得很漂亮,但在我的手机上,只要我
将速度提高到 60 bpm 以上,只演奏 1/4 音符,它跑得像条狗,跟不上时间。我的兴高采烈是很短暂的。

为了减少延迟,我尝试使用 Apple MixerHost 项目作为音频引擎的模板,通过 Audio Units 实现播放,但是在我用 bolt 固定并连接所有东西后,仍然出现错误的访问错误。

经过几个小时的努力,我放弃了这种想法,转而使用 Novocaine 音频引擎。

我现在遇到了一堵砖墙,试图将它连接到我的模型。

在最基本的层面上,我的模型是一个 Neck 对象,其中包含一个 NSDictionary 的 Note 对象。

每个 Note 对象都知道它在吉他琴颈上的弦和品格,并包含自己的 AVAudioPlayer。

根据用户偏好中选择的琴颈尺寸,我构建了一个包含 122 个音符(6 弦 x 22 品)或 144 个音符(6 弦 x 24 品)的半音吉他琴颈。

我使用这些音符作为我的单一事实点,因此音乐引擎生成的所有标量音符都是指向这个半音音符桶的指针。

@interface Note : NSObject <NSCopying>
{
NSString *name;
AVAudioPlayer *soundFilePlayer;
int stringNumber;
int fretNumber;
}

我总是从所选音阶的根音符或和弦开始播放,然后生成要播放的音符,因此我总是在生成的音符后面播放一个音符。这样,下一个要播放的音符总是排队准备就绪。

这些音符的播放控制是通过以下代码实现的:
- (void)runMusicGenerator:(NSNumber *)counter
{
if (self.isRunning) {
Note *NoteToPlay;
// pulseRate is the time interval between beats
// staticNoteLength = 1/4 notes, 1/8th notes, 16th notes, etc.

float delay = self.pulseRate / [self grabStaticNoteLength];

// user setting to play single, double or triplet notes.
if (self.beatCounter == CONST_BEAT_COUNTER_INIT_VAL) {
NoteToPlay = [self.GuitarNeck generateNoteToPlayNext];
} else {
NoteToPlay = [self.GuitarNeck cloneNote:self.GuitarNeck.NoteToPlayNow];
}

self.GuitarNeck.NoteToPlayNow = NoteToPlay;

[self callOutNoteToPlay];

[self performSelector:@selector(runDrill:) withObject:NoteToPlay afterDelay:delay];
}

- (Note *)generateNoteToPlayNext
{
if ((self.musicPaused) || (self.musicStopped)) {
// grab the root note on the string to resume
self.NoteToPlayNow = [self grabRootNoteForString];

//reset the flags
self.musicPaused = NO;
self.musicStopped = NO;
} else {
// Set NoteRingingOut to NoteToPlayNow
self.NoteRingingOut = self.NoteToPlayNow;

// Set NoteToPlaNowy to NoteToPlayNext
self.NoteToPlayNow = self.NoteToPlayNext;
if (!self.NoteToPlayNow) {
self.NoteToPlayNow = [self grabRootNoteForString];

// now prep the note's audio player for playback
[self.NoteToPlayNow.soundFilePlayer prepareToPlay];
}
}

// Load NoteToPlayNext
self.NoteToPlayNext = [self generateRandomNote];
}

- (void)callOutNoteToPlay
{
self.GuitarNeck.NoteToPlayNow.soundFilePlayer.delegate = (id)self;
[self.GuitarNeck.NoteToPlayNow.soundFilePlayer setVolume:1.0];
[self.GuitarNeck.NoteToPlayNow.soundFilePlayer setCurrentTime:0];
[self.GuitarNeck.NoteToPlayNow.soundFilePlayer play];
}

每个 Note 的 AVAudioPlayer 加载如下:
- (AVAudioPlayer *)buildStringNotePlayer:(NSString *)nameOfNote
{
NSString *soundFileName = @"S";
soundFileName = [soundFileName stringByAppendingString:[NSString stringWithFormat:@"%d", stringNumber]];
soundFileName = [soundFileName stringByAppendingString:@"F"];

if (fretNumber < 10) {
soundFileName = [soundFileName stringByAppendingString:@"0"];
}
soundFileName = [soundFileName stringByAppendingString:[NSString stringWithFormat:@"%d", fretNumber]];

NSString *soundPath = [[NSBundle mainBundle] pathForResource:soundFileName ofType:@"caf"];
NSURL *fileURL = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

return notePlayer;
}

这就是我来的地方。

根据 Novocaine Github 页面...

播放音频
Novocaine *audioManager = [Novocaine audioManager];
[audioManager setOutputBlock:^(float *audioToPlay, UInt32 numSamples, UInt32 numChannels) {
// All you have to do is put your audio into "audioToPlay".
}];

但是在下载的项目中,你使用下面的代码加载音频...
// AUDIO FILE READING OHHH YEAHHHH
// ========================================
NSURL *inputFileURL = [[NSBundle mainBundle] URLForResource:@"TLC" withExtension:@"mp3"];

fileReader = [[AudioFileReader alloc]
initWithAudioFileURL:inputFileURL
samplingRate:audioManager.samplingRate
numChannels:audioManager.numOutputChannels];

[fileReader play];
fileReader.currentTime = 30.0;

[audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
{
[fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];
NSLog(@"Time: %f", fileReader.currentTime);
}];

这是我真正开始感到困惑的地方,因为第一种方法使用浮点数,而第二种方法使用 URL。

如何将“caf”文件传递给浮点数?我不确定如何实现诺沃卡因 - 我的脑子里仍然很模糊。

我希望有人可以帮助我的问题如下......
  • Novocaine 对象是否类似于 AVAudioPlayer 对象,只是更通用并调整到最大以减少延迟?即独立的音频播放(/录音/生成)单元?
  • 我可以在我的模型中按原样使用诺卡因吗?即每个半音音符有 1 个诺卡因对象,还是我应该有 1 个包含所有半音音符的诺卡因对象?或者我只是将 URL 存储在笔记中并将其传递给 Novocaine 播放器?
  • 当我的音频是“caf”文件并且“audioToPlay”采用浮点数时,如何将我的音频放入“audioToPlay”?
  • 如果我在 Note.m 中包含并声明了一个 Novocaine 属性,那么我是否必须将该类重命名为 Note.mm 才能使用 Novocaine 对象?
  • 我如何同时演奏多个 Novocaine 对象以重现和弦和音程?
  • 我可以循环播放 Novocaine 对象吗?
  • 我可以设置音符的播放长度吗?即只播放 10 秒的音符 1 秒?
  • 我可以修改上面的代码以使用诺沃卡因吗?
  • 我为 runMusicGenerator 使用的方法是否正确用于保持符合专业标准的节奏?
  • 最佳答案

    Novocaine 无需手动设置 RemoteIO AudioUnit,让您的生活更轻松。这包括不得不痛苦地填充一堆 CoreAudio 结构并提供一堆回调,例如这个音频处理回调。
    static OSStatus PerformThru(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData);
    相反,Novocaine 在其实现中处理它,然后调用您通过执行此操作设置的 block 。
    [audioManager setOutputBlock: ^(float *audioToPlay, UInt32 numSamples, UInt32 numChannels){} ];
    无论你写什么audioToPlay被玩。

  • Novocaine 为您设置了 RemoteIO AudioUnit。这是一个低级 CoreAudio API,与高级 AVFoundation 不同,并且正如预期的那样具有非常低的延迟。您是对的,因为 Novocaine 是独立的。您可以实时录制、生成和处理音频。
  • Novocaine 是一个单例,您不能有多个 Novocaine 实例。一种方法是将您的吉他声音/声音存储在单独的类或数组中,然后编写一堆方法,使用 Novocaine 播放它们。
  • 你有很多选择。您可以使用 Novocaine 的 AudioFileReader 为您播放 .caf 文件。根据示例代码,您可以通过分配一个 AudioFileReader 然后传递您想要播放的 .caf 文件的 URL 来完成此操作。然后你贴[fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels]在您的 block 中,根据示例代码。每次调用 block 时,AudioFileReader 都会从磁盘中抓取并缓冲一段音频并将其放入 audioToPlay随后被播放。这有一些缺点。对于短的声音(例如我假设的吉他声音),请重复调用 retrieveFreshAudio是一个性能打击。将整个文件同步顺序读取到内存中通常是一个更好的主意(对于短声音)。 Novocaine 没有提供一种方法来做到这一点(还)。您必须使用 ExtAudioFileServices 来执行此操作。 Apple 示例项目 MixerHost 详细说明了如何执行此操作。
  • 如果您使用 AudioFileReader 是。您只有在 #import 时才重命名为 .mm来自 Obj-C++ 头文件或 #include ing C++ 头文件。
  • 如前所述,只允许 1 个 Novocaine 实例。您可以通过混合多个音频源来实现复音。这只是将缓冲区添加在一起。如果您以不同的音高制作了相同吉他声音的多个版本,只需将它们全部读入内存,然后混音即可。如果您只想拥有一种吉他声音,那么您必须实时更改正在弹奏的多个音符的播放速率,然后进行混音。
  • Novocaine 与您实际演奏的内容无关,也不关心您演奏样本的时间。为了循环一个声音,你必须保持一个已经过去了多少样本的计数,检查你是否在你的声音的末尾,然后将该计数设置回 0。
  • 是的。假设 44.1k 采样率,1 秒音频 = 44100 个样本。当它达到 44100 时,您将重置您的计数。
  • 是的。它看起来像这样。假设您有 4 个单声道且长度超过 1 秒的吉他声音,并且您已将它们读入内存 float *guitarC , *guitarE , *guitarG , *guitarB; (爵士 CMaj7 和弦 w00t),并且想要将它们混合 1 秒钟并以单声道循环:

  • [audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels){
    static int count = 0;
    for(int i=0; i<numFrames; ++i){
    //Mono mix each sample of each sound together. Since result can be 4x louder, divide the total amp by 4.
    //You should be using `vDSP_vadd` from the accelerate framework for added performance.
    data[count] = (guitarC[count] + guitarE[count] + guitarG[count] + guitarB[count]) * 0.25;
    if(++count >= 44100) count = 0; //Plays the mix for 1 sec
    }
    }];

  • 不完全是。使用 performSelector或者任何调度在 runloop 或线程上的机制都不能保证是准确的。例如,当 CPU 负载波动时,您可能会遇到时序不规则。如果您想要采样准确的时间,请使用音频 block 。
  • 关于iphone - 在音频应用程序中使用诺卡因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291119/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com