I2S.setLSBJFormat(); was what I added to the original sketch, it was needed for my PT8211 DAC. Your board must not need it due to probably having a different format I2S.
I have modified the sketch to play 2 arrays, one to Left and other to Right.
question... Is speed of playing the arrays controlled just by speed of Loop or is the I2S in control...??
one of the arrays is 100 point and other is 400 point of 1 cycle sawtooth in each so the pitch of one is 4 times the other and mixes together via L&R speakers so it sounds alright.
I can alter the pitch (to any pitch) by changing the sample rate but both change together.
I was wondering if its possible to have 2 instances of I2S to control each sound...???
also at the minute if I stop one sound the other sound plays an octave higher because only 1 write....... but I can make it write Zero.
Would welcome your thoughts, sketch copy below...
/*
This example generates a square wave based tone at a specified frequency
and sample rate. Then outputs the data using the I2S interface to a
MAX08357 I2S Amp Breakout board.
created 17 November 2016
by Sandeep Mistry
modified for RP2040 by Earle F. Philhower, III <earlephilhower@yahoo.com>
bool setBCLK(pin_size_t pin);
- This assigns two adjacent pins - the pin after this one (one greater)
is the WS (word select) signal, which toggles before the sample for
each channel is sent
bool setDATA(pin_size_t pin);
- Sets the DOUT pin, can be any valid GPIO pin
xxxx ... Now User-Changed to generate any arbitrary wave from an array L & R ... Mar 2023 xxxx
*/
#include <I2S.h>
// Create the I2S port using a PIO state machine
I2S i2s(OUTPUT);
// GPIO pin numbers
#define pBCLK 20
#define pWS (pBCLK+1)
#define pDOUT 22
long saw400array[] = {
0,
152,
303,
455,
606,
758,
910,
1062,
1213,
1365,
1517,
1668,
1820,
1971,
2123,
2275,
2426,
2578,
2730,
2882,
3033,
3185,
3336,
3488,
3640,
3791,
3943,
4095,
4247,
4398,
4550,
4701,
4853,
5005,
5156,
5308,
5459,
5611,
5763,
5915,
6066,
6218,
6370,
6521,
6673,
6824,
6976,
7128,
7279,
7431,
7583,
7735,
7886,
8038,
8189,
8341,
8493,
8644,
8796,
8947,
9099,
9251,
9403,
9554,
9706,
9858,
10009,
10161,
10312,
10464,
10616,
10768,
10919,
11071,
11223,
11374,
11526,
11677,
11829,
11981,
12132,
12284,
12436,
12588,
12739,
12891,
13042,
13194,
13346,
13497,
13649,
13800,
13952,
14104,
14256,
14407,
14559,
14711,
14862,
15014,
15165,
15317,
15469,
15621,
15772,
15924,
16076,
16227,
16379,
16530,
16682,
16834,
16985,
17137,
17289,
17440,
17592,
17744,
17895,
18047,
18199,
18350,
18502,
18653,
18805,
18957,
19109,
19260,
19412,
19564,
19715,
19867,
20018,
20170,
20322,
20474,
20625,
20777,
20929,
21080,
21232,
21383,
21535,
21687,
21838,
21990,
22142,
22293,
22445,
22597,
22748,
22900,
23052,
23203,
23355,
23506,
23658,
23810,
23962,
24113,
24265,
24417,
24568,
24720,
24871,
25023,
25175,
25327,
25478,
25630,
25781,
25933,
26085,
26236,
26388,
26540,
26691,
26843,
26995,
27146,
27298,
27450,
27601,
27753,
27905,
28056,
28208,
28359,
28511,
28663,
28815,
28966,
29118,
29270,
29421,
29573,
29724,
29876,
30028,
30179,
30331,
-30317,
-30166,
-30014,
-29862,
-29711,
-29559,
-29407,
-29256,
-29104,
-28952,
-28801,
-28649,
-28497,
-28346,
-28194,
-28042,
-27891,
-27739,
-27588,
-27436,
-27284,
-27132,
-26981,
-26829,
-26678,
-26526,
-26374,
-26223,
-26071,
-25919,
-25768,
-25616,
-25464,
-25313,
-25161,
-25009,
-24858,
-24706,
-24554,
-24403,
-24251,
-24100,
-23948,
-23796,
-23644,
-23493,
-23341,
-23190,
-23038,
-22886,
-22735,
-22583,
-22431,
-22279,
-22128,
-21976,
-21825,
-21673,
-21521,
-21370,
-21218,
-21066,
-20915,
-20763,
-20611,
-20460,
-20308,
-20156,
-20005,
-19853,
-19701,
-19550,
-19398,
-19247,
-19095,
-18943,
-18791,
-18640,
-18488,
-18337,
-18185,
-18033,
-17882,
-17730,
-17578,
-17426,
-17275,
-17123,
-16972,
-16820,
-16668,
-16517,
-16365,
-16213,
-16062,
-15910,
-15758,
-15607,
-15455,
-15303,
-15152,
-15000,
-14849,
-14697,
-14545,
-14394,
-14242,
-14090,
-13938,
-13787,
-13635,
-13484,
-13332,
-13180,
-13029,
-12877,
-12725,
-12573,
-12422,
-12270,
-12119,
-11967,
-11815,
-11664,
-11512,
-11360,
-11209,
-11057,
-10905,
-10754,
-10602,
-10450,
-10299,
-10147,
-9996,
-9844,
-9692,
-9541,
-9389,
-9237,
-9085,
-8934,
-8782,
-8631,
-8479,
-8327,
-8176,
-8024,
-7872,
-7720,
-7569,
-7417,
-7266,
-7114,
-6962,
-6811,
-6659,
-6508,
-6356,
-6204,
-6052,
-5901,
-5749,
-5597,
-5446,
-5294,
-5143,
-4991,
-4839,
-4688,
-4536,
-4384,
-4232,
-4081,
-3929,
-3778,
-3626,
-3474,
-3323,
-3171,
-3019,
-2867,
-2716,
-2564,
-2413,
-2261,
-2109,
-1958,
-1806,
-1655,
-1503,
-1351,
-1199,
-1048,
-896,
-744,
-593,
-441,
-290
};
long saw100array[] = {
331,
937,
1544,
2151,
2757,
3364,
3971,
4577,
5184,
5790,
6397,
7004,
7610,
8217,
8824,
9430,
10037,
10643,
11250,
11857,
12463,
13070,
13677,
14283,
14890,
15496,
16103,
16710,
17316,
17923,
18530,
19136,
19743,
20349,
20956,
21563,
22169,
22776,
23383,
23989,
24596,
25202,
25809,
26416,
27022,
27629,
28236,
28842,
29449,
30055,
-30138,
-29531,
-28925,
-28318,
-27712,
-27105,
-26498,
-25892,
-25285,
-24678,
-24072,
-23465,
-22859,
-22252,
-21645,
-21039,
-20432,
-19825,
-19219,
-18612,
-18006,
-17399,
-16792,
-16186,
-15579,
-14972,
-14366,
-13759,
-13153,
-12546,
-11940,
-11333,
-10726,
-10119,
-9513,
-8906,
-8300,
-7693,
-7087,
-6480,
-5873,
-5266,
-4660,
-4053,
-3447,
-2840,
-2234,
-1627,
-1020,
-413
};
const int amplitude = 95; // amplitude of output wave percent
const int sampleRate = 44100; // to change wave frequency
int16_t sample = amplitude; // current sample value
int16_t samp = amplitude;
int count = 0;
int count2 = 0;
void setup() {
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, 1);
// Serial.begin(115200);
//Serial.println("I2S simple tone");
i2s.setBCLK(pBCLK);
i2s.setDATA(pDOUT);
i2s.setBitsPerSample(16);
i2s.setLSBJFormat(); // added this line for Left justified PT8211 DAC
// start I2S at the sample rate with 16-bits per sample
if (!i2s.begin(sampleRate)) {
// Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
}
void loop() {
if(count >= 100) count = 0;
sample = saw100array[count]*amplitude/100; // High Pitch Tone
i2s.write(sample);
if(count2 >= 400) count2 = 0;
samp = saw400array[count2]*amplitude/400; // Low Pitch Tone
i2s.write(samp);
count++;
count2++;
}