Skip to content

Commit

Permalink
fix seek value to 0...1
Browse files Browse the repository at this point in the history
  • Loading branch information
chanonly123 committed Jan 11, 2025
1 parent b182f0c commit 104874e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "flutter_app",
"cwd": "flutter_app",
"request": "launch",
"type": "dart",
},
{
"name": "flutter_app_android",
"cwd": "flutter_app",
Expand Down
33 changes: 20 additions & 13 deletions cpp_source/JuceMixPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ JuceMixPlayer::JuceMixPlayer() {
formatManager.registerBasicFormats();

player = new juce::AudioSourcePlayer();
juce::LinearInterpolator interpolator;
juce::WindowedSincInterpolator interpolator;

player->setSource(this);

Expand Down Expand Up @@ -69,23 +69,30 @@ void JuceMixPlayer::_pause(bool stop) {
}

void JuceMixPlayer::play() {
_play();
taskQueue.async([&]{
_play();
});
}

void JuceMixPlayer::pause() {
_pause(false);
taskQueue.async([&]{
_pause(false);
});
}

void JuceMixPlayer::stop() {
_pause(true);
taskQueue.async([&]{
_pause(true);
});
}

void JuceMixPlayer::seek(float value) {
taskQueue.async([&, value]{
float _value = std::min(1.0f, std::max(value, 0.0f));
taskQueue.async([&, _value]{
bool wasPlaying = _isPlaying;
_isPlaying = false;
_loadAudioBlock(getDuration() * value / blockDuration);
lastSampleIndex = playBuffer.getNumSamples() * value;
_loadAudioBlock(getDuration() * _value / blockDuration);
lastSampleIndex = playBuffer.getNumSamples() * _value;
if (wasPlaying) {
_isPlaying = true;
}
Expand All @@ -94,7 +101,7 @@ void JuceMixPlayer::seek(float value) {

void JuceMixPlayer::_onProgressNotify(float progress) {
if (onProgressCallback != nullptr)
onProgressCallback(this, progress);
onProgressCallback(this, std::min(progress, 1.0F));
}

void JuceMixPlayer::_onStateUpdateNotify(JuceMixPlayerState state) {
Expand Down Expand Up @@ -251,7 +258,10 @@ void JuceMixPlayer::getNextAudioBlock(const juce::AudioSourceChannelInfo &buffer
return;
}

if (lastSampleIndex >= playBuffer.getNumSamples()) {
float speedRatio = sampleRate/deviceSampleRate;
float readCount = (float)bufferToFill.numSamples * speedRatio;

if (lastSampleIndex + readCount > playBuffer.getNumSamples()) {
if (playBuffer.getNumSamples() == 0) {
_onStateUpdateNotify(IDLE);
} else {
Expand All @@ -262,9 +272,6 @@ void JuceMixPlayer::getNextAudioBlock(const juce::AudioSourceChannelInfo &buffer
return;
}

float speedRatio = sampleRate/deviceSampleRate;
float readCount = (float)bufferToFill.numSamples * speedRatio;

for (int i=0; i<playBuffer.getNumChannels(); i++) {
float* buffer = bufferToFill.buffer->getWritePointer(i, bufferToFill.startSample);
interpolator[i].process(speedRatio,
Expand All @@ -288,7 +295,7 @@ void JuceMixPlayer::releaseResources() {

// override
void JuceMixPlayer::timerCallback() {
if (_isPlaying) {
if (_isPlaying && playBuffer.getNumSamples() > 0) {
_onProgressNotify(lastSampleIndex / (float)playBuffer.getNumSamples());
}
}
Expand Down
16 changes: 16 additions & 0 deletions flutter_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ class PlayerPageState extends State<PlayerPage> {

player.setStateUpdateHandler((state) {
setState(() => this.state = state);
switch (state) {
case JuceMixPlayerState.PAUSED:
setState(() => isPlaying = false);
case JuceMixPlayerState.PLAYING:
setState(() => isPlaying = true);
case JuceMixPlayerState.IDLE:
// TODO: Handle this case.
case JuceMixPlayerState.READY:
// TODO: Handle this case.
case JuceMixPlayerState.STOPPED:
// TODO: Handle this case.
case JuceMixPlayerState.COMPLETED:
// TODO: Handle this case.
case JuceMixPlayerState.ERROR:
// TODO: Handle this case.
}
});

player.setProgressHandler((progress) {
Expand Down
4 changes: 4 additions & 0 deletions ios_app/ios_app.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
1826ACFE2CA7C05B0068C0F7 /* libc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 1826ACFD2CA7C05B0068C0F7 /* libc++.tbd */; };
1826AD032CA7C7DD0068C0F7 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1826AD022CA7C7DD0068C0F7 /* Foundation.framework */; };
1826AD052CA7D60C0068C0F7 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1826AD042CA7D60C0068C0F7 /* AVFoundation.framework */; };
1875145D2D3255E90086C3CF /* music.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 1875145C2D3255E90086C3CF /* music.mp3 */; };
1881FF6D2D24A3D2009CEEDA /* music_small.wav in Resources */ = {isa = PBXBuildFile; fileRef = 1881FF6C2D24A3D2009CEEDA /* music_small.wav */; };
1881FF6F2D24A514009CEEDA /* music_big.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 1881FF6E2D24A514009CEEDA /* music_big.mp3 */; };
18A29EBB2CBD12F000459DF0 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18A29EB82CBD12F000459DF0 /* ContentView.swift */; };
Expand Down Expand Up @@ -55,6 +56,7 @@
1826AD022CA7C7DD0068C0F7 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1826AD042CA7D60C0068C0F7 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
1854B1E02CEC914C005EA5F6 /* juce_lib.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = juce_lib.xcodeproj; path = ../juce_lib/Builds/iOS/juce_lib.xcodeproj; sourceTree = SOURCE_ROOT; };
1875145C2D3255E90086C3CF /* music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = music.mp3; sourceTree = "<group>"; };
1881FF6C2D24A3D2009CEEDA /* music_small.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = music_small.wav; sourceTree = "<group>"; };
1881FF6E2D24A514009CEEDA /* music_big.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = music_big.mp3; sourceTree = "<group>"; };
189627BF2CA6C27000364235 /* ios_app.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ios_app.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -145,6 +147,7 @@
18A29EC02CBD130000459DF0 /* AppDelegate.swift */,
1881FF6C2D24A3D2009CEEDA /* music_small.wav */,
1881FF6E2D24A514009CEEDA /* music_big.mp3 */,
1875145C2D3255E90086C3CF /* music.mp3 */,
18A29EB52CBD12F000459DF0 /* Preview Content */,
18A29EB62CBD12F000459DF0 /* Assets.xcassets */,
18A29EB72CBD12F000459DF0 /* bridging.h */,
Expand Down Expand Up @@ -232,6 +235,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1875145D2D3255E90086C3CF /* music.mp3 in Resources */,
18A29EBD2CBD12F000459DF0 /* Preview Assets.xcassets in Resources */,
1881FF6F2D24A514009CEEDA /* music_big.mp3 in Resources */,
18A29EBE2CBD12F000459DF0 /* Assets.xcassets in Resources */,
Expand Down
3 changes: 2 additions & 1 deletion ios_app/ios_app/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ struct PlayerPage: View {
player.togglePlayPause()
}
Button("Set file") {
let path = Bundle.main.path(forResource: "music_big", ofType: "mp3")!
let path = Bundle.main.path(forResource: "music", ofType: "mp3")!
// let path = Bundle.main.path(forResource: "music_big", ofType: "mp3")!
// let path = Bundle.main.path(forResource: "music_small", ofType: "wav")!
player.setFile(path)
}
Expand Down
Binary file added ios_app/ios_app/music.mp3
Binary file not shown.
13 changes: 13 additions & 0 deletions stack_trace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

libname="juce_lib"
libDirectory=`realpath | find . -type d -path "*/$libname/*/RelWithDebInfo/*/obj/arm64-v8a"`

if [ ! -d "$libDirectory" ]; then
echo "🔴 build output binary directory not found!"
exit 1
else
echo "✅ build directory found [$libDirectory]"
fi

$NDK/ndk-stack -sym "$libDirectory" -dump "crash.log"

0 comments on commit 104874e

Please sign in to comment.