GithubHelp home page GithubHelp logo

Comments (4)

ar1st0crat avatar ar1st0crat commented on August 17, 2024

Can you provide some code? What WAV file are you trying to read? You can copy this file to PC and try opening it with NWaves using demo apps.
Also, I assume, you're trying to open (i.e. read, load) the file - not play it or record it: NWaves does not provide such methods, it's a DSP library.

from nwaves.

MAminnn avatar MAminnn commented on August 17, 2024

it's the second week which I'm trying to just record audio in xamarin which has an RIFF header, actully an audio which Nwaves can work with.
I tried Audio Recorder Plugin and Audio Record and Media Recorder . I asked many questions but got no answer.

the easiest way was Audio Recorder Plugin, but the output hasn't RIFF header.
the output of Media Recorder was .3gp which I couldn't convert it to .wav.
and the output of Media Recorder was .pcm which also couldn't convert to .wav

here is the last code I tried :

#region Properties
    int SAMPLING_RATE_IN_HZ = 44100;
    ChannelIn CHANNEL_CONFIG = ChannelIn.Mono;
    Android.Media.Encoding AUDIO_FORMAT = Android.Media.Encoding.Pcm16bit;
    int BUFFER_SIZE_FACTOR = 2;
    int BUFFER_SIZE;
    bool RecordingInProgress = false;
    private AudioRecord recorder = null;
    #endregion
    public void Record()
    {
        BUFFER_SIZE = AudioRecord.GetMinBufferSize(SAMPLING_RATE_IN_HZ,
        CHANNEL_CONFIG, AUDIO_FORMAT) * BUFFER_SIZE_FACTOR;
        recorder = new AudioRecord(AudioSource.Mic, SAMPLING_RATE_IN_HZ,
            CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE);

        recorder.StartRecording();
        RecordingInProgress = true;
        RecordingTask();
    }
    public Task RecordingTask()
    {
        return Task.Run(() =>
        {
            string path = "appdir/demo.pcm";
            MemoryStream buffer = new MemoryStream(BUFFER_SIZE);
            FileOutputStream outStream = new FileOutputStream(path);
            var demo2 = RecordingInProgress;
            while (RecordingInProgress)
            {
                int result = recorder.Read(buffer.GetBuffer(), 0, BUFFER_SIZE);
                if (result < 0)
                {
                    throw new Exception("Reading of audio buffer failed: ");
                }
                   outStream.Write(buffer.GetBuffer(), 0, BUFFER_SIZE);
            }
            
        });
    }
    public void Stop()
    {
        if (null == recorder)
        {
            return;
        }

        RecordingInProgress = false;

        recorder.Stop();

        recorder.Release();

        recorder = null;
    }

}

this code makes a .pcm file that can't convert to anything with even cloud converters.
I also tried this :

NWaves.Audio.WaveFile waveFile = new NWaves.Audio.WaveFile(buffer.GetBuffer());
                waveFile.SaveTo(new FileStream("appdir/demo.wav", FileMode.Create));

insted of outStream.Write(buffer.GetBuffer(), 0, BUFFER_SIZE); at the bottom of while closing block
but it says : No RIFF found

there is about 4 or 5 way to record audio. Nwaves can't work with any of them.

the last try I want to do is add RIFF header to the recorded audio buffer(bytes) programmatically or convert .3gp or .pcm to .wav programmatically.

summary: someone helps me to record an audio in xamarin which Nwaves can work with.
thanks

-- this is the last question I asked in stackoverflow. I also asked the same question at this link

actually I want to make a voice changer for android.
record audio and change it with Nwaves.
but the `Nwaves can't work with the recorded audios in android.

from nwaves.

ar1st0crat avatar ar1st0crat commented on August 17, 2024

OK, so you just need to read the contents of .pcm file.

WaveFile class represents the WAV container, i.e. the PCM data + WAV header. The latter is necessary for correct interpretation of the data in 'pcm' part. And since you know exactly the audio parameters during recording, just read the array of samples from the pcm file:

// mono mode and Pcm16bit (like in your example):

DiscreteSignal signal;

// sample.pcm is essentially just an array of bytes:
using (var streamIn = new FileStream("sample.pcm", FileMode.Open))
{
    var length = (int)streamIn.Length / 2;     // divide by 2, because each sample is represented with 2 bytes (Pcm16bit)

    signal = new DiscreteSignal(SAMPLING_RATE_IN_HZ, length);    // reserve space in the signal for number of samples: length

    using (var reader = new BinaryReader(streamIn))
    {
        for (var i = 0; i < length; i++)    // just read samples
        {
            signal[i] = reader.ReadInt16() / 32768f;
        }
    }
}

// then you can save it to WAV file, if necessary:

 var waveFile = new WaveFile(signal);

using (var stream = new FileStream("sample.wav", FileMode.Create))
{
    waveFile.SaveTo(stream);
}

from nwaves.

MAminnn avatar MAminnn commented on August 17, 2024

Thank you Sooooooooo Much ❤

from nwaves.

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.