Example #1
0
	void DrumKitTest::RunTest()
	{


		std::string workingDirectory("/home/jeremy/Desktop/Prog/RaspiDrums/Data/");
		std::string kitLocation(workingDirectory + "Kits/default.xml");


		DrumKit::Kit kit;

		DrumKit::Module module(workingDirectory);
		module.LoadKit(kitLocation, kit);


		std::string xmlFileLoc(workingDirectory + "alsaConfig.xml");

		Sound::AlsaParams aParams;
		Sound::Alsa::ReadXmlConfig(aParams, xmlFileLoc);


		Sound::Mixer mixer(module.soundParameters, aParams);
		Sound::Alsa  alsa(aParams, mixer);

		std::string sensorFile(workingDirectory + "../out.raw");

		char channel = 0;
		IO::Sensor sensor(channel);

		DrumKit::Trigger snareTrigger(kit.drum[0], mixer);

		alsa.Start();

		high_resolution_clock::time_point tStart = high_resolution_clock::now();
		unsigned long long dt = 0;


		while(dt < 10*1000)
		{

			high_resolution_clock::time_point t = high_resolution_clock::now();
			dt = (unsigned long long) duration<double, std::milli>(t - tStart).count();


			short value = sensor.GetOutput();
			snareTrigger.Trig(value);

		}


		alsa.Stop();


		return;
	}
Example #2
0
	void DrumKit_HddTest::RunTest()
	{


		std::string workingDirectory("/home/jeremy/Desktop/Prog/RaspiDrums/Data/");
		std::string kitLocation(workingDirectory + "Kits/default.xml");


		DrumKit::Kit kit;

		DrumKit::Module module(workingDirectory);
		module.LoadKit(kitLocation, kit);


		std::string xmlFileLoc(workingDirectory + "alsaConfig.xml");

		Sound::AlsaParams aParams;
		Sound::Alsa::ReadXmlConfig(aParams, xmlFileLoc);


		Sound::Mixer mixer(module.soundParameters, aParams);
		Sound::Alsa  alsa(aParams, mixer);

		std::string sensorFile(workingDirectory + "../out.raw");

		IO::HddSensor hddSensor(sensorFile);
		DrumKit::Trigger snareTrigger(kit.drum[0], mixer);

		alsa.Start();

		int N = hddSensor.GetDataLength();

		for(int i = 0; i < N; i++)
		{

			short value = hddSensor.GetOutput();
			snareTrigger.Trig(value);

		}

		alsa.Stop();


		return;
	}
Example #3
0
int main(int argc, char* argv[])
{
  (void)argc;
  (void)argv;

  unsigned int bpm = 120;

  std::array<int, 10> notes = { { 40, 41, 28, 28, 28, 28, 27, 25, 35, 78 } };
  std::array<int, 10> sweepRate = { { 1, 6, 6, 2, 1, 2, 4, 8, 3, 3 } };
  std::array<int, 16> kickSequence = { { 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0 } };
  unsigned int sweepPos = 0;
  unsigned int kickPos = 0;

  Oscillator<WaveformType::Saw<float>> sawA(0.0);
  Oscillator<WaveformType::Saw<float>> sawB(0.0);
  Oscillator<WaveformType::Triangle<float>> sweep(0.0);
  Oscillator<WaveformType::Sine<float>> kick(0.0);
  WhiteNoise<> noise;

  Decay snareDecayA(0.4);
  Decay snareDecayB(0.97);
  Decay kickDecay(0.85);

  Impulse trigger(bpm / 120.0f);
  Impulse snareTrigger(bpm / 50.0f, 44100.0f, 0.5);
  Impulse kickTrigger(bpm / 480.0f);

  random_selector<> randomSelect;
  float targetFrequency = 0.0f;
  float bassFrequency = 0.0f;

  Audio audio([&](Audio::sample_iterator aInBegin,
                  Audio::sample_iterator aInEnd,
                  Audio::sample_iterator aOutBegin,
                  Audio::sample_iterator aOutEnd)
                {
                  for (Audio::sample_iterator it = aOutBegin; it != aOutEnd; it += 2)
                  {
                    if (trigger())
                    {
                      targetFrequency = mtof(randomSelect(notes));
                      sweep.setFrequency(sweepRate[sweepPos++]);
                      if (sweepPos >= sweepRate.size()) sweepPos = 0;
                    }

                    bassFrequency = targetFrequency * 0.0008 + bassFrequency * 0.9992;
                    sawA.setFrequency(bassFrequency * 1.01);
                    sawB.setFrequency(bassFrequency * 0.99);

                    int kickTrig = kickTrigger();
                    if (kickTrig)
                    {
                      kickTrig &= kickSequence[kickPos++];
                      if (kickPos >= kickSequence.size()) kickPos = 0;
                    }

                    float kickDec = kickDecay(kickTrig);
                    kick.setFrequency(40.0 + (kickDec * kickDec * kickDec * 200));

                    int snareTrig = snareTrigger();
                    float snareEnvelope = snareDecayA(snareTrig) * 2 + snareDecayB(snareTrig) * 0.05;

                    float snareLeft = noise() * snareEnvelope;
                    float snareRight = noise() * snareEnvelope;
                    float kickSample = kick() * kickDec * 7;
                    float sample = sawA() + sawB() + kickSample;

                    *it = sample + snareLeft;
                    *(it + 1) = sample + snareRight;
                  }
                });

  audio.play();

  std::cout << "Stream opened with a buffer size of " << audio.bufferSize()
            << " samples, and a stream latency of " << audio.streamLatency()
            << " samples (" << (audio.streamLatency() / audio.samplerateControl().getSamplerate()) * 1000. << " ms.)" << std::endl;

  std::cin.get();
}