SByte GetSample(SoundGenType type, int hz, int i, int sample) { SByte ret = (SByte)0; float t = i / (float)sample; float freq = t * hz * Mathf.PI * 2.0f; float sl = (float)sample / hz; switch (type) { case SoundGenType.PULSE: ret = (SByte)(Mathf.Sin(freq) * Mathf.Max(1.0f - t, 0.0f) * 127); break; case SoundGenType.TRIANGLE: ret = (SByte)((Math.Abs(((i + sl / 2) % (float)(sl)) / (sl)) * 2.0f) * 127); break; case SoundGenType.SINE: ret = (SByte)(Mathf.Sin(freq) * 127); break; case SoundGenType.SQUARE: ret = (SByte)(((Mathf.Round(((i % (float)(sl)) / (sl))))) * 127); break; case SoundGenType.SAWTOOTH: ret = (SByte)((((i % (float)(sl)) / (sl))) * 127); break; case SoundGenType.NOISE: ret = (SByte)(((Mathf.Round(((i % (float)(sl)) / (sl))))) * randomNoise[i % randomNoise.Length]); break; } return(ret); }
// Start is called before the first frame update void Start() { AudioSource source = gameObject.AddComponent <AudioSource>(); freqCtrl.minValue = 10; freqCtrl.maxValue = 600; freqCtrl.onValueChanged.AddListener((v) => { lastHz = hz; hz = (int)v; }); List <string> ops = new List <string>(); for (int i = 0; i < (int)SoundGenType.MAX_COUNT; i++) { ops.Add(((SoundGenType)i).ToString()); } timbreCtrl.ClearOptions(); timbreCtrl.AddOptions(ops); timbreCtrl.onValueChanged.AddListener((v) => { type = (SoundGenType)v; }); autoCtrl.isOn = true; autoCtrl.onValueChanged.AddListener((v) => { auto = v; }); }
void PartInfoLine() { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add Part")) { currentPart = music.NewPart(); } if (music.GetPartsNum() > 1) { if (GUILayout.Button("Remove Part")) { music.RemovePart(currentPart); currentPart = music.GetPart(0); } } string[] Timbres = new string[] { "PULSE", "TRIANGLE", "SINE", "SQUARE", "SAWTOOTH", "NOISE" }; int newtype = EditorGUILayout.Popup((int)type, Timbres); if (newtype != (int)type) { type = (SoundGenType)newtype; currentPart.Timbre = type; } int n = 0; string[] names = new string[music.GetPartsNum()]; for (int i = 0; i < music.GetPartsNum(); i++) { names[i] = "part " + (i + 1); if (music.GetPart(i) == currentPart) { n = i; } } int sel = GUILayout.Toolbar(n, names); currentPart = music.GetPart(sel); type = currentPart.Timbre; EditorGUILayout.EndHorizontal(); }
public SoundClip(SoundGenType type, int sample, int hz, int hz2, int harmonicNum, float time, bool loopType = false) { int count = (int)((float)sample * time); if (hz == hz2) { count = Mathf.RoundToInt((float)sample * time / hz) * hz; } data = new float[count]; for (int i = 0; i < count; i++) { int chz = (int)Mathf.Lerp(hz, hz2, (float)i / count); data[i] += GetSample(type, chz, i, sample) / 128.0f; for (int j = 1; j <= harmonicNum; j++) { data[i] += GetSample(type, chz, i * j, sample) * ((float)(harmonicNum - j) / (float)(harmonicNum)) / 128.0f; } if (harmonicNum > 1) { data[i] /= 2.7f; } else if (harmonicNum > 0) { data[i] /= 1.5f; } } if (!loopType && data.Length > 20) { for (int i = 0; i < 10; i++) { data[i] *= i / 10.0f; data[count - 1 - i] *= i / 10.0f; } } }
public DATA(byte[] rawData) : base(rawData) { var reader = new ByteReader(); SoundType = reader.ReadBytes <SoundGenType>(base.Data); }