@@ -19,8 +19,8 @@ void PWMTimerOutput::start(uint32_t sample_rate)
1919{
2020 mSampleRate = sample_rate;
2121
22- ledcSetup (0 , 50000 , 8 );
23- ledcAttachPin (mPDMPin , 0 );
22+ ledcSetup (2 , 32000 , 11 );
23+ ledcAttachPin (mPDMPin , 2 );
2424
2525 // create a timer that will fire at the sample rate
2626 timer_config_t timer_config = {
@@ -67,7 +67,7 @@ void PWMTimerOutput::start(uint32_t sample_rate)
6767 Serial.println (" PDM Started" );
6868}
6969
70- void PWMTimerOutput::write (int8_t *samples, int count)
70+ void PWMTimerOutput::write (uint8_t *samples, int count)
7171{
7272 // Serial.printf("Count %d\n", mCount);
7373 while (true )
@@ -78,10 +78,10 @@ void PWMTimerOutput::write(int8_t *samples, int count)
7878 {
7979 // Serial.println("Filling second buffer");
8080 // make sure there's enough room for the samples
81- mSecondBuffer = (int8_t *)realloc (mSecondBuffer , count);
81+ mSecondBuffer = (uint8_t *)realloc (mSecondBuffer , count);
8282 // copy them into the second buffer
8383 for (int i = 0 ; i < count; i++) {
84- mSecondBuffer [i] = samples[i] * mVolume / 10 ;
84+ mSecondBuffer [i] = samples[i];
8585 }
8686 // second buffer is now full of samples
8787 mSecondBufferLength = count;
@@ -103,9 +103,9 @@ void PWMTimerOutput::onTimer()
103103 {
104104 mCount ++;
105105 // get the first sample from the buffer
106- int16_t sample = mBuffer [mCurrentIndex ];
106+ uint32_t sample = ( uint32_t ( mBuffer [mCurrentIndex ]) << 3 ) ;
107107 mCurrentIndex ++;
108- ledcWrite (0 , sample+ 128 );
108+ ledcWrite (2 , sample * mVolume / 10 );
109109 }
110110 if (mCurrentIndex >= mBufferLength )
111111 {
@@ -115,7 +115,7 @@ void PWMTimerOutput::onTimer()
115115 {
116116 if (mSecondBufferLength > 0 ) {
117117 // swap the buffers
118- int8_t *tmp = mBuffer ;
118+ uint8_t *tmp = mBuffer ;
119119 mBuffer = mSecondBuffer ;
120120 mBufferLength = mSecondBufferLength ;
121121 mSecondBuffer = tmp;
0 commit comments