我有点失落,希望有人可以对此有所了解。 出于好奇,我正在研究一个简单的softsynth /音序器。一些想法 取自家用电脑黄金时代流行的.mod格式。 目前它只是一个模拟。从数组中读出注释 最多64个值,其中数组中的每个位置对应于十六分之一 注意。到目前为止一切都那么好,一切正常,旋律起作用 正好。如果从一个音符转换到另一个音符,则会出现问题。 例如f4 - >克#4。由于这是一个突然的变化,有一个明显的弹出/点击 声音。为了补偿我试图在不同频率之间进行插值 并开始编写一个简单的例子来说明我的想法并验证它 工作
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
public class Main extends Sprite
{
private var sampleRate:int = 44100;
private var oldFreq:Number = 349.1941058508811;
private var newFreq:Number = 349.1941058508811;
private var volume:Number = 15;
private var position:int = 0;
private var bmp:Bitmap = new Bitmap();
private var bmpData:BitmapData = new BitmapData(400, 100, false, 0x000000);
private var col:uint = 0xff0000;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
bmp.bitmapData = bmpData;
addChild(bmp);
for (var a:int = 0; a < 280; a++)
{
if (a == 140)
{
col = 0x00ff00;
newFreq = 415.26411519488113;
}
if (a == 180)
{
col = 0x0000ff;
}
oldFreq = oldFreq * 0.9 + newFreq * 0.1;
bmpData.setPixel(position, Math.sin((position) * Math.PI * 2 / sampleRate * oldFreq * 2) * volume + bmpData.height/2, col);
position++;
}
}
}
这将生成以下输出:
蓝点表示正弦波为349.1941058508811 hz,红色为415.26411519488113 hz,绿点表示插值。 对于我的眼睛,看起来这应该工作! 但是,如果我将此技术应用于我的项目,结果就不一样了! 实际上,如果我将输出渲染为波形文件,那么它们之间的过渡 两个频率看起来像这样:
显然它会使弹出更糟糕。什么可能是错的? 这是我的(缩短的)代码:
import flash.display.*;
import flash.events.Event;
import flash.events.*;
import flash.utils.ByteArray;
import flash.media.*;
import flash.utils.getTimer;
public class Main extends Sprite
{
private var sampleRate:int = 44100;
private var bufferSize:int = 8192;
private var bpm:int = 125;
private var numberOfRows:int = 64;
private var currentRow:int = 0;
private var quarterNoteLength:Number;
private var sixteenthNoteLength:Number;
private var numOctaves:int = 8;
private var patterns:Array = new Array();
private var currentPattern:int;
private var songOrder:Array = new Array();
private var notes:Array = new Array("c-", "c#", "d-", "d#", "e-", "f-", "f#", "g-", "g#", "a-", "a#", "b-");
private var frequencies:Array = new Array();
private var samplePosition:Number = 0;
private var position:int = 0;
private var channel1:Object = new Object();
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
quarterNoteLength = sampleRate * 60 / bpm;
sixteenthNoteLength = quarterNoteLength / 2 / 2;
for (var a:int = 0; a < numOctaves; a++)
{
for (var b:int = 0; b < notes.length; b++)
{
frequencies.push(new Array(notes[b % notes.length] + a, 16.35 * Math.pow(2, frequencies.length / 12)));
}
}
patterns.push(new Array("f-4", "", "", "", "g#4", "", "", "f-4", "", "f-4", "a#4", "", "f-4", "", "d#4", "", "f-4", "", "", "", "c-5", "", "", "f-4", "", "f-4", "c#5", "", "c-5", "", "g#4", "", "f-4", "", "c-5", "", "f-5", "", "f-4", "d#4", "", "d#4", "c-4", "", "g-4", "", "f-4", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""));
songOrder = new Array(0, 0);
currentRow = 0;
currentPattern = 0;
channel1.volume = .05;
channel1.waveform = "sine";
channel1.frequency = [0];
channel1.oldFrequency = [0,0,0,0];
channel1.noteTriggered = false;
updateRow();
var sound:Sound = new Sound();
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
sound.play();
}
private function updateRow():void
{
var tempNote:String = patterns[songOrder[currentPattern]][currentRow];
if (tempNote != "")
{
channel1.frequency = new Array();
if (tempNote.indexOf("|") == -1)
{
channel1.frequency.push(findFrequency(tempNote));
}
channel1.noteTriggered = true;
}
}
private function onSampleData(event:SampleDataEvent):void
{
var sampleData:Number;
for (var i:int = 0; i < bufferSize; i++)
{
if (++samplePosition == sixteenthNoteLength)
{
if (++currentRow == numberOfRows)
{
currentRow = 0;
if (++currentPattern == songOrder.length)
{
currentPattern = 0;
}
}
updateRow();
samplePosition = 0;
}
for (var a:int = 0; a < (channel1.frequency).length; a++ )
{
channel1.oldFrequency[a] = channel1.oldFrequency[a]*0.9+channel1.frequency[a]*0.1;
}
if ((channel1.frequency).length == 1)
{
sampleData = generate(channel1.waveform, position, channel1.oldFrequency[0], channel1.volume);
}
else
{
sampleData = generate(channel1.waveform, position, channel1.oldFrequency[0], channel1.volume);
sampleData += generate(channel1.waveform, position, channel1.oldFrequency[1], channel1.volume);
}
event.data.writeFloat(sampleData);
event.data.writeFloat(sampleData);
position++;
}
}
private function generate(waveForm:String, pos:Number, frequency:Number, volume:Number):Number
{
var retVal:Number
switch (waveForm)
{
case "square":
retVal = Math.sin((pos) * 2 * Math.PI / sampleRate * frequency) > 0 ? volume : -volume;
break;
case "sine":
retVal = Math.sin((pos) * Math.PI * 2 / sampleRate * frequency * 2) * volume;
break;
case "sawtooth":
retVal = (2 * (pos % (sampleRate / frequency)) / (sampleRate / frequency) - 1) * volume;
break;
}
return retVal;
}
private function findFrequency(inpNote:String):Number
{
var retVal:Number;
for (var a:int = 0; a < frequencies.length; a++)
{
if (frequencies[a][0] == inpNote)
{
retVal = frequencies[a][1];
break;
}
}
return retVal;
}
}
谢谢! =)
答案 0 :(得分:1)
您错过了切换频率时,pos
中的generate
值会失去不变性,也就是说,Math.sin((pos) * Math.PI * 2 / sampleRate * frequency * 2)
在使用不同频率运行时会给出非常不同的值。相反,你应该使用&#34;阶段&#34;变量将从0到1然后返回到0并再次像锯齿图那样转发,并将通过(当前频率)*(1 /采样率)的值转发。因此,错误是您将两个generate()
结果添加到一个sampleData
(您通常不能这样做,因为干扰)以及您使用一个position
作为时间值来计算相位而不是累积阶段。检查这种方法,它应该更好一点:
private function generate(waveForm:String, var phase:Number, frequency:Number, volume:Number):Number {
// "pos" changed to "phase". This also means that "generate" should be called once per sample
var retVal:Number;
switch (waveForm)
{
case "square":
retVal = Math.sin(phase * 2 * Math.PI) > 0 ? volume : -volume;
break;
case "sine":
retVal = Math.sin(phase * 2 * Math.PI ) * volume;
break;
case "sawtooth":
retVal = (2*Math.abs(2*phase-1)-1)* volume;
break;
}
phase+=frequency/sampleRate;// calculate new phase
if (phase>1.0) { phase-=1.0; } // normalize phase to 0..1
return retVal;
}
private function onSampleData(event:SampleDataEvent):void {
var sampleData:Number;
for(var i:int=0;i<bufferSize;i++) {
if (++samplePosition == sixteenthNoteLength)
{ // leaving this part as is, seems working
if (++currentRow == numberOfRows)
{
currentRow = 0;
if (++currentPattern == songOrder.length)
{
currentPattern = 0;
}
}
updateRow();
samplePosition = 0;
}
sampleData=0;
for (i=0;i</*channels.length*/1;i++) {
// TODO convert "channel1" to an array
// sampleData+=generate(channels[i].waveform, channels[i].phase, channels[i].frequency, channels[i].volume);
sampleData+=generate(channel1.waveform, channel1.phase, channel1.frequency[0], channel1.volume);
}
event.data.writeFloat(sampleData);
event.data.writeFloat(sampleData);
}
}
事实上,你的频道应该转到一个单独的类,它将所有参数保存在一起(相位,频率,波形,音量),然后,只要你需要它们进行采样,你就可以调用{{ 1}}并获得一个没有参数麻烦的浮动。另外,一个频道,一个频率,所以跳过那些&#34; oldFrequency&#34;东西。
作为后续行动,channels[i].generateNextSample()
班的草图:
Channel
使用示例:
public class Channel {
public const WAVE_SINE:int=0;
public const WAVE_SQUARE:int=1;
public const WAVE_SAWTOOTH:int=2;
private var phase:Number=0;
private var currentVolume:Number=0;
public var volume:Number; // 0 to 1, should build a setter to normalize
public var frequency:Number=0;
public var waveform:int; // should also not allow changing this mid-play probably
public function Channel(v:Number=0,wf:int=WAVE_SINE,f:Number=0) {
this.volume=v;
this.frequency=f;
this.waveform=wf;
phase=0;
currentVolume=0;
}
public function generateNextSample():Number {...} // use the generate() code above to fill
public function reset():void { currentVolume=0; phase=0; } // POW
// rest to taste, enabled, active, whatever
}