108 lines
2.9 KiB
JavaScript
108 lines
2.9 KiB
JavaScript
const express = require('express');
|
||
const { createServer } = require('node:http');
|
||
const { Server } = require('socket.io');
|
||
const { SerialPort } = require('serialport');
|
||
const { ReadlineParser } = require('@serialport/parser-readline');
|
||
const { spawn } = require('child_process');
|
||
|
||
const app = express();
|
||
const server = createServer(app);
|
||
const io = new Server(server);
|
||
|
||
const PORT = 3000;
|
||
const SOUND_FILE = 'assets/sounds/muddy_files.mp3'; // chemin vers ton fichier mp3
|
||
let mpg123Process = null;
|
||
let lastPosition = null;
|
||
let lastTime = null;
|
||
let stopTimeout = null;
|
||
|
||
const arduinoCOMPort = '/dev/ttyUSB0'; // adapte si besoin
|
||
const arduinoSerialPort = new SerialPort({
|
||
path: arduinoCOMPort,
|
||
baudRate: 9600
|
||
});
|
||
|
||
// Lorsqu’on ouvre la connexion série
|
||
arduinoSerialPort.on('open', () => {
|
||
console.log('Port série ouvert:', arduinoCOMPort);
|
||
});
|
||
|
||
const parser = arduinoSerialPort.pipe(new ReadlineParser({ delimiter: '\n' }));
|
||
|
||
// Middleware
|
||
app.use(express.static('assets'));
|
||
|
||
// Routes
|
||
app.get('/', (req, res) => {
|
||
res.sendFile('assets/main.html', { root: __dirname });
|
||
});
|
||
|
||
// Socket.io
|
||
io.on('connection', (socket) => {
|
||
console.log('Un client s\'est connecté');
|
||
|
||
parser.on('data', (data) => {
|
||
const position = parseInt(data.trim(), 10);
|
||
if (isNaN(position)) return;
|
||
|
||
socket.emit('position', position); // envoie au client pour l'animation
|
||
|
||
const now = Date.now();
|
||
if (lastPosition !== null && lastTime !== null) {
|
||
const deltaPos = position - lastPosition;
|
||
const deltaTime = (now - lastTime) / 1000;
|
||
const speed = deltaPos / deltaTime;
|
||
|
||
clearTimeout(stopTimeout);
|
||
|
||
if (Math.abs(speed) < 5) {
|
||
stopPlayback();
|
||
return;
|
||
}
|
||
|
||
if (speed < 0) {
|
||
stopPlayback(); // Rotation inverse = arrêt
|
||
} else {
|
||
// Rotation positive = jouer le son
|
||
const rate = Math.min(Math.max(0.5 + speed / 400, 0.5), 1.5);
|
||
startOrAdjustPlayback(rate);
|
||
}
|
||
}
|
||
|
||
lastPosition = position;
|
||
lastTime = now;
|
||
|
||
stopTimeout = setTimeout(() => {
|
||
stopPlayback();
|
||
}, 300);
|
||
});
|
||
});
|
||
|
||
// Fonctions de lecture audio
|
||
function startOrAdjustPlayback(speed = 1.0) {
|
||
if (mpg123Process === null) {
|
||
console.log(`⏯️ Lecture à ${speed.toFixed(2)}x`);
|
||
mpg123Process = spawn('mpg123', ['-k', '0', SOUND_FILE]);
|
||
|
||
mpg123Process.on('exit', (code) => {
|
||
console.log(`🔇 mpg123 terminé (code ${code})`);
|
||
mpg123Process = null;
|
||
});
|
||
} else {
|
||
console.log(`⚙️ (Note : mpg123 ne supporte pas le changement de vitesse à chaud)`);
|
||
// Pour changer la vitesse, il faudrait tuer et relancer mpg123 avec options spécifiques
|
||
}
|
||
}
|
||
|
||
function stopPlayback() {
|
||
if (mpg123Process !== null) {
|
||
console.log('🛑 Arrêt de la lecture');
|
||
mpg123Process.kill('SIGKILL');
|
||
mpg123Process = null;
|
||
}
|
||
}
|
||
|
||
server.listen(PORT, () => {
|
||
console.log(`Serveur en écoute sur http://localhost:${PORT}`);
|
||
});
|