Skip to content

A flutter plugin for playing raw PCM audio data (16-bit integer)

License

Notifications You must be signed in to change notification settings

wsievern/flutter_pcm_sound

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pub package

Logo

Send real-time PCM audio (16-bit integer) to your device speakers, from your Flutter app!

No Dependencies

FlutterPcmSound has zero dependencies besides Flutter, Android, iOS, and MacOS themselves.

Callback Based, For Real-Time Audio

In contrast to raw_sound, FlutterPcmSound uses a callback setFeedCallback to signal when to feed more samples.

You can lower the feed threshold using setFeedThreshold to achieve real time audio, or increase it to have a cushy buffer.

You can also manually feed whenever you like.

Usage

// for testing purposes, a C-Major scale 
MajorScale scale = MajorScale(sampleRate: 44100, noteDuration: 0.25);

// invoked whenever we need to feed more samples to the platform
void onFeed(int remainingFrames) async {
    // you could use 'remainingFrames' to feed very precisely.
    // But here we just load a few thousand samples everytime we run low.
    List<int> frame = scale.generate(periods: 100);
    await FlutterPcmSound.feed(PcmArrayInt16.fromList(frame));
}

await FlutterPcmSound.setup(sampleRate: 44100, channelCount: 1);
await FlutterPcmSound.setFeedThreshold(8000); // feed when below 8000 queued frames
await FlutterPcmSound.setFeedCallback(onFeed);
await FlutterPcmSound.play();

Other Functions

// suspend playback but does *not* clear queued samples
await FlutterPcmSound.pause();

// clears all queued samples
await FlutterPcmSound.clear();

// suspend playback & clear queued samples
await FlutterPcmSound.stop();

// get the current number of queued frames
int samples = await FlutterPcmSound.remainingFrames();

⭐ Stars ⭐

Please star this repo & on pub.dev. We all benefit from having a larger community.

Example App

Enable the platforms you need.

cd ./example                      
flutter config --enable-macos-desktop                                                      
flutter config --enable-android 
flutter config --enable-ios 
flutter create .
flutter run

About

A flutter plugin for playing raw PCM audio data (16-bit integer)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 35.7%
  • Objective-C 32.3%
  • Dart 27.1%
  • Ruby 4.9%