How I can merge two audio files - JavaScript?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How I Can Merge Two Audio Files using JavaScript and AJAX

When dealing with multi-source recordings, such as those captured via RecordRTC or the Web MediaRecorder API, a common requirement is to combine these separate streams into a single, cohesive file before uploading it to a server. This process involves more than just concatenating files; it requires understanding how browser APIs handle audio data and managing asynchronous operations.

This post will walk you through the conceptual steps and provide practical JavaScript examples for merging two audio files in the browser and subsequently uploading the result via AJAX. We will focus on leveraging modern Web APIs to handle the heavy lifting before sending the data to your backend, which often involves frameworks like Laravel.

The Challenge of Merging Audio Streams

Merging audio files is more complex than simply concatenating two text files because audio data is time-based and requires specific handling within the browser environment. We cannot just stitch raw byte streams together; we need to treat the audio as an AudioBuffer to manipulate its contents, ensuring correct sample rates and playback context.

The workflow generally looks like this:

  1. Record Audio A (Blob 1).
  2. Record Audio B (Blob 2).
  3. Load Blob 1 and Blob 2 into an AudioContext.
  4. Decode both Blobs into AudioBuffer objects.
  5. Concatenate the buffers in memory.
  6. Encode the combined buffer back into a single downloadable format (e.g., WebM or MP3).
  7. Prepare the final merged file for AJAX upload.

Step-by-Step JavaScript Implementation

Since directly merging raw stream data can be highly complex, we often rely on manipulating the underlying Blob objects and using the browser's built-in capabilities to handle the encoding/decoding process efficiently.

For simplicity in this example, we will focus on combining two existing recorded files (audio1.webm and audio2.webm) into a single new file before uploading.

1. Handling Blob Objects

First, you need to prepare your two audio files as File or Blob objects so they can be processed by the browser's media capabilities. The provided snippet demonstrates how to package a recorded blob for transmission:

mediaRecorder.stopRecording(function() {
    var blob = mediaRecorder.getBlob();
    var fileName = getFileName('webm'); // Assuming you have a helper function

    var fileObject = new File([blob], fileName, {
        type: 'audio/webm'
    });

    // This object is ready for FormData upload
    var formData = new FormData();
    formData.append('blob', fileObject);
    formData.append('filename', fileObject.name);

    // Proceed to AJAX call...
});

2. Merging the Audio Data (Conceptual Approach)

To truly merge two audio files in JavaScript, you would typically load them into an AudioContext and use an AudioBufferSourceNode to play them sequentially. To create a single merged file from scratch, the process involves:

  1. Loading: Use fetch to load both audio Blobs into memory.
  2. Decoding: Use audioContext.decodeAudioData() for each Blob to get two distinct AudioBuffer objects.
  3. Concatenation: This is the trickiest part. You would need a custom approach, often involving using an AudioWorklet or manipulating the raw PCM data if you want true bit-level merging, or simply concatenating the raw encoded streams if your recorder supports it directly. For many use cases, stitching two WebM/MP4 files together requires external libraries or server-side processing for perfect results.

A robust approach in modern web development involves using specialized libraries that abstract away the complexities of AudioContext manipulation. While you can perform basic concatenation, complex cross-format merging is often best handled by a dedicated utility function running on the client side before the upload step.

3. Uploading the Merged File via AJAX

Once you have your final merged file (let's assume you generated merged_audio.webm), you use standard FormData and XMLHttpRequest (or the modern fetch API) to send it to your server endpoint. This is where the backend framework, such as Laravel, excels at handling large file uploads securely.

// Assuming 'mergedFile' is the Blob of the newly merged audio
var formData = new FormData();
formData.append('audio_file', mergedFile); // Append the merged blob
formData.append('filename', 'merged_audio.webm');

$.ajax({
    url: '{{ url('/') }}/save-audio', // Laravel route handler
    data: formData,
    cache: false,
    contentType: false, // Crucial for FormData uploads
    processData: false, // Tells jQuery not to try and process the data
    type: 'POST',
    success: function(response) {
        console.log('Audio successfully uploaded:', response);
    },
    error: function(xhr, status, error) {
        console.error('Upload failed:', error);
    }
});

Conclusion and Backend Considerations

Merging audio files in JavaScript is feasible using the Web Audio API, but it demands careful management of AudioContext states and buffer decoding. For production applications, especially when dealing with large media files, relying solely on client-side merging for complex synchronization can introduce latency or errors.

The most robust pattern is to ensure that the front-end handles gathering the necessary data (the two source files) and then cleanly sends them to a powerful backend for final processing. Frameworks like Laravel provide excellent file storage and stream handling capabilities, making it ideal for receiving these FormData uploads and performing heavy media manipulation on the server side. Utilizing the power of your backend infrastructure ensures that complex merging operations are handled reliably, regardless of client-side performance constraints.