songbird-sync
Sync engine — channel-based state routing, transforms, guards, and transports Thesongbird-sync crate provides the core synchronization infrastructure for Songbird, implementing a sophisticated channel-based architecture for routing state updates between the audio engine, UI frontends, collaboration systems, and external integrations.
Overview
The sync engine acts as the central nervous system of Songbird, coordinating state changes across all components through a unified channel-based messaging system. It ensures consistency, provides transformation capabilities, implements access controls, and enables real-time collaboration.Architecture
Core Components
Channels
The sync engine organizes functionality into logical channels, each handling a specific domain:mixer- Audio mixing, track routing, effects, sends/returnstransport- Playback control, tempo, looping, metronomeproject- Project lifecycle, file operations, templates, presetsai- AI-powered composition and chat functionalityplugin- Plugin management, parameter automation, presetsrecording- Audio/MIDI recording, input management, take lanesexport- Audio export, rendering, format conversionmeters- Real-time audio level monitoring and analysissettings- User preferences and application configurationnotifications- System notifications and alertscollab- Real-time collaboration and conflict resolutionengine- Audio engine configuration and control
- Command definitions - Available operations and their parameters
- Event definitions - State change notifications
- Action handlers - Command execution logic
- TypeScript bindings - Type-safe frontend integration
Guards
Guards implement access control and state validation:- Load guards - Prevent operations during project loading
- Drag guards - Handle UI interaction conflicts
- Collaboration guards - Manage concurrent user access
- Echo suppression - Prevent feedback loops in distributed systems
Transforms
Data transformation pipeline for format conversion:- JSON ↔ Binary - Efficient serialization for real-time transport
- Path mapping - Route updates to specific state locations
- Schema validation - Ensure data integrity
- Versioning - Handle compatibility across different client versions
Transports
Multiple transport mechanisms for different integration scenarios:- Native functions - Direct in-process communication
- WebSocket - Real-time web frontend integration
- Tauri IPC - Desktop application bridge
- Collaboration transport - Distributed state synchronization
Dispatch System
Unified command routing and execution:DispatchContext- Execution context with state accessdispatch_helpers- Common operations and state queries- Channel routing - Route commands to appropriate handlers
- Error handling - Consistent error reporting and recovery
Key Features
Real-time State Synchronization
- Immediate consistency - All clients see the same state
- Conflict resolution - Handle concurrent modifications gracefully
- Selective updates - Only transmit changed data
- Batch optimization - Group related changes for efficiency
TypeScript Integration
- Automatic type generation -
ts-rsgenerates TypeScript types from Rust - Type-safe channels - Frontend gets compile-time type checking
- Command validation - Parameters are validated at the boundary
- Event subscriptions - Strongly typed event handlers
Performance Optimization
- Batching and throttling - Reduce update frequency for high-frequency events
- Profiling integration - Track performance of sync operations
- Memory efficiency - Minimize allocations in hot paths
- Concurrent processing - Handle multiple channels simultaneously
Extensibility
- Plugin architecture - Add new channels without modifying core
- Custom transforms - Implement domain-specific data processing
- Transport abstraction - Support new communication protocols
- Guard composition - Combine multiple access control strategies
Usage
Basic Engine Setup
Command Dispatch
Event Subscription
TypeScript Integration
The sync engine automatically generates TypeScript types for frontend integration:Development
Adding a New Channel
-
Create channel module in
src/channels/: -
Define commands and events:
-
Implement command handlers:
-
Register in channel registry (
src/channels/mod.rs):
Running Tests
Dependencies
songbird-state- State management and persistencesongbird-engine- Audio engine integrationsongbird-plugins- Plugin system integrationserde- JSON serializationts-rs- TypeScript type generationchrono- Timestamp handling