This Chrome extension is a sample project demonstrating how to capture audio from a source within its side panel (currently an MP3 file) and makes it available as a virtual microphone input to the main web page. This allows web applications on the main page to use the audio originating from the side panel as if it were a standard microphone.
- Audio Piping: Relays audio from the Chrome side panel to the active tab.
- Virtual Microphone: Overrides
navigator.mediaDevices.getUserMediaandnavigator.mediaDevices.enumerateDeviceson the main page to present the side panel's audio as a selectable microphone source named "Side Panel Audio Output". - Fixed Audio Parameters: The virtual microphone provides audio at a fixed sample rate of 48000 Hz and a single (mono) channel, simplifying audio handling.
- MP3 Audio Source: Currently uses a
test-tone.mp3file as the audio source in the side panel, playing in a loop.
The extension consists of several components:
-
Side Panel (
sidepanel.html,sidepanel.js):- Loads and plays an audio source (e.g.,
test-tone.mp3). - Uses the Web Audio API to capture this audio.
- Connects to
background.jsvia a long-lived port (audioBridge) to stream the captured audio data.
- Loads and plays an audio source (e.g.,
-
Background Script (
background.js):- Manages the opening of the side panel.
- Listens for connections on the
audioBridgeport fromsidepanel.js. - Receives audio data chunks from the side panel and forwards them to the content script of the active tab.
-
Content Script (
content.js):- Injects
inject-script.jsinto the main web page. - Listens for audio data messages from
background.jsand posts them toinject-script.jsusingwindow.postMessage().
- Injects
-
Inject Script (
inject-script.js):- Runs in the context of the main web page.
- Overrides
navigator.mediaDevices.getUserMediaandnavigator.mediaDevices.enumerateDevices. - When
getUserMediais called for the virtual device, it creates anAudioContextwith fixed parameters (48kHz, mono) and anAudioBufferSourceNodethat is fed by a queue. - Listens for
AUDIO_DATA_FROM_BACKGROUNDmessages (forwarded bycontent.js) containing audio chunks from the side panel. - Buffers these chunks and plays them back through the
ScriptProcessorNode(or anAudioWorkletNodein future versions) that acts as the virtual microphone's audio stream.
-
Clone the Repository (if you haven't already):
git clone <your-repository-url> cd sidepanel-audio-bridge
-
Place Audio File:
- Ensure you have an MP3 file named
test-tone.mp3in the root directory of the extension (/home/jiangzhuo/Desktop/kizunaai/sidepanel-audio-bridge/test-tone.mp3).
- Ensure you have an MP3 file named
-
Load the Extension in Chrome:
- Open Chrome and navigate to
chrome://extensions. - Enable "Developer mode" (usually a toggle in the top right).
- Click on "Load unpacked".
- Select the directory where you cloned/downloaded the extension files (e.g.,
/home/jiangzhuo/Desktop/kizunaai/sidepanel-audio-bridge).
- Open Chrome and navigate to
- Navigate to any web page where you want to use the virtual microphone.
- Click the extension icon in the Chrome toolbar to open the side panel. The audio from
test-tone.mp3should start playing within the side panel's context. - On the main web page, if the site attempts to access microphone devices (e.g., via a "select microphone" dropdown or by calling
navigator.mediaDevices.getUserMedia), you should see "Side Panel Audio Output" as an option. - Selecting this virtual microphone will allow the main page to receive the audio originating from the side panel.
manifest.json: Defines the extension's properties, permissions, and components.sidepanel.html: The HTML structure for the side panel.sidepanel.js: Manages audio playback and capture within the side panel.background.js: Handles communication between the side panel and content scripts, and manages the side panel itself.content.js: Injects theinject-script.jsand acts as a message bridge for audio data.inject-script.js: Runs in the main page's context, overrides Web Audio API methods, and reconstructs the audio stream.test-tone.mp3: The audio file used as the source in the side panel (you need to provide this).
- The
inject-script.jscurrently uses a fixed sample rate of 48000 Hz and 1 channel (mono) for itsAudioContext. The audio data from the side panel is expected to be in this format or will be effectively resampled/processed by the browser's Web Audio API internals. - The use of
ScriptProcessorNodeininject-script.jsis for simplicity in this version. For more robust and performant audio processing, especially to avoid potential audio glitches under heavy load, future versions should consider migrating toAudioWorkletNode.