reSound question
David Betz
Posts: 14,519
in Propeller 2
I'm converting one of the reSound examples to C and I'm wondering why this code does coginit:
PUB start coginit(cogid, reSoundSampleDemo, @stack) PUB reSoundSampleDemo | input1, input2, input3Why not just this:
PUB start | input1, input2, input3

Comments
Here is my translated code and below that are the errors I got. Some are due to my attempt to replace "sound#SIGNED16" with "sound.SIGNED16". I'm not sure what caused the others.
#include <stdint.h> // config PLL, 20MHz/2*25*1 = 250MHz #define my_clkmode 0b1_000001_0000011000_1111_10_00 #define my_clkfreq 250_000_000 #define OUTPUT_PIN_LEFT 14 #define OUTPUT_PIN_RIGHT 15 #define MIXING_FREQ 88200 #define NR_INPUT_CHANNELS 3 #define HZ 0x8000_0000 struct __using("reSound.spin2") sound; int32_t resoundHubFootprint[64]; #include "kick.c" #include "hihat.c" #include "snare.c" void main() { int32_t input1, input2, input3; clkset(my_clkmode, my_clkfreq); // Initialize and start reSound using a mixing rate of 88200 Hz, with 3 input channels and stereo output sound.init(clkfreq, MIXING_FREQ, true, NR_INPUT_CHANNELS, OUTPUT_PIN_LEFT, OUTPUT_PIN_RIGHT, -1, true, resoundHubFootprint); // Configure 3 input channels using 16 bit signed format input1 = sound.configureInput(0, 0, 65536, 0, -1& sound.SIGNED16, 0); input2 = sound.configureInput(0, 0, 65536, 0, -1& sound.SIGNED16, 0); input3 = sound.configureInput(0, 0, 65536, 0, -1& sound.SIGNED16, 0); sound.start(); for (;;) { // Play a little beat // 1 / 4 sound.playSample(input1, kick_array, kick_size, 1, 44100|Hz, 12000, 32768); sound.playSample(input2, hihat_array, hihat_size, 1, 44100|Hz, 2000, 65535); waitcnt(CNT + 70_000_000); // 2 / 4 sound.playSample(input2, hihat_array, hihat_size, 1, 44100|Hz, 4000, 32768); waitcnt(CNT + 70_000_000); // 3 / 4 sound.playSample(input1, kick_array, kick_size, 1, 44100|Hz, 12000, 32768); sound.playSample(input3, snare_array, snare_size, 1, 44100|Hz, 9000, 10000); sound.playSample(input2, hihat_array, hihat_size, 1, 44100|Hz, 2000, 0); waitcnt(CNT + 70_000_000); // 4 / 4 sound.playSample(input1, kick_array, kick_size, 1, 44100|Hz, 12000, 32768); sound.playSample(input2, hihat_array, hihat_size, 1, 44100|Hz, 2000, 32768); waitcnt(CNT + 70_000_000); } }And here are the errors I got:
@ersmith Do you know why this takes a long time to compiler with fastspin?
#include <stdint.h> // config PLL, 20MHz/2*25*1 = 250MHz #define my_clkmode 0b1_000001_0000011000_1111_10_00 #define my_clkfreq 250_000_000 #define OUTPUT_PIN_LEFT 14 #define OUTPUT_PIN_RIGHT 15 #define MIXING_FREQ 88200 #define NR_INPUT_CHANNELS 3 #define Hz 0x8000_0000 struct __using("reSound.spin2") sound; int32_t resoundHubFootprint[64]; #include "kick.c" #include "hihat.c" #include "snare.c" void main() { int32_t input1, input2, input3; clkset(my_clkmode, my_clkfreq); // Initialize and start reSound using a mixing rate of 88200 Hz, with 3 input channels and stereo output sound.init(clkfreq, MIXING_FREQ, true, NR_INPUT_CHANNELS, OUTPUT_PIN_LEFT, OUTPUT_PIN_RIGHT, -1, true, resoundHubFootprint); // Configure 3 input channels using 16 bit signed format input1 = sound.configureinput(0, 0, 65536, 0, -1& sound.signed16, 0); input2 = sound.configureinput(0, 0, 65536, 0, -1& sound.signed16, 0); input3 = sound.configureinput(0, 0, 65536, 0, -1& sound.signed16, 0); sound.start(); for (;;) { // Play a little beat // 1 / 4 sound.playsample(input1, kick_array, kick_size, 1, 44100|Hz, 12000, 32768); sound.playsample(input2, hihat_array, hihat_size, 1, 44100|Hz, 2000, 65535); waitcnt(cnt + 70_000_000); // 2 / 4 sound.playsample(input2, hihat_array, hihat_size, 1, 44100|Hz, 4000, 32768); waitcnt(cnt + 70_000_000); // 3 / 4 sound.playsample(input1, kick_array, kick_size, 1, 44100|Hz, 12000, 32768); sound.playsample(input3, snare_array, snare_size, 1, 44100|Hz, 9000, 10000); sound.playsample(input2, hihat_array, hihat_size, 1, 44100|Hz, 2000, 0); waitcnt(cnt + 70_000_000); // 4 / 4 sound.playsample(input1, kick_array, kick_size, 1, 44100|Hz, 12000, 32768); sound.playsample(input2, hihat_array, hihat_size, 1, 44100|Hz, 2000, 32768); waitcnt(cnt + 70_000_000); } }Starting like this gives you control of where you want your stack and its size.
Parsing of large initializers was using an O(n^2) algorithm. I've improved that, the current github is still a bit slower than I'd like but much much better than before.
Is it working now, or is something funky with the system clock or mixing rate?
#include <stdint.h> #include <propeller.h> // config PLL, 20MHz/2*25*1 = 250MHz #define my_clkmode 0b1_000001_0000011000_1111_10_00 #define my_clkfreq 250_000_000 #define OUTPUT_PIN_LEFT 14 #define OUTPUT_PIN_RIGHT 15 #define MIXING_FREQ 88200 #define NR_INPUT_CHANNELS 3 #define Hz 0x8000_0000 struct __using("reSound.spin2") sound; int32_t resoundHubFootprint[64]; #include "kick.c" #include "hihat.c" #include "snare.c" void main() { int32_t input1, input2, input3; _clkset(my_clkmode, my_clkfreq); // Initialize and start reSound using a mixing rate of 88200 Hz, with 3 input channels and stereo output sound.init(clkfreq, MIXING_FREQ, true, NR_INPUT_CHANNELS, OUTPUT_PIN_LEFT, OUTPUT_PIN_RIGHT, -1, true, resoundHubFootprint); // Configure 3 input channels using 16 bit signed format input1 = sound.configureinput(0, 0, 65536, 0, -1& sound.signed16, 0); input2 = sound.configureinput(0, 0, 65536, 0, -1& sound.signed16, 0); input3 = sound.configureinput(0, 0, 65536, 0, -1& sound.signed16, 0); sound.start(); for (;;) { // Play a little beat // 1 / 4 sound.playsample(input1, kick_array, kick_size/2, 1, 44100|Hz, 12000, 32768); sound.playsample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 2000, 65535); waitcnt(cnt + 70_000_000); // 2 / 4 sound.playsample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 4000, 32768); waitcnt(cnt + 70_000_000); // 3 / 4 sound.playsample(input1, kick_array, kick_size/2, 1, 44100|Hz, 12000, 32768); sound.playsample(input3, snare_array, snare_size/2, 1, 44100|Hz, 9000, 10000); sound.playsample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 2000, 0); waitcnt(cnt + 70_000_000); // 4 / 4 sound.playsample(input1, kick_array, kick_size/2, 1, 44100|Hz, 12000, 32768); sound.playsample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 2000, 32768); waitcnt(cnt + 70_000_000); } }Changing all "waitcnt(cnt + 70_000_000);" to "waitx(70_000_000);" fixed the problem.
The question is why the waitcnt() function behaves differently on Spin2 and C... Eric?!
To answer your question regarding the pitch/frequency; Just change the "44100|Hz" to any sample rate you would like to replay at. (The original samples are sampled at 44100 Hz) This can be independantly set for all inputs.
#include <stdint.h> #include <propeller.h> // config PLL, 20MHz/2*25*1 = 250MHz #define my_clkmode 0b1_000001_0000011000_1111_10_00 #define my_clkfreq 250_000_000 #define OUTPUT_PIN_LEFT 14 #define OUTPUT_PIN_RIGHT 15 #define MIXING_FREQ 88200 #define NR_INPUT_CHANNELS 3 #define Hz 0x8000_0000 struct __using("reSound.spin2") sound; int32_t resoundHubFootprint[64]; #include "kick.c" #include "hihat.c" #include "snare.c" void main() { int32_t input1, input2, input3; _clkset(my_clkmode, my_clkfreq); // Initialize and start reSound using a mixing rate of 88200 Hz, with 3 input channels and stereo output sound.init(clkfreq, MIXING_FREQ, true, NR_INPUT_CHANNELS, OUTPUT_PIN_LEFT, OUTPUT_PIN_RIGHT, -1, true, resoundHubFootprint); // Configure 3 input channels using 16 bit signed format input1 = sound.configureInput(0, 0, 65536, 0, -1& sound.SIGNED16, 0); input2 = sound.configureInput(0, 0, 65536, 0, -1& sound.SIGNED16, 0); input3 = sound.configureInput(0, 0, 65536, 0, -1& sound.SIGNED16, 0); sound.start(); for (;;) { // Play a little beat // 1 / 4 sound.playSample(input1, kick_array, kick_size/2, 1, 44100|Hz, 12000, 32768); sound.playSample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 2000, 65535); waitx(70_000_000); // 2 / 4 sound.playSample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 4000, 32768); waitx(70_000_000); // 3 / 4 sound.playSample(input1, kick_array, kick_size/2, 1, 44100|Hz, 12000, 32768); sound.playSample(input3, snare_array, snare_size/2, 1, 44100|Hz, 9000, 10000); sound.playSample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 2000, 0); waitx(70_000_000); // 4 / 4 sound.playSample(input1, kick_array, kick_size/2, 1, 44100|Hz, 12000, 32768); sound.playSample(input2, hihat_array, hihat_size/2, 1, 44100|Hz, 2000, 32768); waitx(70_000_000); } }