Giter VIP home page Giter VIP logo

Comments (13)

dmrschmidt avatar dmrschmidt commented on June 5, 2024

Hmm, that's an interesting problem. I guess there's a bunch of different approaches you could take that I can see. The two most reasonable are maybe:

  1. save the samples: when you save the draft, you could also persist a sufficient amount of the most recent samples that you're feeding to the WaveformLiveView. When you restore your state, you'd feed these back into it again and it will look exactly the same. Slight downside is that you'd have to store these separately of course.

  2. use WaveformAnalyzer: when you restore your draft, use WaveformAnalyzer to have the samples re-generated. Then feed those into the live view. You'd have to figure out the ideal amount of samples to generate from you draft file, to get the most similar looking waveform again.

If you look closely at how WhatsApp does it, you'll notice that they render the restored waveform differently from how it looked during recording. So they're probably doing something like 2).

from dswaveformimage.

AhmadDurrani579 avatar AhmadDurrani579 commented on June 5, 2024

@dmrschmidt you means like this
let waveformAnalyzer = WaveformAnalyzer(audioAssetURL: audioURL)
waveformAnalyzer?.samples(count: 10) { samples in
print("sampled down to 10, results are (samples ?? [])")
}
Just tell me one thing in this code what is 10 let suppose I have drafted 4 minuted audio and I want to apply the WaveFormAnalyzer as you suggest

Just let me know how much should I pass the sample count because of in 4 minuted drafted audio I don't know how much sample count it is

from dswaveformimage.

dmrschmidt avatar dmrschmidt commented on June 5, 2024

Well that depends on the rate how frequently you sample the mic input to feed it into the LiveView, the duration of the recording that has already been captured as a draft and to some extend the width of your view as well as the resolution of your screen.

Looking at WhatsApp's example, you should be fine if these are only rough approximations probably. If we look at the recorder from the example app, it gets updated roughly every 10ms. We then add the same sample 3 times (for no particular reason other than to speed up the animation).

So that means to restore this view, you will need roughly view.width * screen.scale * 3 = x samples of the draft audio, and that amount of samples needs to represent roughly 4 seconds from the draft audio. The 4s of course is also specific to your case, I just had a look how much time it roughly takes in the example app, your case may be entirely different!

So if your duration is y = 4m (= 240s), then the overall amount of samples you will need to generate would be y / 4s * x = # samples. And from that number you just take the last x samples and feed it into the live view. Done :)

from dswaveformimage.

AhmadDurrani579 avatar AhmadDurrani579 commented on June 5, 2024

@dmrschmidt sorry for the late reply can you please just let me know the formula for the calculation of the sample count
x = view.width * screen.scale * 3
y / 4s * x --- in this formula 4s means duration of the audio ?

from dswaveformimage.

dmrschmidt avatar dmrschmidt commented on June 5, 2024

Yes, 4s there is 4 seconds. As I had stated, that's just a rough estimate though and not a scientific number. It's "the amount of seconds of audio signal visible on screen at any given time". Which depends on physical screen dimension, ie on an iPad on full screen in landscape for instance you'd see way more of the audio signal than on an iPhone Mini in portrait mode.

from dswaveformimage.

AhmadDurrani579 avatar AhmadDurrani579 commented on June 5, 2024

@dmrschmidt Should I calculate the full sample count of the wave or only visible view sample count ?

from dswaveformimage.

dmrschmidt avatar dmrschmidt commented on June 5, 2024

If we’re assuming that you are using the full audio draft as it has been recorded, then you will need to calculate the samples for the full audio, yes. You would end up discarding most of it though for longer audio drafts of course as you are only actually rendering those ~4s on screen.

So it could be worth considering trimming the input audio from the draft before doing the rendering as another valid alternative approach / optimization.

from dswaveformimage.

AhmadDurrani579 avatar AhmadDurrani579 commented on June 5, 2024

@dmrschmidt this formula is not working as I want because of I have multiple audio file and I want to combine it and make it one so the sample count is generating in different form its not working as the what's app does I have use the WaveAnalyzer as you suggest but still not get the same result

from dswaveformimage.

dmrschmidt avatar dmrschmidt commented on June 5, 2024

If you look at WhatsApp, you'll notice that in their case the waveform during recording and the waveform after pausing and then resuming, also do not match exactly. Unless you are using AVAudioEngine directly and add a bytestream tap on your incoming audio stream, this also won't be possible pixel-perfect. That is due to the fact that AVAudioRecorder only gives exposes an optimized, pre-calculated "snapshot stream" and not the raw bytes coming through. Once you draw from the recording though, we have to work off the raw byte stream. Which is totally fine for a WhatsApp style use case, as the waveform is really just some extra visual guide to the user to see whether their audio is too loud or too quiet.

Compare this WhatsApp screenshot pre-pausing

IMG_9491

to this WhatsApp screenshot post-pausing

IMG_9492

So if you're not managing to get the exact same result, I wouldn't sweat on it, this may be the reason.

If you're not getting the right window size to look at in your samples, you'd need to adjust the right parameters for your specific use case. As I'd mentioned, the formula I posted is just to give you an idea of what you need to take into consideration, and not a hard formula. Depends on your use cases and platforms you're on / need / want to support. So I can't provide that for you here unfortunately.

from dswaveformimage.

AhmadDurrani579 avatar AhmadDurrani579 commented on June 5, 2024
         let x =  viewOfAudioWave.frame.width * UIScreen.main.scale * 3
         let sampleCount =  asset.duration / x 
          print("Print the Audi Drafted Duration \(sampleCount)")

@dmrschmidt In this formula the the sample count is always in . because of let suppose 240(4 mint audio) and if you divide this with the x so it will be in point like 0.2121 something like this so its not possible that this is the sample count

from dswaveformimage.

dmrschmidt avatar dmrschmidt commented on June 5, 2024

Yes, that's true. That formula is also different from the one I had share in my previous comment. So here is that formula in (pseudo-)code:

let x = view.width * screen.scale * 3 // sample count / pixels to be rendered on screen
let y = asset.duration // 240 seconds of total audio recorded so far
let samplesToRender = y / 4 * x // where 4 is the seconds that will actually be rendered on screen from those 240 total seconds

I haven't tested this, but this should give you the total number of samples required to pass into WaveformAnalyzer.samples(count:). You'd then pick the last x amount of samples of those.

from dswaveformimage.

AhmadDurrani579 avatar AhmadDurrani579 commented on June 5, 2024

@dmrschmidt I have apply the same formula as you suggest but the result is different let me upload the screen shots
First screen shot before the drafting of the audio
IMG_0044

And the second Screen is after drafting both are the different at least the sampling of the wave will be same

IMG_0045

The result is fine @dmrschmidt but I want the sampling will be same because of in first screen the audio sample is plane and in second screen the sampling is to high how we handle this

from dswaveformimage.

dmrschmidt avatar dmrschmidt commented on June 5, 2024

This I think is an example of what I mentioned before, there is different ways of downsampling amplitudes and different ways of then mapping these downsampled dB values to normalized values between [0,1]. See RMS as one of the steps potentially involved. These steps will be different from audio source to audio source. What the library could theoretically provide is different algorithms to choose from. In practice that won't be something I can add currently, so if this was needed, it would have to come from a PR.

Given that, what might be the easiest solution for you at this point, is to simply figure out through experimentation, how much you'll need to "squeeze" the samples from the 2nd image to look more like the first.

To be a little more concrete, you want to lower their amplitude. That might be as simple as multiplying each sample value by some constant factor < 1. That might be squaring each sample to better preserve peaks. So on the results you get from the WaveformAnalyzer, map them with some function to a new range of values more resembling what you'd like them to look like.

from dswaveformimage.

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.