PianoMitra is a powerful, client-side application running entirely in your browser. This gives us low latency for audio, but it raises a fundamental question: **where do we store your music?** The answer is **Google Firebase**, specifically the **Firebase Realtime Database (RTDB)**. We chose RTDB because it is a NoSQL, cloud-hosted database that synchronizes data in real-time, which is essential for collaborative and instant social features.
Unlike traditional databases that require you to explicitly request data (a REST API call), RTDB works like a massive, constantly-updated JSON tree. When one user updates a piece of data (like saving a new track), all other users who are "listening" to that data instantly receive the update. This is the core technology that enables your shared tracks to light up on another user's keyboard as soon as they press play.
The Structure of Musical Data
We do not save audio files. We save **MIDI event data**, which is incredibly lightweight and efficient. This is the data that tells the engine *when* to press *which* key with *what* velocity, and for *how long*.
Track Data Structure :
Every recorded track in PianoMitra is saved as a specific JSON object under the user's ID. This object contains all the necessary instructions for the browser's synthesis engine to perfectly reconstruct the performance. A simplified track object looks like this:
{
"trackId": "userABC_1672345678",
"userId": "userABC",
"title": "My Chill Lofi Beat",
"tempo": 80,
"synthPreset": "WarmPadSynth",
"events": [
{ "time": 0.0, "note": 60, "velocity": 0.8, "duration": 0.5 },
{ "time": 0.5, "note": 64, "velocity": 0.7, "duration": 0.5 },
{ "time": 1.0, "note": 67, "velocity": 0.9, "duration": 0.5 }
],
"meta": { "score": 850, "verified": true, "likes": 42 }
}
This structure is critical for several reasons:
- **Instant Loading:** Loading this small JSON object is nearly instantaneous, even on slow mobile connections, compared to downloading a multi-megabyte MP3.
- **Replayability:** Because the data is sequence-based, the player's browser *regenerates* the music using their local synthesis engine. This is how we can show the keys light up during playback, offering an interactive listening experience.
- **AI Integration:** Mitra Bot can generate a `events` array directly from a text prompt, which is then immediately playable by the audio engine, seamlessly closing the loop between theory (text) and practice (sound).
Ensuring Security and Privacy with Rules
A JSON database with realtime capabilities requires stringent security. If any user could write or read data anywhere, the system would be chaos. Firebase uses **Security Rules** to manage access, which are written in a specialized JSON syntax. These rules are the backbone of our privacy commitment, as noted in the manifesto.
How Security Rules are Used (Over 300 words here):
We implement rules at the data path level. For instance, in a user's private storage path (`/users/{uid}/private_tracks`), the rules look something like this:
{
"rules": {
"users": {
"$uid": {
".read": "auth != null",
".write": "auth.uid === $uid && newData.events.length <= 10000"
}
}
}
}
This code snippet enforces crucial rules:
- **Authentication:** `.read`: Only logged-in users (`auth != null`) can access the `/users` data tree, ensuring data is not publicly visible without a login.
- **Data Ownership:** `.write`: A user can only write data to the node that matches their own User ID (`auth.uid === $uid`). This prevents User A from deleting or modifying User B's saved tracks.
- **Service Limits:** `newData.events.length <= 10000`: This enforces our strict data limits to keep the service fast and free. It prevents a user from uploading a single, excessively long sequence of notes that would inflate storage costs.
Realtime in Action: The Community Feed
The best example of RTDB in action is the Community Feed. When a user marks a private track as "Public," the client-side code moves a copy of the track's data to the `/public_feed` node. Every other user on the site has a listener attached to this node. The moment the track is moved:
- The `/public_feed` node is instantly updated.
- All connected users receive the new track data in milliseconds.
- The new track card appears on their Feed without requiring a page refresh.
This instant, event-driven data model is why the PianoMitra experience feels so immediate and responsive. Firebase abstracts away the complexity of managing server-side connections and websockets, allowing our development team to focus entirely on the quality of the synthesis engine and the front-end user experience. It truly bridges the gap between a standalone web app and a collaborative online platform.