Hi,
I’ve made a super simple 16 step sequencer (for no real reason at all), it’s not quite perfect, I’ve used an array of bools that gets iterated over using a counter, I suspect I’m one digit off somewhere, or I need to shift the array by a digit. If anyone could take a look and see if they can spot the problem that would be amazing and I am welcome to any suggestions or feedback for general improvement.
using UnityEngine;
using System.Collections;
using System;
public class Sampler16 : MonoBehaviour {
public float bpm = 120;
public float beatSegment = 16;
private double nextEventTime;
private int flip = 0;
private bool running = false;
//public AudioClip[] clips = new AudioClip[2];
public AudioClip[] kick;
public AudioClip[] snare;
public AudioClip[] hiHat;
public AudioClip[] bassOneShot;
AudioClip[] openHat;
AudioClip[] chordOneShot;
private AudioSource[] audioSources = new AudioSource[4];
public int counter = 0;
public bool[] myHiBools = new bool[16];
public bool[] myKikBools = new bool[16];
public bool[] mySnrBools = new bool[16];
public bool[] myBassBools = new bool[16];
// Use this for initialization
void Start () {
GameObject child = new GameObject ("Player");
child.transform.parent = gameObject.transform;
audioSources[0] = child.AddComponent<AudioSource> ();
audioSources[1] = child.AddComponent<AudioSource> ();
audioSources[2] = child.AddComponent<AudioSource> ();
audioSources[3] = child.AddComponent<AudioSource> ();
nextEventTime = AudioSettings.dspTime + 2.0f;
running = true;
}
// Update is called once per frame
void Update () {
if (!running)
return;
double time = AudioSettings.dspTime;
if (time + 1.0f > nextEventTime)
{
// audioSources.clip = kick[0];
// audioSources.PlayScheduled (nextEventTime);
// Debug.Log ("Scheduled source to start at time " + nextEventTime);
Counter ();
PlayStuff();
nextEventTime += (60.0f / bpm) * beatSegment;
}
}
void Counter() {
counter = counter + 1;
if (counter > 15) {
counter = 0;
}
Debug.Log (counter);
}
void PlayStuff(){
for (int i = 0; i < counter; i++)
{
if (myHiBools[counter] == true) {
PlayHiHat (0);
}
if (mySnrBools[counter] == true) {
PlaySnare(0);
}
if (myKikBools[counter] == true) {
PlayKick(0);
}
if (myBassBools[counter] == true) {
PlayBass(0);
}
}
}
void PlayKick (int clip)
{
AudioSource audio1 = audioSources[0];
audio1.clip = kick[clip];
audio1.Play ();
}
void PlaySnare (int clip)
{
AudioSource audio2 = audioSources[1];
audio2.clip = snare[clip];
audio2.Play ();
}
void PlayHiHat (int clip)
{
AudioSource audio3 = audioSources[2];
audio3.clip = hiHat[clip];
audio3.Play ();
}
void PlayBass (int clip)
{
AudioSource audio4 = audioSources[3];
audio4.clip = bassOneShot[clip];
audio4.Play ();
}
}
Ideally I’d like the boolean arrays to appear Horizontal instead of vertical, but that’s only a superficial improvement so non important.
Also I’ve created four different audio sources for the elements, would it be simple enough to expose the volume control from each of the sources in the public variables bit?