Skip to content

Commit

Permalink
feat: add background music
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Dec 23, 2023
1 parent 4c67ee6 commit 9fc1c06
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 5 deletions.
45 changes: 45 additions & 0 deletions assets/audio/bgm/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
===============================
Abstraction - Music Loop Bundle
===============================

You can find this document in a slightly easier to read format at http://abstractionmusic.com/#secLicensing


**You may use this music in any media**
This encompasses pretty much anything including streaming, YouTube, game development, podcasts, and film. But please, contact me through Twitter (@ben_burnes) before you do; I want to know how the music is being used so I can share your stuff!


**You must legally acquire the music to use it**
When using a song or album, you must have acquired it through legal means. If you grabbed the music through Bandcamp, through many of the available digital outlets, or received it from my Patreon campaign (https://www.patreon.com/ben_burnes), you're good to go. Many albums I offer for free and there are plenty of free downloads on SoundCloud as well, and these are perfectly acceptable to use. However, ripping the song from Soundcloud or YouTube, or copying the files from a friend is not OK.


**Attribution is required**
If you are using a song, it is required to properly attribute it in your project's credits. When attributing music, list the song's title and give credit by name (Abstraction). Also, you must include a link to either the Bandcamp page (http://abstractionmusic.bandcamp.com/) or home page (http://www.abstractionmusic.com/).

I understand that much of this will rely on the honor system since I cannot monitor the entire Internet, but please keep in mind that everyone works hard at their craft and recognition like this helps more than you think.
Does your project intend to make money?

If your project intends on making any sort of income (sales, ad revenue, microtransactions, etc), please support my Patreon (https://www.patreon.com/ben_burnes) campaign. I feel that it is fair to kick back at least $1 of your monthly income my way. I prefer this method because managing project income and royalty payouts can be be a real pain. Subscribing to my Patreon will help support the current and future library of work as well as give you plenty of new music to use guilt-free in future projects!


**You cannot redistribute the music in its raw form**
What I mean by this is you cannot just broadcast the music without adding your own content. Added content could be considered gameplay on Twitch, or a podcast, or in your video game or movie. You can't just upload the MP3 files to YouTube or stream the music with no additional content. You also cannot claim the music as your own, or sell the music in compilations or bundles.


**If you are unsure, contact me**
I am an easy-going guy and would much rather help someone succeed than take an independent artist's grocery money. If you have questions about something, please get in touch (@ben_burnes on Twitter, or [email protected]) I'd be happy to help.

-----

Here are a few other places on the Internet where you can find me:

Website: abstractionmusic.com
Bandcamp: abstractionmusic.bandcamp.com
Patreon: patreon.com/ben_burnes
YouTube: youtube.com/ben_burnes
Soundcloud: soundcloud.com/abstraction

You can get in contact with me directly through Twitter @ben_burnes, I love to chat with new people!

<3
- Benjamin
Binary file added assets/audio/bgm/Ludum_Dare_32_Track_4.ogg
Binary file not shown.
34 changes: 34 additions & 0 deletions assets/audio/bgm/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Ricochlime notes:

1. Converted from wav to ogg with this command: `oggenc -a Abstraction -t "Ludum Dare 32 - Track 4" -q 6 Ludum_Dare_32_Track_4.wav`. This brought the filesize down from 5.6MB to 0.9MB.

---

===============================
Abstraction - Music Loop Bundle
===============================

This ZIP package is the full album download from Itch.io. It contains seamless audio loops ideal for video games and other media.

==== PLEASE NOTE ====
MP3 files will often NOT loop seamlessly. You will notice a very short gap when the song loops. This is due to the nature of the MP3 file format and is often difficult to overcome. I highly suggest using these songs as WAV files or converting to OGG if file size is an issue.
=====================

If you paid to download this album from Itch.io: Thank you very much! I'm extremely happy that this music moved you in some way. There is so much music out there competing for people's attention it is an honor that you decided this is worthwhile enough to pay for. Financial support is an incredible motivator to continue writing music and releasing more music in the future. I wish you the best of luck using these songs inside your games and other media! <3

If you somehow got this album outside of Itch.io: Hi! My name is Ben and I write music. If you like what you hear here, I have more music which can be found through the links below. If you dig what I create, I'd love it if you took a minute and let your friends know over Facebook and Twitter. Not many things are more motivating than a solid recommendation from a friend.

-----

Here are a few other places on the Internet where you can find me:

Website: abstractionmusic.com
Bandcamp: abstractionmusic.bandcamp.com
Patreon: patreon.com/ben_burnes
YouTube: youtube.com/ben_burnes
Soundcloud: soundcloud.com/abstraction

You can get in contact with me directly through Twitter @ben_burnes, I love to chat with new people!

<3
- Benjamin
71 changes: 70 additions & 1 deletion lib/flame/ricochlime_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'dart:math';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flame_forge2d/flame_forge2d.dart' hide Timer;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
// ignore: implementation_imports
Expand Down Expand Up @@ -104,6 +105,8 @@ class RicochlimeGame extends Forge2DGame
assert(size.x == expectedWidth);
assert(size.y == expectedHeight);

_initBgMusic();

background = Background();
add(background);

Expand All @@ -127,6 +130,72 @@ class RicochlimeGame extends Forge2DGame
await importFromGame(Prefs.currentGame.value);
}

/// Whether [_initBgMusic] has been run.
bool bgMusicInitialized = false;

/// Handles fading in/out the background music.
Timer? _bgMusicFadeTimer;

/// Initializes the background music,
/// and starts playing it.
void _initBgMusic() {
if (kDebugMode) print('Initializing bg music');
FlameAudio.bgm.initialize();
FlameAudio.bgm.play('bgm/Ludum_Dare_32_Track_4.ogg');
bgMusicInitialized = true;
}

void pauseBgMusic() {
if (kDebugMode) print('Fading out bg music');
_bgMusicFadeTimer?.cancel();
_bgMusicFadeTimer = _fadeBgmInOut(
startingVolume: FlameAudio.bgm.audioPlayer.volume,
targetVolume: 0,
onFinished: FlameAudio.bgm.pause,
);
}

void resumeBgMusic() {
if (kDebugMode) print('Fading in bg music');
_bgMusicFadeTimer?.cancel();
_bgMusicFadeTimer = _fadeBgmInOut(
startingVolume: FlameAudio.bgm.audioPlayer.volume,
targetVolume: 1,
onFinished: null,
);
}

Timer _fadeBgmInOut({
required double startingVolume,
required double targetVolume,
required void Function()? onFinished,
int steps = 10,
int totalMs = 1000,
}) {
if (!FlameAudio.bgm.isPlaying) {
FlameAudio.bgm.resume();
}
return Timer.periodic(
Duration(milliseconds: totalMs ~/ steps),
(_) {
final newVolume = FlameAudio.bgm.audioPlayer.volume +
(targetVolume - startingVolume) / steps;
final finished = targetVolume > startingVolume
? newVolume >= targetVolume
: newVolume <= targetVolume;
if (finished) {
_bgMusicFadeTimer?.cancel();
_bgMusicFadeTimer = null;
FlameAudio.bgm.audioPlayer.setVolume(targetVolume);
onFinished?.call();
if (kDebugMode) print('Finished fading bgm in/out to $targetVolume');
} else {
FlameAudio.bgm.audioPlayer.setVolume(newVolume);
}
},
);
}

Future<void> importFromGame(GameData? data) async {
if (data == null) {
// new game
Expand Down
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ void _addLicenses() {
['_gfx'],
await rootBundle.loadString('assets/images/LICENSE.md'),
);
yield LicenseEntryWithLineBreaks(
['_ludum_dare_32_track_4'],
await rootBundle.loadString('assets/audio/bgm/LICENSE.txt'),
);
yield LicenseEntryWithLineBreaks(
['google_fonts'],
await rootBundle
Expand Down
9 changes: 7 additions & 2 deletions lib/pages/play.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class _PlayPageState extends State<PlayPage> {
void initState() {
super.initState();
game.showGameOverDialog = showGameOverDialog;
if (game.bgMusicInitialized) {
game.resumeBgMusic();
}
if (game.state == GameState.gameOver) {
WidgetsBinding.instance.addPostFrameCallback(
(_) => game.gameOver(),
Expand All @@ -47,8 +50,10 @@ class _PlayPageState extends State<PlayPage> {

@override
void dispose() {
game.showGameOverDialog = null;
game.cancelCurrentTurn();
game
..showGameOverDialog = null
..cancelCurrentTurn()
..pauseBgMusic();
super.dispose();
}

Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ flutter:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- assets/images/
- assets/tutorial/
- assets/icon/icon.png
- assets/images/
- assets/google_fonts/Atkinson_Hyperlegible/
- assets/google_fonts/Silkscreen/
- assets/audio/bgm/
- assets/tutorial/

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
Expand Down

0 comments on commit 9fc1c06

Please sign in to comment.