Skip to content

Embed Presentation

Do What

music box สำหรับ gen เพลงโดยมีการอ้างอิง pattern มาจากnoteของuser

Do Why

คิง: ชอบเกี่ยวกับเพลง โอ: อยากได้something ที่เกี่ยวข้อกับAI (อยากทำเกี่ยวกับAI) +ทั้งคิงและโอรู้สึกว่ามันดูcreativeดี
Copy of Hardware devices
devices
Quantity
esp32 node32 lite
1
MAX98357
1
Speaker 3W
1
Keypad matrix 4*4
1
There are no rows in this table

Schematic

Screenshot 2025-11-04 at 11.19.13 PM.png
Copy of Pin
devices
Pin
Duty
Keypad matrix4*4
IO14, IO27, IO19, IO18 (rowPin1-4) IO4, IO13, IO32, IO33 (colPin5-8)
I/O Pin
MAX98357
IO25 (LRCLK), IO26 (BCLK), IO16 (DIN), GND, VCC(5V)
I/O Pin
Speaker 3W
VCC(+), GND(-)
-
There are no rows in this table

Architect Overview

Script.png

Component

Esp32 (Client)
HW Interface: Keypad, I2S Audio
Processing: Multi-threading
Core 0: Request API
Core 1: Play sound (High Priority)
Communication: HTTPS (nissorn.codes) → Wifi with TLS/SSL
Safety: Watchdog, Mutex lock
Raspberry PI (Server)
AI Model: Markov Chain 4th-order (with PyTorch for ARM)
API: FastAPI RESTful web service
Deployment: Systemd service start auto
Cloud
CDN: Cloudflare global edge nw
Security: SSL/ TLS encryption


Firmware


4*4 Matrix

const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};

byte rowPins[ROWS] = { 14, 27, 19, 18 };
byte colPins[COLS] = { 4, 13, 32, 33 };

I2S Configuration

i2s_config_t i2s_config = {
//ESP32 เป็น master (สร้าง clock), TX = ส่งข้อมูล
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
//ส่ง sample 16000 ตัวต่อวินาที
.sample_rate = 16000,
//แต่ละ sample ใช้ 16 bits
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
//สลับส่ง Left แล้ว Right
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
//ใช้ standard I2S protocol
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
//Interrupt priority level 1
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
//ใช้ 8 DMA buffers
.dma_buf_count = 8,

//แต่ละ buffer เก็บได้ 128 samples
.dma_buf_len = 128,
// ไม่ใช้ APLL (Audio PLL) ใช้ clock ปกติ
.use_apll = false,
// ล้าง descriptor อัตโนมัติหลังส่งเสร็จ
.tx_desc_auto_clear = true
};



Audio Generate

Sine Wave

image.png
void generateAndPlayTone() {
unsigned long elapsed = millis() - noteStartTime;
float envelope = 1.0;

// ADSR Envelope - Fade out
int fadeTime = min(80, currentNoteDuration / 4);
if (elapsed > currentNoteDuration - fadeTime) {
envelope = (float)(currentNoteDuration - elapsed) / fadeTime;
envelope = max(0.0f, min(1.0f, envelope));
}

float volumeMultiplier = getVolumeMultiplier();

// Generate samples
for (int i = 0; i < BUFFER_SIZE; i++) {
float angle = 2.0 * PI * currentFrequency * sampleCounter / SAMPLE_RATE;
int16_t sample = (int16_t)(sin(angle) * 20000 * envelope * volumeMultiplier);
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.