我使用this library中的MIKMIDISynthesizer
在iPhone上播放MIDI文件。不幸的是,即使将iPhone的系统音量一直提升,音量也非常低。为了进一步增加它,我尝试了这些:
AUGraph
。这足以提高音量,但会立即扭曲信号,使质量太差。现在我的选项已经用完了。是否有任何额外的参数或级别(如AVAudioSession
或CoreMIDI
,我可能会错过)并提供调整音量/增益的可能性?还是做同样的MIDI命令?
1。使用混音器节点
这是我修改的MIKMIDISynthesizer
部分,用于通过混音器节点控制音量。
- (BOOL)setupAUGraph
{
AUGraph graph;
OSStatus err = 0;
// 1. Create new AUGraph
if ((err = NewAUGraph(&graph))) {
NSLog(@"Unable to create AU graph: %@", @(err));
return NO;
}
// 2. Create output node
AudioComponentDescription outputcd = {0};
outputcd.componentType = kAudioUnitType_Output;
outputcd.componentSubType = kAudioUnitSubType_RemoteIO;
outputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
AUNode outputNode;
if ((err = AUGraphAddNode(graph, &outputcd, &outputNode))) {
NSLog(@"Unable to add ouptput node to graph: %@", @(err));
return NO;
}
// 3. Create the instrument node
AudioComponentDescription instrumentcd = self.componentDescription;
AUNode instrumentNode;
if ((err = AUGraphAddNode(graph, &instrumentcd, &instrumentNode))) {
NSLog(@"Unable to add instrument node to AU graph: %@", @(err));
return NO;
}
if ((err = AUGraphOpen(graph))) {
NSLog(@"Unable to open AU graph: %@", @(err));
return NO;
}
AudioUnit instrumentUnit;
if ((err = AUGraphNodeInfo(graph, instrumentNode, NULL, &instrumentUnit))) {
NSLog(@"Unable to get instrument AU unit: %@", @(err));
return NO;
}
// 4. Create the mixer node
AUNode mixerNode;
AudioComponentDescription cd = {};
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
cd.componentType = kAudioUnitType_Mixer;
cd.componentSubType = kAudioUnitSubType_MultiChannelMixer;
if ((err = AUGraphAddNode(graph, &cd, &mixerNode))) {
NSLog(@"Unable to add mixer node to AU graph: %@", @(err));
return NO;
}
if ((err = AUGraphNodeInfo(graph, mixerNode, 0, &_mixerUnit))) {
NSLog(@"Unable to fetch mixer node reference from AU graph: %@", @(err));
return NO;
}
UInt32 busCount = 2;
err = AudioUnitSetProperty(_mixerUnit,
kAudioUnitProperty_ElementCount,
kAudioUnitScope_Input,
0,
&busCount,
sizeof (busCount)
);
if (err) {
NSLog(@"Unable to set mixer unit properties");
return NO;
}
// 5.1 Connect the instrument node as the mixer node's input
if ((err = AUGraphConnectNodeInput(graph, instrumentNode, 0, mixerNode, 0))) {
NSLog(@"Unable to connect input node and mixer node: %@", @(err));
return NO;
}
// 5.2 Connect the mixer node as the output node's input
if ((err = AUGraphConnectNodeInput(graph, mixerNode, 0, outputNode, 0))) {
NSLog(@"Unable to connect input node and mixer node: %@", @(err));
return NO;
}
if ((err = AUGraphInitialize(graph))) {
NSLog(@"Unable to initialize AU graph: %@", @(err));
return NO;
}
// 6. Finally, start the graph
if ((err = AUGraphStart(graph))) {
NSLog(@"Unable to start AU graph: %@", @(err));
return NO;
}
self.graph = graph;
self.instrumentUnit = instrumentUnit;
return YES;
}
设置混音器节点后,我可以像这样更改音量(引入剪辑!):
-(void) setVolume: (float) volume {
AudioUnitSetParameter(_mixerUnit, kMultiChannelMixerParam_Volume, kAudioUnitScope_Output, 0, volume, 0);
}
2。更改MIDI事件
第二种方法将每个Node On事件的velocity
属性设置为最大值127.它会增加音量而不会产生任何噪音,但只会增加到某个级别。代码位于MIKMIDINoteEvent
:
+ (MIKMIDINoteOnCommand *)noteOnCommandFromNoteEvent:(MIKMIDINoteEvent *)noteEvent clock:(MIKMIDIClock *)clock
{
MIKMutableMIDINoteOnCommand *noteOn = [MIKMutableMIDINoteOnCommand commandForCommandType:MIKMIDICommandTypeNoteOn];
noteOn.midiTimestamp = [clock midiTimeStampForMusicTimeStamp:noteEvent.timeStamp];
noteOn.channel = noteEvent.channel;
noteOn.note = noteEvent.note;
noteOn.velocity = 127; // <------- Here's the magic
return [noteOn copy];
}
答案 0 :(得分:5)
对于MIDI合成器使用的某些类型的采样波形,可能会发生这种情况。采样波形的峰值体积可能已包含格式附近或最大可能的样本(例如,对于16位样本为+ -32767,对于浮点样本为+ -1.0),即使平均体积(能量总和)为太低。因此,音量的任何简单线性增加都会产生剪切噪声,这听起来非常糟糕。
各种压缩器技术或滤波器可能能够在不削波的情况下增加平均音量,但这可以增加其自身的质量降低(但不如削波那么烦人)。
补充:使用另一个midi采样器,声音字体或具有接近恒定音量的声音(较低的PAPR,峰值平均功率比)是另一种可能性。
答案 1 :(得分:2)
我终于明白了。我的AVSession
类别设置为AVAudioSessionCategoryPlayAndRecord
,因为我还使用了麦克风,它可以通过iPhone顶部的小型耳机自动输出所有音频。这就是它看起来如此安静的原因。要将输出路由到主扬声器,您必须调用
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&err];
尽管如此,感谢大家的排除故障,我很感激。
答案 2 :(得分:0)
您是否尝试使用SampleRate设置AVAudioSession,其等于44100.0 Hertz?