-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
208 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
lib/controller/platform/waveform_extractor/waveform_extractor.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path/path.dart' as p; | ||
import 'package:waveform_extractor/waveform_extractor.dart' as pkgwaveform; | ||
|
||
import 'package:namida/class/file_parts.dart'; | ||
import 'package:namida/controller/ffmpeg_controller.dart'; | ||
import 'package:namida/controller/platform/base.dart'; | ||
import 'package:namida/core/constants.dart'; | ||
import 'package:namida/core/extensions.dart'; | ||
|
||
part 'waveform_extractor_android.dart'; | ||
part 'waveform_extractor_base.dart'; | ||
part 'waveform_extractor_windows.dart'; |
25 changes: 25 additions & 0 deletions
25
lib/controller/platform/waveform_extractor/waveform_extractor_android.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
part of 'waveform_extractor.dart'; | ||
|
||
class _WaveformExtractorAndroid extends WaveformExtractor { | ||
_WaveformExtractorAndroid._internal(); | ||
|
||
final _extractor = pkgwaveform.WaveformExtractor(); | ||
|
||
@override | ||
void init() {} | ||
|
||
@override | ||
Future<List<num>> extractWaveformData( | ||
String source, { | ||
bool useCache = true, | ||
String? cacheKey, | ||
int? samplesPerSecond, | ||
}) { | ||
return _extractor.extractWaveformDataOnly( | ||
source, | ||
useCache: useCache, | ||
cacheKey: cacheKey, | ||
samplePerSecond: samplesPerSecond, | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
lib/controller/platform/waveform_extractor/waveform_extractor_base.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
part of 'waveform_extractor.dart'; | ||
|
||
abstract class WaveformExtractor { | ||
static WaveformExtractor platform() { | ||
return NamidaPlatformBuilder.init( | ||
android: () => _WaveformExtractorAndroid._internal(), | ||
windows: () => _WaveformExtractorWindows._internal(NamidaFFMPEG.inst), | ||
); | ||
} | ||
|
||
void init(); | ||
|
||
Future<List<num>> extractWaveformData( | ||
String source, { | ||
bool useCache = true, | ||
String? cacheKey, | ||
int? samplesPerSecond, | ||
}); | ||
|
||
int getSampleRateFromDuration({ | ||
required Duration audioDuration, | ||
int maxSampleRate = 400, | ||
double scaleFactor = 0.4, | ||
}) { | ||
return pkgwaveform.WaveformExtractor.getSampleRateFromDuration( | ||
audioDuration: audioDuration, | ||
maxSampleRate: maxSampleRate, | ||
scaleFactor: scaleFactor, | ||
); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
lib/controller/platform/waveform_extractor/waveform_extractor_windows.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
part of 'waveform_extractor.dart'; | ||
|
||
class _WaveformExtractorWindows extends WaveformExtractor { | ||
final NamidaFFMPEG ffmpegController; | ||
_WaveformExtractorWindows._internal(this.ffmpegController); | ||
|
||
late String ffmpegExePath; | ||
late String waveformExePath; | ||
|
||
static const _supportedFormats = <String>{'wav', 'flac', 'mp3', 'ogg', 'opus', 'webm'}; | ||
|
||
@override | ||
void init() { | ||
final executablesPath = NamidaPlatformBuilder.getExecutablesPath(); | ||
ffmpegExePath = p.join(executablesPath, 'ffmpeg.exe'); | ||
waveformExePath = p.join(executablesPath, 'audiowaveform.exe'); | ||
} | ||
|
||
@override | ||
Future<List<num>> extractWaveformData( | ||
String source, { | ||
bool useCache = true, | ||
String? cacheKey, | ||
int? sampleRate, | ||
int? samplesPerSecond, | ||
}) async { | ||
File? cacheFile; | ||
if (useCache) { | ||
final cacheDir = AppDirs.APP_CACHE; | ||
cacheKey ??= source.hashCode.toString(); | ||
cacheFile = FileParts.join(cacheDir, '$cacheKey.txt'); | ||
final cachedWaveform = _parseWaveformFile(cacheFile); | ||
if (cachedWaveform != null && cachedWaveform.isNotEmpty) return cachedWaveform; | ||
} | ||
final wavelist = await _extractWaveformNew( | ||
source, | ||
cacheFile: cacheFile, | ||
sampleRate: sampleRate, | ||
samplesPerSecond: samplesPerSecond, | ||
); | ||
if (cacheFile != null) _encodeWaveformFile(cacheFile, wavelist); | ||
return wavelist; | ||
} | ||
|
||
List<num>? _parseWaveformFile(File cacheFile) { | ||
if (cacheFile.existsSync()) return LineSplitter.split(cacheFile.readAsStringSync()).map((e) => num.parse(e)).toList(); | ||
return null; | ||
} | ||
|
||
void _encodeWaveformFile(File cacheFile, List<num> list) { | ||
if (list.isNotEmpty) cacheFile.writeAsStringSync(list.join('\n')); | ||
} | ||
|
||
Future<List<num>> _extractWaveformNew( | ||
String source, { | ||
File? cacheFile, | ||
int? sampleRate, | ||
int? samplesPerSecond, | ||
}) async { | ||
final extension = source.getExtension; | ||
const multiplier = 0.05; | ||
final waveformOutputOptions = [ | ||
'--output-format', | ||
'json', | ||
'-b', | ||
'8', | ||
if (samplesPerSecond != null) ...[ | ||
'--pixels-per-second', | ||
'$samplesPerSecond', | ||
], | ||
]; | ||
ProcessResult audioWaveformGenerate; | ||
bool needsConversion = true; | ||
String? convertedFilePath; | ||
final probablyGoodFormat = _supportedFormats.contains(extension); | ||
if (probablyGoodFormat) { | ||
try { | ||
audioWaveformGenerate = await Process.run(waveformExePath, ['-i', source, '--input-format', extension, ...waveformOutputOptions]); | ||
needsConversion = false; | ||
} catch (_) {} | ||
} | ||
if (needsConversion) { | ||
convertedFilePath = FileParts.joinPath(AppDirs.APP_CACHE, '${source.hashCode}.wav'); | ||
final ffmpegConvert = await Process.run( | ||
ffmpegExePath, | ||
['-i', source, '-f', 'wav', convertedFilePath], | ||
); | ||
if (ffmpegConvert.exitCode != 0) return <num>[]; | ||
} | ||
|
||
audioWaveformGenerate = await Process.run( | ||
waveformExePath, | ||
['-i', convertedFilePath ?? source, '--input-format', 'wav', ...waveformOutputOptions], | ||
); | ||
if (convertedFilePath != null) File(convertedFilePath).delete().catchError((_) => File('')); | ||
|
||
final data = jsonDecode(audioWaveformGenerate.stdout)?['data'] as List?; | ||
final finalList = data?.cast<num>() ?? []; | ||
|
||
final combinedList = <num>[]; | ||
final maxLength = finalList.length % 2 == 0 ? finalList.length : finalList.length - 1; // ensure even number for pair combination | ||
for (int i = 0; i < maxLength; i += 2) { | ||
final left = finalList[i].abs(); | ||
final right = finalList[i + 1].abs(); | ||
final combined = left > right ? left : right; | ||
final finalnumber = combined * multiplier; | ||
combinedList.add(finalnumber); | ||
} | ||
return combinedList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters