audio_codec 0.0.2 copy "audio_codec: ^0.0.2" to clipboard
audio_codec: ^0.0.2 copied to clipboard

A library to decode and perhaps encode audios

example/main.dart

import 'dart:io';
import 'dart:typed_data';

import 'package:audio_codec/src/flac/flac_decoder.dart';
import 'package:audio_codec/src/wav/wav_encoder.dart';

void main() {
  final flacFile = File('test.flac');

  final decoder = FlacDecoder(track: flacFile);
  final result = decoder.decode();

  print(result.streamInfoBlock);

  final pcmSamples = Int32List(
    result.streamInfoBlock!.totalSamples * result.streamInfoBlock!.channels,
  );

  // Decode frames and write to PCM
  int frameNumber = 0;

  while (decoder.hasNextFrame()) {
    final frame = decoder.readFrame();

    writeFrameToPcm(
      pcmSamples,
      frame,
      frameNumber,
      result.streamInfoBlock!.sampleRate,
    );

    frameNumber++;
  }

  decoder.close();

  WavEncoder(
    sampleRate: result.streamInfoBlock!.sampleRate,
    numChannels: result.streamInfoBlock!.channels,
    bitDepth: result.streamInfoBlock!.bitsPerSample,
  ).encode(
    File("output.wav"),
    pcmSamples,
  );
}

void writeFrameToPcm(
    Int32List samples, FlacFrame frame, int frameNumber, int sampleRate) {
  final numChannels = frame.channels.nbChannels;
  final numSamples = frame.blockSize;

  // Calculate the starting index in 'samples' for this frame
  final frameStart = frameNumber * numSamples * numChannels;

  // Interleave and write samples
  for (int i = 0; i < numSamples; i++) {
    for (int c = 0; c < numChannels; c++) {
      // Write directly to the correct position in 'samples'
      samples[frameStart + i * numChannels + c] = frame.subframes[c][i];
    }
  }
}
2
likes
150
points
30
downloads

Publisher

unverified uploader

Weekly Downloads

A library to decode and perhaps encode audios

Homepage
Repository (GitHub)
View/report issues

Topics

#audio #codec #decoder #encoder

Documentation

API reference

License

MIT (license)

Dependencies

convert, crypto

More

Packages that depend on audio_codec