44 float x = (float)sample / 32768.0f;
46 float y = copysignf(log1pf(MU * fabsf(x)) / log1pf(MU), x);
48 int log12 = (int)(y * 2047.0f);
50 if (log12 > 2047) log12 = 2047;
51 if (log12 < -2047) log12 = -2047;
53 return (int16_t)log12;
77 i2s_config_t i2s_config = {
78 .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
79 .sample_rate = I2S_SAMPLE_RATE,
80 .bits_per_sample = i2s_bits_per_sample_t(I2S_SAMPLE_BITS),
81 .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
82 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
83 .intr_alloc_flags = 0,
89 i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
91 const i2s_pin_config_t pin_config = {
92 .bck_io_num = I2S_SCK,
98 i2s_set_pin(I2S_PORT, &pin_config);
112 i2s_read(I2S_PORT, (
void*) i2s_read_buff, I2S_READ_LEN, &bytes_read, portMAX_DELAY);
115 int32_t* buffer32 = (int32_t*)i2s_read_buff;
116 int samples = bytes_read /
sizeof(int32_t);
119 for (
int i = 0; i < samples; i+=2) {
120 int16_t left = buffer32[i] >> 16;
121 int16_t right = buffer32[i + 1] >> 16;
126 flash_write_buff[j++] = left12 & 0xFF;
127 flash_write_buff[j++] = ((left12 >> 8) & 0x0F) | (right4 << 4);
130 SerialBT.write((
const byte*) flash_write_buff, j);
132 vTaskDelay(10 / portTICK_PERIOD_MS);
143 Serial.println(
"I2S Initialized!");
145 xTaskCreate(
i2s_adc,
"i2s_adc", 2048, NULL, 1, &taskHandler);
147 vTaskDelay(100 / portTICK_PERIOD_MS);
149 Serial.println(
"I2S released!");
150 i2s_driver_uninstall(I2S_PORT);
151 vTaskDelete(taskHandler);
153 vTaskDelay(100 / portTICK_PERIOD_MS);
187void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
188 if (event == ESP_SPP_SRV_OPEN_EVT) {
189 Serial.println(
"Client Connected!");
190 esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
193 else if (event == ESP_SPP_DATA_IND_EVT) {
194 Serial.printf(
"ESP_SPP_DATA_IND_EVT len=%d handle=%d\n", param->data_ind.len, param->data_ind.handle);
196 if (param->data_ind.len > 0) {
197 int dataLen = param->data_ind.len;
198 char textArray[dataLen + 1];
199 strncpy(textArray, (
const char*)param->data_ind.data, dataLen);
200 textArray[dataLen] =
'\0';
201 String textString = textArray;
204 Serial.printf(
"*** Text String: %s\n", textString.c_str());
206 if (textString.equals(
"START")) {
209 else if (textString.equals(
"STOP")) {
214 else if (event == ESP_SPP_CLOSE_EVT){
215 Serial.println(
"Client Disconnected!");
216 esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
uint8_t compress_log4(int16_t sample)
將 16-bit PCM 壓到 4-bit(0~15),作為輔助通道
void i2sTask(void *pvParameters)
I2S 主任務(負責啟動與釋放 I2S 驅動)
void initBT(const char *name="Recorder")
啟動藍牙並註冊 callback
int16_t compress_log12(int16_t sample)
μ-law 壓縮的自訂 12-bit 版本
void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
Bluetooth SPP 事件callback.