Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion cores/esp8266/core_esp8266_i2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bool ICACHE_FLASH_ATTR i2s_is_empty(){
}

int16_t ICACHE_FLASH_ATTR i2s_available(){
return (SLC_BUF_CNT - i2s_slc_queue_len) * SLC_BUF_LEN;
return (i2s_slc_queue_len * SLC_BUF_LEN) + (SLC_BUF_LEN - i2s_curr_slc_buf_pos);
}

uint32_t ICACHE_FLASH_ATTR i2s_slc_queue_next_item(){ //pop the top off the queue
Expand Down Expand Up @@ -195,6 +195,47 @@ bool ICACHE_FLASH_ATTR i2s_write_sample_nb(uint32_t sample) {
return true;
}

#define MIN(a,b) (a<b?a:b)

// writes a buffer of samples into the DMA memory, returns the amount of samples written
int16_t ICACHE_FLASH_ATTR i2s_write_buffer_mono(int16_t *sample, int16_t len) {
int16_t samples_written=0;

for(;;) {
// make sure we have room in the current buffer
if (i2s_curr_slc_buf_pos==SLC_BUF_LEN || i2s_curr_slc_buf==NULL){
// no room in the current buffer? if there are no buffers available then exit
if(i2s_slc_queue_len == 0)
break;

// get a new buffer
ETS_SLC_INTR_DISABLE();
i2s_curr_slc_buf = (uint32_t *)i2s_slc_queue_next_item();
ETS_SLC_INTR_ENABLE();
i2s_curr_slc_buf_pos=0;
}

if (len==0)
break;

//space available in the current buffer
int16_t available = SLC_BUF_LEN - i2s_curr_slc_buf_pos;

int16_t m = MIN(available,len);

for(int16_t i=0;i<m;i++){
int16_t v = (*sample);
uint32_t vv = (v<<16) | (v & 0xffff);
i2s_curr_slc_buf[i2s_curr_slc_buf_pos++] = vv;
sample++;
}
len -= m;
samples_written += m;
}
return samples_written;
}


bool ICACHE_FLASH_ATTR i2s_write_lr(int16_t left, int16_t right){
int sample = right & 0xFFFF;
sample = sample << 16;
Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/i2s.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ bool i2s_write_lr(int16_t left, int16_t right);//combines both channels and call
bool i2s_is_full();//returns true if DMA is full and can not take more bytes (overflow)
bool i2s_is_empty();//returns true if DMA is empty (underflow)
int16_t i2s_available();// returns the number of samples than can be written before blocking
int16_t ICACHE_FLASH_ATTR i2s_write_buffer_mono(int16_t *sample, int16_t len); //writes a number of samplers in the the DMA mem, returns the numner of written samples

#ifdef __cplusplus
}
Expand Down