What do fragment and width mean in audioop.rms(fragment, width) - python-3.x

I am trying to analyze a wav file in python and get the rms value from the wav. I am using audioop.rms to get the value from the wav. I went to do this and I did not know what fragment and width stood for. I am new to audioop and hope somebody can explain this. I am also wondering if there is any better way to do this in python.
Update: I have done some research and I found out fragment stood for the wav file. I still need to figure out what width means.

A fragment is just a chunk of data. Width is the size in bytes that the data is organized. ex 8bits data has width 1, 16bits has 2 and so on.
```
import alsaaudio, audioop
self.input = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
self.input.setchannels(1)
self.input.setrate(8000)
self.input.setformat(alsaaudio.PCM_FORMAT_S16_LE)
self.input.setperiodsize(300)
length, data = self.input.read()
avg_i = audioop.avg(data,2)
```
In the example i am setting alsa capture card to use a S16_LE signed 16bits Little Endian, so I have to set width to be 2. For the fragment is just the data captured by alsa. In your case the wav file is your data.

Related

Using lame from within SoxSharp

I've always used this command line to create an mp3 with Bit rate: 32kBit / s and Sample rate: 22050 Hz:
"lame -b 32 --resample 22050 input.wav output.mp3"
Now I wanted to use SoxSharp for that, and it has an mp3 option and uses libmp3lame.dll, so I guess it should work.
However, I'm unable to figure the right parameters.
The available parameters for the mp3 output are listed below.
Using nSox As Sox = New Sox("d:\dev\projects\sox-14-4-0\sox.exe")
nSox.Output.Type = FileType.MP3
nSox.Output.SampleRate = I guess that would be 22050 in my case?
nSox.Output.Channels = 1 'yep, I want mono
nSox.Output.Encoding = // not sure what to make of it
nSox.Output.SampleSize = // not sure what to make of it
nSox.Output.ByteOrder = // I guess I shouldn't touch that
nSox.Output.ReverseBits = // I guess I shouldn't touch that
nSox.Output.Compression = // absolutely not sure what I should choose here
nSox.Process("input.wav", "output.mp3")
End Using
Does anybody see where I should insert my "32"? And is .SampleRate = 22050 correct in my case?? The Windows file property dialogue doesn't give me any real hints if I do it correctly, and Audacity converts the audio to the format of my project.
Thank you very much for the help!
Investigating on the source code of SoxSharp, it can't even handle the most basic lame commands out of the box. Basically everything has to be put in the "CustomArguments" property.

Adding watermark to video

I am able to use the moviepy library to add a watermark to a section of video. However when I do this it is taking the watermarked segment, and creating a new file with it. I am trying to figure out if it is possible to simply splice in the edited part back into the original video, as moviepy is EXTREMELY slow writing to the disk, so the smaller the segment the better.
I was thinking maybe using shutil?
video = mp.VideoFileClip("C:\\Users\\admin\\Desktop\\Test\\demovideo.mp4").subclip(10,20)
logo = (mp.ImageClip("C:\\Users\\admin\\Desktop\\Watermark\\watermarkpic.png")
.set_duration(20)
.resize(height=20) # if you need to resize...
.margin(right=8, bottom=8, opacity=0) # (optional) logo-border padding
.set_pos(("right","bottom")))
final = mp.CompositeVideoClip([video, logo])
final.write_videofile("C:\\Users\\admin\\Desktop\\output\\demovideo(watermarked).mp4", audio = True, progress_bar = False)
Is there a way to copy the 10 second watermarked snippet back into the original video file? Or is there another library that allows me to do this?
What is slow in your use case is the fact that Moviepy needs to decode and reencode each frame of the movie. If you want speed, I believe there are ways to ask FFMPEG to copy video segments without rencoding.
So you could use ffmpeg to cut the video into 3 subclips (before.mp4/fragment.mp4/after.mp4), only process fragment.mp4, then reconcatenate all clips together with ffmpeg.
The cutting into 3 clips using ffmpeg can be done from moviepy:
https://github.com/Zulko/moviepy/blob/master/moviepy/video/io/ffmpeg_tools.py#L27
However for concatenating everything together you may need to call ffmpeg directly.

How to create 10bit YUY2 packed YUV Renderer?

I have created 8 bit yuv player for format YUY2 packed using SDL lib,some part of code:
handle->texture = SDL_CreateTexture(handle->renderer, SDL_PIXELFORMAT_YUY2, SDL_TEXTUREACCESS_STREAMING, width, height);
SDL_UpdateTexture(handle->texture, NULL,pDisplay->Ydata,(handle->width*2));
in that while creating texture,pixel format is given SDL_PIXELFORMAT_YUY2 and update texture pitch in twice of width. So it is playing fine.
But when it comes to 10 bit YUV, it plays disturbed and greenish video.
What I have tried is changed pitch to (handle->width*2 * 2) but no success
also someone suggested to convert 10bit value to 8bit but I don't want to do that.
Please help me to play 10bit YUY2 packed format YUV.
Is SDL support more than 8 bit depth pixel rendering ?

How to detect if a file is encoded using mp3PRO?

I have a folder which contains lot of MP3 files, some of them are encoded using mp3PRO.
Since this format is now obsolete, I'd like to convert them back to MP3 (converters can be found easily).
Is there is a way to detect programatically if a file is encoded using mp3PRO format ? (eg : by looking at file header or specific signatures using an hex editor)
The official player is able to detect if file is encoded using mp3PRO (the logo is highlighted or not) so I suppose this is technically possible.
What I found so far is that bitrate of mp3PRO file appears to be pretty low (50% of non encoded file) : eg : a 128 kbps file will appears as 64kbps. However a 320 kbps file will appears as 160 kpbs (which are pretty common) so it cannot be used as a rule.
Here is what I found out and how I fixed it. I wrote in here in case somebody would need it :
MP3Pro files does not contains any special flag in the mp3 header that would help to recognize them.
They are technically very similar to usual mp3 files, except they are encoded half the bit and sample rate (eg : a 128kpbs 44100hz file will be encoded as a 64kps 22050hz file, resulting in mp3pro file being approx half the size of original file).
This has been made for compatibility, so default players can play them without any change.
They also contains some SBR data, which allow to synthetically rebuild the lost audio part (high frequencies) and to play them it was before the mp3 pro conversion.
Detecting the SBR data seems very hard if not impossible : it would require to decode the actual mp3 frames. Also there is no documentation to be found about mp3pro format.
What I did (which works but required some manual effort) : I added all files to be checked to playlist of an mp3 player (foobar 2000 in my case) then sorted the files on the sample rate column : most 22050 hz mp3 files were indeed mp3 pro files.
They were converted back to mp3 using winamp + the mp3pro plugin made for it, available here : http://www.wav-mp3.com/mp3pro-to-mp3.htm

visualize irregular data in vtk

I have an irregular data, x dimension - 384, y dimension - 256 and z dimension 64. Now these coordinates are stored in 3 separate binary files and i have a data file having a data value for these points. I want to know, how can i represent such data to be easily visualized in vtk.
Till now we were using AVS which has fld files, which can read such data easily. I dont know how to do it in vtk. Would appreciate any pointers in this direction.
My best answer would be write a small program that reads in the files and then fills a vtkImageData object and then save it using vtkMetaImageWriter or something?
vtkSmartPointer<vtkImageData> ImageData = vtkSmartPointer<vtkImageData>::New();
ImageData->SetDimensions(384,254,64);
ImageData->SetOrigin(0.0,0.0,0.0);
ImageData->SetSpacing(1.0,1.0,1.0);
ImageData->SetScalarTypeToDouble();
ImageData->AllocateScalars();
for(int i=0; i<z_dim-1; i++){
for(int j=0;j<y_dim-1;j++){
for(int k=0;k<x_dim-1;j++){
double pix= pixel from data file
double* pixel = static_cast<double*>(ImageData->GetScalarPointer(k,j,i));
pixel[0] = pix;
}
}
}
Maybe you can write a short program to convert the files to a VTK native format. They are straightforward to work with, and there are ASCII and binary flavors. They are described in this document: www.vtk.org/VTK/img/file-formats.pdf
You may find this helpful also: http://www.rug.nl/cit/hpcv/visualisation/VTK/avs2vtk/man.html - if you dig through the page, there are scripts there to convert AVS files to VTK formats, it may be a good start point.
Hope this helps,
Carlos-
You can use paraview to open all files, merge the points and visualize.
here is a example for load files
you can save the vtk file too like this example
here is a example for save the points

Resources