@karaplay/file-coder 1.4.9 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/demo-server.js ADDED
@@ -0,0 +1,184 @@
1
+ const express = require('express');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const { convertEmkToKar } = require('./dist/index.js');
5
+ const { Midi } = require('@tonejs/midi');
6
+
7
+ const app = express();
8
+ const PORT = 3000;
9
+
10
+ // Serve static files
11
+ app.use(express.static(__dirname));
12
+
13
+ // Serve karaoke-player library
14
+ app.use('/lib', express.static(__dirname + '/node_modules/karaoke-player/lib'));
15
+
16
+ // API: Get list of EMK files
17
+ app.get('/api/emk-files', (req, res) => {
18
+ try {
19
+ const emkDir = path.join(__dirname, 'songs/emk');
20
+ const files = fs.readdirSync(emkDir)
21
+ .filter(f => f.endsWith('.emk'))
22
+ .map(file => {
23
+ const filePath = path.join(emkDir, file);
24
+ const stats = fs.statSync(filePath);
25
+ return {
26
+ name: file,
27
+ size: stats.size,
28
+ path: `/songs/emk/${file}`
29
+ };
30
+ });
31
+
32
+ res.json({ success: true, files });
33
+ } catch (error) {
34
+ res.status(500).json({ success: false, error: error.message });
35
+ }
36
+ });
37
+
38
+ // API: Analyze EMK file (before conversion)
39
+ app.post('/api/analyze-emk', express.json(), async (req, res) => {
40
+ try {
41
+ const { filename } = req.body;
42
+
43
+ if (!filename) {
44
+ return res.status(400).json({ success: false, error: 'Filename required' });
45
+ }
46
+
47
+ const inputEmk = path.join(__dirname, 'songs/emk', filename);
48
+
49
+ console.log(`Analyzing ${filename}...`);
50
+
51
+ // Decode EMK
52
+ const { decodeEmkServer } = require('./dist/index.js');
53
+ const emkBuffer = fs.readFileSync(inputEmk);
54
+ const decoded = decodeEmkServer(emkBuffer);
55
+
56
+ // Analyze MIDI with Tone.js
57
+ const midi = new Midi(decoded.midi);
58
+ const originalTempo = midi.header.tempos.length > 0 ? midi.header.tempos[0].bpm.toFixed(2) : 'N/A';
59
+ const originalDuration = midi.duration.toFixed(2);
60
+ const ppq = midi.header.ppq;
61
+
62
+ // Parse lyric for metadata
63
+ const { parseLyricFile } = require('./dist/index.js');
64
+
65
+ // Write temp lyric file
66
+ const tempDir = path.join(__dirname, 'temp');
67
+ if (!fs.existsSync(tempDir)) {
68
+ fs.mkdirSync(tempDir, { recursive: true });
69
+ }
70
+ const tempLyricFile = path.join(tempDir, 'temp.lyr');
71
+ fs.writeFileSync(tempLyricFile, decoded.lyric);
72
+
73
+ const lyricMeta = parseLyricFile(tempLyricFile);
74
+ fs.unlinkSync(tempLyricFile);
75
+
76
+ // Count notes
77
+ let totalNotes = 0;
78
+ midi.tracks.forEach(track => {
79
+ totalNotes += track.notes.length;
80
+ });
81
+
82
+ res.json({
83
+ success: true,
84
+ emkInfo: {
85
+ format: decoded.isZxioFormat ? 'ZXIO' : 'MThd',
86
+ title: lyricMeta.title || 'Unknown',
87
+ artist: lyricMeta.artist || 'Unknown',
88
+ tempo: originalTempo,
89
+ duration: originalDuration,
90
+ durationMinutes: (parseFloat(originalDuration) / 60).toFixed(2),
91
+ ppq: ppq,
92
+ tracks: midi.tracks.length,
93
+ notes: totalNotes,
94
+ hasLyrics: decoded.lyric ? decoded.lyric.length : 0,
95
+ hasCursor: decoded.cursor ? decoded.cursor.length : 0
96
+ }
97
+ });
98
+
99
+ } catch (error) {
100
+ console.error('Analysis error:', error);
101
+ res.status(500).json({
102
+ success: false,
103
+ error: error.message
104
+ });
105
+ }
106
+ });
107
+
108
+ // API: Convert EMK to KAR
109
+ app.post('/api/convert', express.json(), async (req, res) => {
110
+ try {
111
+ const { filename } = req.body;
112
+
113
+ if (!filename) {
114
+ return res.status(400).json({ success: false, error: 'Filename required' });
115
+ }
116
+
117
+ const inputEmk = path.join(__dirname, 'songs/emk', filename);
118
+ const outputKar = path.join(__dirname, 'temp', filename.replace('.emk', '.kar'));
119
+
120
+ // Create temp directory if not exists
121
+ const tempDir = path.join(__dirname, 'temp');
122
+ if (!fs.existsSync(tempDir)) {
123
+ fs.mkdirSync(tempDir, { recursive: true });
124
+ }
125
+
126
+ console.log(`Converting ${filename}...`);
127
+
128
+ // Convert EMK to KAR
129
+ const result = convertEmkToKar({
130
+ inputEmk: inputEmk,
131
+ outputKar: outputKar,
132
+ keepIntermediateFiles: false
133
+ });
134
+
135
+ // Read the converted KAR
136
+ const karBuffer = fs.readFileSync(outputKar);
137
+
138
+ // Analyze with Tone.js
139
+ const midi = new Midi(karBuffer);
140
+ const tempo = midi.header.tempos.length > 0 ? midi.header.tempos[0].bpm.toFixed(2) : 'N/A';
141
+ const duration = midi.duration.toFixed(2);
142
+
143
+ // Convert to base64 for transmission
144
+ const karBase64 = karBuffer.toString('base64');
145
+
146
+ // Clean up
147
+ fs.unlinkSync(outputKar);
148
+
149
+ res.json({
150
+ success: true,
151
+ metadata: {
152
+ title: result.metadata.title,
153
+ artist: result.metadata.artist,
154
+ format: result.metadata.code ? 'ZXIO' : 'MThd',
155
+ tempo: tempo,
156
+ duration: duration,
157
+ durationMinutes: (parseFloat(duration) / 60).toFixed(2),
158
+ warnings: result.warnings
159
+ },
160
+ karData: karBase64
161
+ });
162
+
163
+ } catch (error) {
164
+ console.error('Conversion error:', error);
165
+ res.status(500).json({
166
+ success: false,
167
+ error: error.message,
168
+ stack: error.stack
169
+ });
170
+ }
171
+ });
172
+
173
+ // Start server
174
+ app.listen(PORT, () => {
175
+ console.log('='.repeat(80));
176
+ console.log('🎤 EMK to KAR Demo Server');
177
+ console.log('='.repeat(80));
178
+ console.log('');
179
+ console.log(`Server running at: http://localhost:${PORT}`);
180
+ console.log(`Demo page: http://localhost:${PORT}/demo-client.html`);
181
+ console.log('');
182
+ console.log('Press Ctrl+C to stop');
183
+ console.log('');
184
+ });