Skip to content

Commit

Permalink
add missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
LabyStudio committed Jul 14, 2023
1 parent 4f8f3bc commit e6d2330
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {
}
dependencies {
implementation 'com.github.LabyStudio:java-spotify-api:1.1.13:all'
implementation 'com.github.LabyStudio:java-spotify-api:+:all'
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package de.labystudio.spotifyapi.platform.windows.api.playback;

import de.labystudio.spotifyapi.platform.windows.api.WinProcess;

/**
* Accessor to read the duration, position and playing state from the Spotify process.
*
* @author LabyStudio
*/
public class MemoryPlaybackAccessor implements PlaybackAccessor {

private static final long MIN_TRACK_DURATION = 1000; // 1 second
private static final long MAX_TRACK_DURATION = 1000 * 60 * 10; // 10 minutes

private final WinProcess process;
private final PointerRegistry pointerRegistry;

private int length;
private int position;
private boolean isPlaying;

/**
* Creates a new instance of the PlaybackAccessor.
*
* @param process The Spotify process to read from.
* @param address The reference address of the playback section
*/
public MemoryPlaybackAccessor(WinProcess process, long address) {
this.process = process;

// Create pointer registry to calculate the absolute addresses using the relative offsets
this.pointerRegistry = new PointerRegistry(0x0CFF4498, address);
this.pointerRegistry.register("position", 0x0CFF4810);
this.pointerRegistry.register("length", 0x0CFF4820);
this.pointerRegistry.register("is_playing", 0x0CFF4850); // 1=true, 0=false

this.update();
}

/**
* Read the current length, position and playing state from the Spotify process.
*
* @return true if the new values are valid, false otherwise
*/
@Override
public boolean update() {
this.position = this.process.readInteger(this.pointerRegistry.getAddress("position"));
this.length = this.process.readInteger(this.pointerRegistry.getAddress("length"));
this.isPlaying = this.process.readBoolean(this.pointerRegistry.getAddress("is_playing"));
return this.isValid();
}

/**
* Checks if the current values are valid.
* <p>
* To make sure that we have correct values from the memory address,
* we have to set some rules what kind of duration is correct.
* <p>
* The values are correct if:<br>
* - position {@literal <}= length<br>
* - length {@literal >} 0<br>
* - length {@literal <}= 10 minutes<br>
* - position {@literal >}= 1 second<br>
* - the parity bits are correct<br>
*
* @return true if the current values are valid, false otherwise
*/
@Override
public boolean isValid() {
return this.position <= this.length
&& this.position >= 0
&& this.length <= MAX_TRACK_DURATION
&& this.length >= MIN_TRACK_DURATION;
}

@Override
public int getLength() {
return this.length;
}

@Override
public int getPosition() {
return this.position;
}

@Override
public boolean isPlaying() {
return this.isPlaying;
}

}

0 comments on commit e6d2330

Please sign in to comment.