forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.dart
More file actions
51 lines (42 loc) · 1.63 KB
/
Copy pathfile.dart
File metadata and controls
51 lines (42 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:omi/utils/audio/wav_bytes.dart';
import 'package:omi/utils/logger.dart';
class FileUtils {
static Future<File> saveAudioBytesToTempFile(List<List<int>> chunk, int timerStart, int frameSize) async {
final directory = await getTemporaryDirectory();
String filePath = '${directory.path}/audio_fs${frameSize}_${timerStart}.bin';
List<int> data = [];
for (int i = 0; i < chunk.length; i++) {
var frame = chunk[i];
// Format: <length>|<data> ; bytes: 4 | n
final byteFrame = ByteData(frame.length);
for (int i = 0; i < frame.length; i++) {
byteFrame.setUint8(i, frame[i]);
}
data.addAll(Uint32List.fromList([frame.length]).buffer.asUint8List());
data.addAll(byteFrame.buffer.asUint8List());
}
final file = File(filePath);
await file.writeAsBytes(data);
return file;
}
static Future<File> convertPcmToWavFile(Uint8List pcmBytes, int sampleRate, int channels) async {
try {
// Convert PCM to WAV bytes
final wavBytes = WavBytes.fromPcm(pcmBytes, sampleRate: sampleRate, numChannels: channels).asBytes();
// Create a temporary file
final tempDir = await getTemporaryDirectory();
final tempPath = '${tempDir.path}/recording_${DateTime.now().millisecondsSinceEpoch}.wav';
final file = File(tempPath);
// Write WAV bytes to file
await file.writeAsBytes(wavBytes);
return file;
} catch (e) {
Logger.debug('Error converting PCM to WAV: $e');
rethrow;
}
}
}