Combining Audio Chunks Received via WebSockets into a Single Audio File in Django

I’m working on a Django project where I need to collect streaming audio chunks from the frontend via WebSockets, and then combine these chunks on the backend to create a single audio file. My current setup involves streamin audio on the frontend and sending it to the backend in small chunks. After creating audio file in backend I want to transcript audio file to text.

Welcome @BJoshi !

That sounds like an interesting project.

Thank you for reply but I need solution of this because i already search and do R&D about it But still i didn’t found any solution on it.

So what part of this do you have questions about? The more specific you are with identifying where you are stuck, the better chance we have of providing assistance.

    
    async def connect(self):
        await self.accept()
        self.model = whisper.load_model('base')
        # self.audio_chunk = []

    async def disconnect(self, close_code):
        await self.close()

    async def receive(self, text_data=None, bytes_data=None):
        if bytes_data:
            try:
                file_name = 'test.wav'
                audio = AudioSegment.from_file(io.BytesIO(bytes_data), format='webm')
                audio.export(file_name, format="wav")

                text = await speech_to_text(file_name)
                print(text,'text')
            except Exception as e:
                await self.send(text_data=json.dumps({
                    'error': f"Error: {str(e)}"
                }))

async def speech_to_text(file_name):

    recognizer = sr.Recognizer()
    
    with sr.AudioFile(file_name) as source:
        audio_data = recognizer.record(source)
    try:
        text = recognizer.recognize_google(audio_data)
        print(text,'text')
        return text
    except sr.UnknownValueError:
        return "Could not understand the audio"
    except sr.RequestError as e:
        return f"Could not request results from the speech recognition service; {e}"

Here is my consumers.py file

                    const stream = await navigator.mediaDevices.getUserMedia({audio:true});

                    if (!MediaRecorder.isTypeSupported('audio/webm')) {
                        alert('Browser not supported for MediaRecorder');
                        return;
                    }

                    const mediaRecorder = new MediaRecorder(stream, {
                        mimeType: 'audio/webm',
                    });

                    const socket = new WebSocket('ws://' + window.location.host + '/ws/chat/');

                    socket.onopen = () => {
                        document.querySelector('#status').textContent = 'Connected';
                        console.log({ event: 'onopen' });

                        mediaRecorder.addEventListener('dataavailable', async (event) => {
                            if (event.data.size > 0 && socket.readyState === 1) {
                                    socket.send(array);
                            }
                        });
                        mediaRecorder.start(1000);
                    };

Here is my javascript code in which i get audio and send it via websocket

Please do not post images of code here. Copy/paste the code into the body of your post.

When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Please delete these images and post the code you would like us to examine.

Also, don’t just post the code. Be specific and identify what the issue or problem is that you would like assistance with.

ohk thank you for information

In this code i want to create audio file of bytes_data i send throw javascript and transcript this audio file in text.
In short term I want to transcribe real-time audio into text if you have any other option beside my code then provide me.
I don’t want to use any paid services.

Sorry, I am not familiar with any such software.

ohk, have you seen my code ?

Need to improve code ?

Use ffmpeg
Its good and i was also facing same issue