Skip to content

Commit

Permalink
feat: initial pencil sound implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Apr 21, 2024
1 parent efa3263 commit 652747c
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions assets/audio/white-noise-8117.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sound Effect by <a href="https://pixabay.com/users/blendertimer-9538909/?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=8117">Daniel Roberts</a> from <a href="https://pixabay.com/sound-effects//?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=8117">Pixabay</a>
Binary file added assets/audio/white-noise-8117.mp3
Binary file not shown.
31 changes: 31 additions & 0 deletions lib/data/editor/pencil_sound.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dart:math';

import 'package:audioplayers/audioplayers.dart';

/// Emulates the scratchy sound of pencil on paper.
abstract class PencilSound {
static const _source = 'audio/white-noise-8117.mp3';
static final _player = AudioPlayer()
..setSourceAsset(_source)
..setPlayerMode(PlayerMode.lowLatency)
..setVolume(0.1)
..setReleaseMode(ReleaseMode.loop);

static Future<void> preload() {
return _player.audioCache.loadPath(_source);
}

static void resume() {
_player.resume();
}

static void pause() {
_player.pause();
}

static void update(double distanceSquared) {
final speed = min(1, distanceSquared / 10000);
_player.setVolume(speed * 0.5);
_player.setPlaybackRate(1 - (1 - speed) * 0.5);
}
}
7 changes: 7 additions & 0 deletions lib/pages/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import 'package:saber/data/editor/editor_core_info.dart';
import 'package:saber/data/editor/editor_exporter.dart';
import 'package:saber/data/editor/editor_history.dart';
import 'package:saber/data/editor/page.dart';
import 'package:saber/data/editor/pencil_sound.dart';
import 'package:saber/data/extensions/change_notifier_extensions.dart';
import 'package:saber/data/file_manager/file_manager.dart';
import 'package:saber/data/prefs.dart';
Expand Down Expand Up @@ -551,6 +552,8 @@ class EditorState extends State<Editor> {
final position = page.renderBox!.globalToLocal(details.focalPoint);
history.canRedo = false;

PencilSound.resume();

if (currentTool is Pen) {
(currentTool as Pen)
.onDragStart(position, page, dragPageIndex!, currentPressure);
Expand Down Expand Up @@ -589,6 +592,9 @@ class EditorState extends State<Editor> {
final page = coreInfo.pages[dragPageIndex!];
final position = page.renderBox!.globalToLocal(details.focalPoint);
final offset = position - previousPosition;

PencilSound.update(offset.distanceSquared);

if (currentTool is Pen) {
(currentTool as Pen).onDragUpdate(position, currentPressure);
page.redrawStrokes();
Expand Down Expand Up @@ -624,6 +630,7 @@ class EditorState extends State<Editor> {
void onDrawEnd(ScaleEndDetails details) {
final page = coreInfo.pages[dragPageIndex!];
bool shouldSave = true;
PencilSound.pause();
setState(() {
if (currentTool is Pen) {
Stroke newStroke = (currentTool as Pen).onDragEnd();
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "generated_plugin_registrant.h"

#include <audioplayers_linux/audioplayers_linux_plugin.h>
#include <dynamic_color/dynamic_color_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <gtk/gtk_plugin.h>
Expand All @@ -18,6 +19,9 @@
#include <yaru_window_linux/yaru_window_linux_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) audioplayers_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "AudioplayersLinuxPlugin");
audioplayers_linux_plugin_register_with_registrar(audioplayers_linux_registrar);
g_autoptr(FlPluginRegistrar) dynamic_color_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "DynamicColorPlugin");
dynamic_color_plugin_register_with_registrar(dynamic_color_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
audioplayers_linux
dynamic_color
flutter_secure_storage_linux
gtk
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import FlutterMacOS
import Foundation

import audioplayers_darwin
import device_info_plus
import dynamic_color
import flutter_secure_storage_macos
Expand All @@ -19,6 +20,7 @@ import url_launcher_macos
import window_manager

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
DynamicColorPlugin.register(with: registry.registrar(forPlugin: "DynamicColorPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
Expand Down
64 changes: 64 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,62 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.11.0"
audioplayers:
dependency: "direct main"
description:
name: audioplayers
sha256: "752039d6aa752597c98ec212e9759519061759e402e7da59a511f39d43aa07d2"
url: "https://pub.dev"
source: hosted
version: "6.0.0"
audioplayers_android:
dependency: transitive
description:
name: audioplayers_android
sha256: de576b890befe27175c2f511ba8b742bec83765fa97c3ce4282bba46212f58e4
url: "https://pub.dev"
source: hosted
version: "5.0.0"
audioplayers_darwin:
dependency: transitive
description:
name: audioplayers_darwin
sha256: e507887f3ff18d8e5a10a668d7bedc28206b12e10b98347797257c6ae1019c3b
url: "https://pub.dev"
source: hosted
version: "6.0.0"
audioplayers_linux:
dependency: transitive
description:
name: audioplayers_linux
sha256: "3d3d244c90436115417f170426ce768856d8fe4dfc5ed66a049d2890acfa82f9"
url: "https://pub.dev"
source: hosted
version: "4.0.0"
audioplayers_platform_interface:
dependency: transitive
description:
name: audioplayers_platform_interface
sha256: "6834dd48dfb7bc6c2404998ebdd161f79cd3774a7e6779e1348d54a3bfdcfaa5"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
audioplayers_web:
dependency: transitive
description:
name: audioplayers_web
sha256: db8fc420dadf80da18e2286c18e746fb4c3b2c5adbf0c963299dde046828886d
url: "https://pub.dev"
source: hosted
version: "5.0.0"
audioplayers_windows:
dependency: transitive
description:
name: audioplayers_windows
sha256: "8605762dddba992138d476f6a0c3afd9df30ac5b96039929063eceed416795c2"
url: "https://pub.dev"
source: hosted
version: "4.0.0"
auto_screenshot:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -1249,6 +1305,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.3.1"
synchronized:
dependency: transitive
description:
name: synchronized
sha256: "539ef412b170d65ecdafd780f924e5be3f60032a1128df156adad6c5b373d558"
url: "https://pub.dev"
source: hosted
version: "3.1.0+1"
term_glyph:
dependency: transitive
description:
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ dependencies:

collection: ^1.0.0

audioplayers: ^6.0.0

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down Expand Up @@ -190,6 +192,7 @@ flutter:
- assets/icon/resized/icon-128x128.png
- assets/images/
- assets/google_fonts/
- assets/audio/

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "generated_plugin_registrant.h"

#include <audioplayers_windows/audioplayers_windows_plugin.h>
#include <dynamic_color/dynamic_color_plugin_c_api.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <irondash_engine_context/irondash_engine_context_plugin_c_api.h>
Expand All @@ -17,6 +18,8 @@
#include <window_manager/window_manager_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
AudioplayersWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin"));
DynamicColorPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DynamicColorPluginCApi"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
audioplayers_windows
dynamic_color
flutter_secure_storage_windows
irondash_engine_context
Expand Down

0 comments on commit 652747c

Please sign in to comment.