Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little endian... #2

Closed
dagnall53 opened this issue Dec 14, 2017 · 6 comments
Closed

Little endian... #2

dagnall53 opened this issue Dec 14, 2017 · 6 comments

Comments

@dagnall53
Copy link

dagnall53 commented Dec 14, 2017

@romaca made a comment in bbx10/SFX-I2S-web-trigger#3,
Where he suggests the following code will correct some issues: Unfortunately this looks very different in your ESP8266wavplay library code, so I was hoping you could add this shift to your "library" version please??

`Here's my play loop:-

#define SLC_BUF_LEN (64) // Use the same buffer size as DMA

int playWaveFile(wavFILE_t *wavFile,wavProperties_t props){
static uint8_t readBuffer[SLC_BUF_LEN2]; // DMA uses 32bits (only mono therefore 16 bits) / reads from SPIFFS in bytes
static uint16_t *p = (uint16_t *)readBuffer;
int count;

i2s_begin();
i2s_set_rate(props->sampleRate);
//Serial.println(props->sampleRate);
int bShiftIn= 0;
int bShiftOut = 0;

// Note: 8266 little endian - wav file little endian - should be good

while((count = wavRead(wavFile,readBuffer, SLC_BUF_LEN*2)) > 0) {
//Serial.print("Bytes transferred "); Serial.println(accumulated_count);
count >>=1;
for(p=(uint16_t *)readBuffer;count;count--,p++) {
// Shift output data 1 bit to the right to conform to I2S (non left justified) as
// hardware outputs data left justified.
bShiftOut = *p & 0x0001;
*p = (*p>>1 & 0x7fff) | (bShiftIn<<15);
bShiftIn = bShiftOut;
//Serial.println(String(*p,HEX));
i2s_write_lr(*p,p); //left channel data = right channel (mono) - blocks when full - DMA's in background
}
//Serial.print("
");
yield();
}
i2s_end();`

@dagnall53
Copy link
Author

dagnall53 commented Dec 15, 2017

I think this may be the correction as applied in your code:
Unfortunately, it does not seem to fully sort the noise issue, which may therefore be due to the LRC phasing...

bool ICACHE_FLASH_ATTR i2s_write_lr_nb(int16_t left, int16_t right){
int sampler = right & 0xFFFE; // was FFFF trying 1 bit right shift as per
// bbx10/SFX-I2S-web-trigger#3
int samplel= left & 0xFFFE;
sampler = sampler << 15; // bit shift left was 16, so this equates to one right
samplel = samplel >> 1; // right shift left channel
sampler |= samplel;
return i2s_write_sample_nb(sampler);
}
<

@romaca
Copy link

romaca commented Dec 15, 2017

@dagnall53 Since I made this post I have determine that shifting, although may solve one of the problems, that patch is hiding a more fundamental bug imo. There should be no need to do this as I discovered after using Earle Philhower's recent library at https://github.com/earlephilhower/ESP8266Audio.git. This library seems to be an excellent library and includes the MAD MP3 decoder. If you want to continue with @techman83's code it maybe useful to eyeball the relevant parts of Earle's code to determine why it behaves differently. The code also btw has no issues with the clocks either.
Yes I did rewrite the play loop to ensure I understood what was happening and to make it as efficient loading the buffers as possible. After opening a wav file just call the playWaveFile(..) function from the loop() function. If you want to repeat the file you'll have to close then reopen the wave file. Also my shift routine treats the entire audio file as a long bitstream which gets shifted one bit to the right. I believe you're taking each sample from the left and right channel and shifting them which isn't the same, as you loose the MSB of sampler.

@dagnall53
Copy link
Author

dagnall53 commented Dec 16, 2017 via email

@techman83
Copy link
Owner

Earle Philhower's library is far more fleshed out and thorough. I pretty much made some minimal changes to bbx10's code and made it behave like a library, but it needs much more work to be properly releasable. I'll update my readme's to point at that one, because I'll likely update my Doorbell to that as MP3 support would be super nice!

As for the crashing, I'm using the code as "released" and no changes to the ESP codebase. I did have issues with distortion, but using a proper power supply rather than USB solved that. Considering how loud it can drive the speaker I plugged into the dac I'm using, I'm entirely unsurprising.

Thanks for commenting @romaca!

@dagnall53
Copy link
Author

I'm using Earles code now so thought this issue should be closed..

@techman83
Copy link
Owner

Awesome! Glad it worked out 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants