Skip to content

Commit 1f6ba4d

Browse files
committed
Commit
1 parent 8078f7e commit 1f6ba4d

18 files changed

+1371
-328
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
**/secrets.h
12
build/
2-
secrets.h
3-
managed_components/*/
3+
spiffs/
4+
managed_components/
5+
*.uf2

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# ESP32-S3 Scream Receiver
1+
# ESP32-ScreamReader
22

3-
This receives scream data over tcp to an esp32-s3 and outputs it to a UAC 1.0 sound device, very much WIP and barely works at this stage
3+
This is an ESP32 screamreader that can target the ESP32 for SPDIF or ESP32S3 for USB UAC 1.0 Host.

dependencies.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies:
88
component_hash: null
99
source:
1010
type: idf
11-
version: 5.0.6
11+
version: 5.4.0
1212
manifest_hash: 4181ba46eaef906dc497541b34620c1ce46b918c380e8201c75bc7ffc1628d98
1313
target: esp32s3
1414
version: 1.0.0

main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
idf_component_register(SRCS "audio.c" "buffer.c" "network.c" "usb_audio_player_main.c"
1+
idf_component_register(SRCS "audio.c" "buffer.c" "network.c" "usb_audio_player_main.c" "spdif.c"
22
INCLUDE_DIRS ".")
33

44
target_link_libraries(${COMPONENT_LIB} "-L ${CMAKE_CURRENT_SOURCE_DIR}")

main/audio.c

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,87 @@
22
#include "buffer.h"
33
#include "freertos/FreeRTOS.h"
44
#include "freertos/task.h"
5-
#include "usb/uac_host.h"
65
#include "esp_log.h"
7-
6+
#ifdef IS_SPDIF
7+
#include "spdif.h"
8+
#endif
9+
#ifdef IS_USB
10+
#include "usb/uac_host.h"
811
uac_host_device_handle_t spkr_handle = NULL;
12+
#endif
913

1014
bool playing = false;
1115

12-
uint8_t volume = 2;
16+
uint8_t volume = 100;
1317
uint8_t silence[32] = {0};
18+
bool is_silent = false;
1419

15-
void start_playback(uac_host_device_handle_t _spkr_handle) {
16-
spkr_handle = _spkr_handle;
20+
void resume_playback() {
21+
#ifdef IS_USB
1722
uac_host_stream_config_t stm_config = {
1823
.channels = 2,
1924
.bit_resolution = 16,
2025
.sample_freq = 48000,
2126
};
22-
playing = true;
2327
ESP_ERROR_CHECK(uac_host_device_start(spkr_handle, &stm_config));
2428
ESP_ERROR_CHECK(uac_host_device_set_volume(spkr_handle, volume));
29+
#endif
30+
ESP_LOGI(TAG, "Resume Playback");
31+
playing = true;
2532
}
2633

34+
#ifdef IS_USB
35+
void start_playback(uac_host_device_handle_t _spkr_handle) {
36+
spkr_handle = _spkr_handle;
37+
}
38+
#endif
39+
2740
void stop_playback() {
2841
playing = false;
42+
ESP_LOGI(TAG, "Stop Playback");
43+
#ifdef IS_USB
2944
uac_host_device_stop(spkr_handle);
45+
#endif
46+
}
47+
48+
void audio_direct_write(uint8_t *data) {
49+
#ifdef IS_USB
50+
uac_host_device_write(spkr_handle, data, PCM_CHUNK_SIZE, portMAX_DELAY);
51+
#endif
52+
#ifdef IS_SPDIF
53+
spdif_write(data, PCM_CHUNK_SIZE);
54+
#endif
3055
}
3156

3257
void pcm_handler(void*) {
3358
while (true) {
3459
if (playing) {
35-
uint8_t *data = pop_chunk();
36-
if (data) {
37-
int result = uac_host_device_write(spkr_handle, data, PCM_CHUNK_SIZE, portMAX_DELAY);
38-
}
39-
else
40-
for (int i=0;i<1152;i++)
41-
uac_host_device_write(spkr_handle, silence, 32, portMAX_DELAY);
60+
uint8_t *data = pop_chunk();
61+
if (data) {
62+
#ifdef IS_USB
63+
uac_host_device_write(spkr_handle, data, PCM_CHUNK_SIZE, portMAX_DELAY);
64+
#endif
65+
#ifdef IS_SPDIF
66+
spdif_write(data, PCM_CHUNK_SIZE);
67+
#endif
68+
is_silent = false;
69+
}
70+
else if (!is_silent) {
71+
ESP_LOGI(TAG, "Silent");
72+
is_silent = true;
73+
}
74+
//else
75+
// for (int i=0;i<1152/32;i++)
76+
// uac_host_device_write(spkr_handle, silence, 32, portMAX_DELAY);
77+
// vTaskDelay(7);
4278
}
4379
vTaskDelay(1);
4480
}
4581
}
4682

4783
void setup_audio() {
84+
#ifdef IS_SPDIF
85+
spdif_init(48000);
86+
#endif
4887
xTaskCreatePinnedToCore(pcm_handler, "pcm_handler", 16384, NULL, 1, NULL, 1);
4988
}

main/audio.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#pragma once
2+
#include "config.h"
3+
#ifdef IS_USB
24
#include "usb/uac_host.h"
5+
#endif
36
void process_audio_actions(bool is_startup);
47
void register_button(int button,void (*action)(bool, int, void *));
58
void setup_audio();
9+
#ifdef IS_USB
610
void start_playback(uac_host_device_handle_t _spkr_handle);
11+
#endif
712
void stop_playback();
8-
void audio_write(uint8_t* data);
13+
void audio_write(uint8_t* data);
14+
void audio_direct_write(uint8_t *data);
15+
void resume_playback();

main/buffer.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
#include "esp_psram.h"
88

99
// Flag if the stream is currently underrun and rebuffering
10-
bool is_underrun = true;
10+
bool is_underrun = true;
1111
// Number of received packets since last underflow
12-
uint64_t received_packets = 0;
12+
unsigned int received_packets = 0;
1313
// Number of packets in ring buffer
1414
unsigned int packet_buffer_size = 0;
1515
// Position of ring buffer read head
16-
uint64_t packet_buffer_pos = 0;
16+
unsigned int packet_buffer_pos = 0;
1717
// Number of bytes to buffer
18-
uint64_t target_buffer_size = INITIAL_BUFFER_SIZE;
18+
unsigned int target_buffer_size = INITIAL_BUFFER_SIZE;
1919
// Buffer of packets to send
2020
uint8_t *packet_buffer[MAX_BUFFER_SIZE] = { 0 };
2121
portMUX_TYPE buffer_mutex = portMUX_INITIALIZER_UNLOCKED;
@@ -24,20 +24,20 @@ void set_underrun() {
2424
if (!is_underrun) {
2525
received_packets = 0;
2626
target_buffer_size += BUFFER_GROW_STEP_SIZE;
27-
if (target_buffer_size >= MAX_BUFFER_SIZE)
28-
target_buffer_size = MAX_BUFFER_SIZE;
29-
ESP_LOGI(TAG, "Buffer Underflow");
27+
if (target_buffer_size >= MAX_GROW_SIZE)
28+
target_buffer_size = MAX_GROW_SIZE;
29+
ESP_LOGI(TAG, "Buffer Underflow, New Size: %i", target_buffer_size);
3030
}
3131
is_underrun = true;
3232
}
3333

3434
bool push_chunk(uint8_t *chunk) {
3535
int write_position = (packet_buffer_pos + packet_buffer_size) % MAX_BUFFER_SIZE;
3636
// ESP_LOGI(TAG, "memcpy(%p, %p, %i)", packet_buffer[write_position], chunk, PCM_CHUNK_SIZE);
37-
//taskENTER_CRITICAL(&buffer_mutex);
37+
taskENTER_CRITICAL(&buffer_mutex);
3838
if (packet_buffer_size == MAX_BUFFER_SIZE) {
3939
packet_buffer_size = target_buffer_size;
40-
//taskEXIT_CRITICAL(&buffer_mutex);
40+
taskEXIT_CRITICAL(&buffer_mutex);
4141
ESP_LOGI(TAG, "Buffer Overflow");
4242
return false;
4343
}
@@ -48,32 +48,38 @@ bool push_chunk(uint8_t *chunk) {
4848
received_packets++;
4949
if (received_packets >= target_buffer_size)
5050
is_underrun = false;
51-
//taskEXIT_CRITICAL(&buffer_mutex);
51+
taskEXIT_CRITICAL(&buffer_mutex);
5252
return true;
5353
}
5454

5555
uint8_t *pop_chunk() {
56-
//taskENTER_CRITICAL(&buffer_mutex);
56+
taskENTER_CRITICAL(&buffer_mutex);
5757
if (packet_buffer_size == 0) {
58-
//taskEXIT_CRITICAL(&buffer_mutex);
58+
taskEXIT_CRITICAL(&buffer_mutex);
5959
set_underrun();
6060
return NULL;
6161
}
6262
if (is_underrun) {
63-
//taskEXIT_CRITICAL(&buffer_mutex);
63+
taskEXIT_CRITICAL(&buffer_mutex);
6464
return NULL;
6565
}
6666
uint8_t *return_chunk = packet_buffer[packet_buffer_pos];
6767
packet_buffer_size--;
6868
packet_buffer_pos = (packet_buffer_pos + 1) % MAX_BUFFER_SIZE;
69-
//taskEXIT_CRITICAL(&buffer_mutex);
69+
taskEXIT_CRITICAL(&buffer_mutex);
7070
return return_chunk;
7171
}
7272

73+
void empty_buffer() {
74+
taskENTER_CRITICAL(&buffer_mutex);
75+
packet_buffer_size = 0;
76+
received_packets = 0;
77+
taskEXIT_CRITICAL(&buffer_mutex);
78+
}
79+
7380
void setup_buffer() {
7481
ESP_LOGI(TAG, "Allocating buffer");
7582
uint8_t *buffer = 0;
76-
7783
buffer = (uint8_t *)malloc(PCM_CHUNK_SIZE * MAX_BUFFER_SIZE);
7884
memset(buffer, 0, PCM_CHUNK_SIZE * MAX_BUFFER_SIZE);
7985
for (int i = 0; i < MAX_BUFFER_SIZE; i++)

main/buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ void setup_buffer();
1010
bool push_chunk(uint8_t *chunk);
1111
uint8_t *pop_chunk();
1212
void setup_buffer();
13+
void empty_buffer();

main/config.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#pragma once
22
// TCP port for Scream server data, configurable
33
#define PORT 4010
4-
// Scream server IP, configurable
5-
#define SERVER "192.168.3.114"
64

75
// Number of chunks to be buffered before playback starts, configurable
8-
#define INITIAL_BUFFER_SIZE 48
6+
#define INITIAL_BUFFER_SIZE 4
97
// Number of chunks to add each underflow, configurable
10-
#define BUFFER_GROW_STEP_SIZE 16
8+
#define BUFFER_GROW_STEP_SIZE 0
119
// Max number of chunks to be buffered before packets are dropped, configurable
12-
#define MAX_BUFFER_SIZE 128
10+
#define MAX_BUFFER_SIZE 16
11+
// Max number of chunks to be targeted for buffer
12+
#define MAX_GROW_SIZE 4
1313

1414
// Sample rate for incoming PCM, configurable
1515
#define SAMPLE_RATE 48000
@@ -18,4 +18,7 @@
1818
//Volume 0.0f-1.0f
1919
#define VOLUME 1.0f
2020

21+
//#define IS_SPDIF
22+
#define IS_USB
23+
2124
#define TAG "scream_receiver"

main/idf_component.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
dependencies:
2-
idf: ">=5.0" # The version of the ESP-IDF that esp-audio-player requires
3-
usb_host_uac: "1.0.*"
2+
idf: ">=5.0"
3+
usb_host_uac: "1.0.*"

0 commit comments

Comments
 (0)