Code Monkey home page Code Monkey logo

Comments (6)

themailman05 avatar themailman05 commented on May 25, 2024 1

from flutter_soloud.

alnitak avatar alnitak commented on May 25, 2024

Hi @themailman05,

I reviewed the code a bit because it's been a while since i wrote that.
You are right, the getCaptureAudioTexture2d is the only function exposed here. It takes the first 256 floats of the captured buffer samples and use them to calculate the FFT returning one 2D array where each row (512 floats) is represented by 256 floats of FFT data and 256 float with wave data.
So you can use just the 2nd 256 floats of the first row (which is the latest data). But as said before, the buffer is 1024. You will lose the other 768 floats.

It was planned to add a function to get all the buffer, but some testing are needed.
The way to proceed:

  1. the miniaudio callback fills and replaces data of the buffer every time is called
  2. the miniaudio callback must not do anything expensive
  3. write a C function to get the buffer and its Dart binding

The problem:
from Dart, after calling the get buffer method, you are supposed to do something with that data. Meanwhile the miniaudio callback might have been called many times and so you will lose those new buffer data.
I don't know how to solve this if it is a real problem, losing some data from the mic could't be a problem in most cases. My thoughts is to write a temp file or a buffer that decrease when requesting data (and increases when the miniaudio callback is called).

If you want to contribute in some way that would be great!! I'm terrible at writing docs, some guys told me they were scared reading it! :)

Anyway, In this week end I'll try to do something. Let me know.

Thanks

from flutter_soloud.

themailman05 avatar themailman05 commented on May 25, 2024

Tinkering with it a bit this weekend. I tried just refactoring the getAudioTexture2D but without printf statements showing up I am having a hard time debugging the C++ side. Any tips for getting debug printing to display? Would help a great deal.

from flutter_soloud.

alnitak avatar alnitak commented on May 25, 2024

On Android the printf doesn't output on console as you noticed. It has its own log like __android_log_vprint(). I have written a common platform_log() in common.h which should work but I didn't tested well on other platforms.

If you are using VS Code you could also debug C/C++ code. I set up the run actions in .vscode/launch.json for linux and windows, but you should install C/C++ Extension Pack and C/C++ for Visual Studio Code by Microsoft.
To debug C/C++ code with Android Studio, you should open the android folder project and edit the run configuration to use native debug.

from flutter_soloud.

alnitak avatar alnitak commented on May 25, 2024

Some times ago I did already a circular buffer class. Feel free to use it

circular_buffer.h
#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

#include <iostream>
#include <vector>
#include <algorithm>

class CircularBuffer {
private:
    std::vector<float> buffer;
    size_t maxSize;
    size_t currentSize;
    size_t head;
    size_t tail;

public:
    explicit CircularBuffer(size_t size) : buffer(size), maxSize(size), currentSize(0), head(0), tail(0) {}

    int size() {return currentSize;}

    void push(const std::vector<float>& values) {
        size_t numValues = values.size();

        // Check if the buffer is full
        if (currentSize + numValues > maxSize) {
            size_t numToRemove = currentSize + numValues - maxSize;
            head = (head + numToRemove) % maxSize;
            currentSize -= numToRemove;
        }

        size_t numTail = std::min(numValues, maxSize - tail);

        // Copy values to the tail of the buffer
        std::copy(values.begin(), values.begin() + numTail, buffer.begin() + tail);

        // If there are remaining values, copy them to the head of the buffer
        if (numValues > numTail) {
            std::copy(values.begin() + numTail, values.end(), buffer.begin());
        }

        tail = (tail + numValues) % maxSize;
        currentSize = std::min(currentSize + numValues, maxSize);
    }

    std::vector<float> pop(size_t numValues) {
        numValues = std::min(numValues, currentSize);
        std::vector<float> poppedValues(numValues);

        size_t numTail = std::min(numValues, maxSize - head);

        // Copy values from the head of the buffer
        std::copy(buffer.begin() + head, buffer.begin() + head + numTail, poppedValues.begin());

        // If there are remaining values, copy them from the tail of the buffer
        if (numValues > numTail) {
            std::copy(buffer.begin(), buffer.begin() + numValues - numTail, poppedValues.begin() + numTail);
        }

        head = (head + numValues) % maxSize;
        currentSize -= numValues;

        return poppedValues;
    }

    void print() const {
        std::cout << "N: " << currentSize << " - ";
        for (size_t i = 0; i < currentSize; ++i) {
            size_t index = (head + i) % maxSize;
            std::cout << buffer[index] << " ";
        }
        std::cout << std::endl;
    }
};



#endif // CIRCULAR_BUFFER_H

from flutter_soloud.

themailman05 avatar themailman05 commented on May 25, 2024

Hello,

I have made some progress on audio capture to buffer, but having some trouble with formats.
As I understand it the void* pInput incoming to data_callback is an interleaved 2-channel float32 buffer.

I am using memcpy in the callback to write to a preallocated buffer.

I am now working on exposing the SoLoud Wav loadMem bindings as function calls as well.

I think actually I need to use SoLoud Wav loadRawWave for the float pcm buffer that is emitted from the capture callback. I am still learning miniaudio so please let me know if I am on the right track. I'm happy to contribute back what I build once I get the kinks worked out.

from flutter_soloud.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.