OffiTracker/docs/library-usage.md

92 lines
2.5 KiB
Markdown

# OffiTracker as a python library
The OffiTracker program is designed in a way that allows it to be integrated in other projects by importing it.
A reference design for this use case can be found in the `offiplayergui.py` file.
## Importing
The OffiTracker library can be imported by putting the line
```python
import offitracker
```
in the head of your python project. For this to work, you need to place the `offitracker.py` file in your projects directory as well as the `drums` folder.
## The stop signal
Before we start playing anything, it would be useful to know how to stop the playback again.
For that, OffiTracker has a `stop_signal` variable which we can change from outside.
Example:
```python
import offitracker as oftr
# Set stop signal to False to allow for playback
oftr.stop_signal = False
# Set stop signal to True to stop playback
oftr.stop_signal = True
```
## Playing a csv file
Playing a csv file in the OffiTracker format can be done using the `play_csv_file` function.
Example:
```python
import offitracker as oftr
# Set stop signal to False to allow for playback
oftr.stop_signal = False
# Start playing back a file
oftr.play_csv_file("example.csv")
```
## Playback position, threading
The `play_csv_file` function has an optional `playback_row_index` variable that stores the currently playing row. If the variable is not initialized, the function will print a status message to stdout. Initializing it will disable that message and instead store the value which is useful when writing more complex software around the library.
Example:
```python
import offitracker as oftr
import threading
import time
def playback_thread(csv_file_path):
# Set stop signal to False to allow for playback
oftr.stop_signal = False
# Initialise playback row index
oftr.playback_row_index = 0
# Start playing back the file
oftr.play_csv_file(csv_file_path)
# Example CSV file path
csv_file_path = "example.csv"
# Create a thread for playback
playback_thread = threading.Thread(target=playback_thread, args=(csv_file_path,))
try:
# Start the playback thread
playback_thread.start()
while not oftr.stop_signal:
# Read the current row index
current_row_index = oftr.playback_row_index
print(f"Current Row Index: {current_row_index}")
time.sleep(1) # Adjust the sleep duration as needed
except KeyboardInterrupt:
# Set stop signal to True to stop playback when Ctrl+C is pressed
oftr.stop_signal = True
playback_thread.join() # Wait for the playback thread to finish
print("Playback stopped.")
```