I need to extract the amplitudes of a wav file and want to do it as a simple commandline app. What's an easy way to do that? Cross-platform would be great. Needs to at least work on windows.
You can download an audio file for testing from soundcloud (or from https://dl.dropboxusercontent.com/u/313647/hosted/one_pos_to_neg_crossing.wav until soundcloud processes it).
Some libraries for potential use:
.NET
NAudio
Python
scipy.io.wavfile
pydub
PySoundFile
friture
snack
Here is a solution I came up with using Accord.NET:
public IEnumerable<float> AmplitudesFromFile(string fileName)
{
Accord.Audio.Formats.WaveDecoder sourceDecoder = new Accord.Audio.Formats.WaveDecoder(fileName);
Signal sourceSignal = sourceDecoder.Decode();
for (int i = 0; i < sourceSignal.Samples; i++) yield return sourceSignal.GetSample(1, i);
}
Project Page: http://accord-framework.net/
Documentation: http://accord-framework.net/docs/Index.html
Here's the first way I figured out how to do it with NAudio.
Output
>SoundToAmplitudes.exe w:\materials\audio\one_pos_to_neg_crossing.wav
0.04284668
-0.005615234
-0.1177368
Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;
namespace SoundToAmplitudes {
class Program {
private static int Main(string[] args) {
#if DEBUG
Console.WriteLine(String.Join(", ", args));
args = new[] { #"W:\materials\audio\one_pos_to_neg_crossing.wav" };
#endif
return Cli(args);
}
static int Cli(string[] args) {
string fileName = args[0];
var soundFile = new FileInfo(fileName);
foreach (float s in AmplitudesFromFile(soundFile)) {
Console.WriteLine(s);
}
//Console.WriteLine();
#if DEBUG
Console.Read();
#endif
return 0;
}
public static IEnumerable<float> AmplitudesFromFile(FileInfo soundFile) {
var reader = new AudioFileReader(soundFile.FullName);
int count = 4096; // arbitrary
float[] buffer = new float[count];
int offset = 0;
int numRead = 0;
while ((numRead = reader.Read(buffer, offset, count)) > 0) {
foreach (float amp in buffer.Take(numRead)) {
yield return amp;
}
}
}
}
}
Here's one way to do it in python (using scipy), although I assume that the wav file is 16-bit.
Output
W:\>python amplitudes.py w:\materials\audio\one_pos_to_neg_crossing.wav
0.0428479873043
-0.00561540574358
-0.117740409558
Code
"""Usage:
amplitudes WAV_FILE
Returns the (linear) amplitude of the signal inside the wav file, sample by sample.
"""
from __future__ import division
import docopt
import scipy.io.wavfile
MAX_WAV16_AMP = 32767 # = 2**15-1 # because wav16 is signed (wav8 isn't)
def main():
args = docopt.docopt(__doc__)
one_pos_to_neg_crossing_path = r"W:\materials\audio\one_pos_to_neg_crossing.wav"
if False: args['WAV_FILE'] = one_pos_to_neg_crossing_path
rate, amp_arr = scipy.io.wavfile.read(args['WAV_FILE'])
for amp in (amp_arr / MAX_WAV16_AMP):
print amp
if __name__ == '__main__':
main()
See also
Reading *.wav files in Python
Extracting an amplitude list from *.wav file for use in Python
Related
I am currently elaborating which content I should use in the different version numbers, so I read What are differences between AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion? and many other posts and articles.
Based on SemVer, I would increase the minor version number if I make backwards compatible changes. I understand and like this concept, and I want to use it.
This answer on the above linked post has good explanations on the different version numbers, but it also says that changing AssemblyVersion would require recompiling all dependent assemblies and executables.
I did a quick test:
testVersionLib.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>abc.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>3.4.5.6</AssemblyVersion>
</PropertyGroup>
</Project>
project testVersionLib: class1.cs
using System;
namespace testVersionLib
{
public class Class1
{
public string m_versionText = "3.4.5.6";
}
}
project testVersionExe: program.cs
using System;
using System.Diagnostics;
namespace testVersionExe
{
class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
PrintFileVersion ();
PrintAssemblyFullName ();
}
private static void PrintAssemblyFullName ()
{
Console.WriteLine ("m_versionText: " + new testVersionLib.Class1 ().m_versionText);
Console.WriteLine ("DLL Assembly FullName: " + System.Reflection.Assembly.GetAssembly (typeof (testVersionLib.Class1)).FullName);
}
private static void PrintFileVersion ()
{
Console.WriteLine ("DLL FileVersion: " + FileVersionInfo.GetVersionInfo ("testVersionLib.dll").FileVersion);
}
}
}
and found that this may apply to .Net Framework, but it obviously does not apply to .NET 5 (and most likely .NET 6 and above as well, and maybe previous versions of .NET Core): I created a .NET 5 C# console app with AssemblyVersion 1.2.3.4 and strong name. This EXE references a DLL with AssemblyVersion 3.4.5.6 and strong name. The DLL compiled with different versions and is placed in the EXE's folder without compiling that one again.
The results:
The EXE fails to start if the DLL version is below 3.4.5.6 (e.g. 3.4.5.5, 3.4.4.6, 3.3.5.6), which makes sense.
The EXE successfully runs if the DLL version is equal to or above the version that was used to created the app (equal: 3.4.5.6; above: 3.4.5.7, 3.4.6.6, 3.5.5.6, 4.4.5.6).
This answer only says that
[...] .Net 5+ does not (by default) require that the assembly version used at runtime match the assembly version used at build time.
but it does not explain why and how.
How are assemblies located, resolved and loaded in .NET 5?
If someone wants to repeat my test with the compiled files, here's the 7z archive, encoded as PNG:
To decode the image, save it as PNG and use this code:
static void Main (string[] args)
{
string dataPath = #"c:\temp\net5ver.7z";
string imagePath = #"c:\temp\net5ver.7z.png";
string decodedDataPath = #"c:\temp\net5ver.out.7z";
int imageWidth = 1024;
Encode (dataPath, imagePath, imageWidth);
Decode (imagePath, decodedDataPath);
}
public static void Decode (string i_imagePath, string i_dataPath)
{
var bitmap = new Bitmap (i_imagePath);
var bitmapData = bitmap.LockBits (new Rectangle (Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
byte[] dataLengthBytes = new byte[4];
Marshal.Copy (bitmapData.Scan0, dataLengthBytes, 0, 4);
int dataLength = BitConverter.ToInt32 (dataLengthBytes);
int imageWidth = bitmap.Width;
int dataLines = (int)Math.Ceiling (dataLength / (double)imageWidth);
if (bitmap.Height != dataLines + 1)
throw new Exception ();
byte[] row = new byte[imageWidth];
List<byte> data = new();
for (int copyIndex = 0; copyIndex < dataLines; copyIndex++)
{
int rowStartIndex = imageWidth * (copyIndex + 1);
Marshal.Copy (IntPtr.Add (bitmapData.Scan0, rowStartIndex), row, 0, row.Length);
data.AddRange (row.Take (dataLength - data.Count));
}
bitmap.UnlockBits (bitmapData);
System.IO.File.WriteAllBytes (i_dataPath, data.ToArray ());
}
public static void Encode (string i_dataPath,
string i_imagePath,
int i_imageWidth)
{
byte[] data = System.IO.File.ReadAllBytes (i_dataPath);
int dataLines = (int)Math.Ceiling (data.Length / (double)i_imageWidth);
int imageHeight = dataLines + 1;
var bitmap = new Bitmap (i_imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
var palette = bitmap.Palette;
for (int index = 0; index < byte.MaxValue; index++)
palette.Entries[index] = Color.FromArgb (index, index, index);
bitmap.Palette = palette;
var bitmapData = bitmap.LockBits (new Rectangle (Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);
Marshal.Copy (BitConverter.GetBytes (data.Length), 0, bitmapData.Scan0, 4);
for (int copyIndex = 0; copyIndex < dataLines; copyIndex++)
{
int dataStartIndex = i_imageWidth * copyIndex;
int rowStartIndex = i_imageWidth * (copyIndex + 1);
byte[] row = data.Skip (dataStartIndex).Take (i_imageWidth).ToArray ();
Marshal.Copy (row, 0, IntPtr.Add (bitmapData.Scan0, rowStartIndex), row.Length);
}
bitmap.UnlockBits (bitmapData);
bitmap.Save (i_imagePath);
}
i have it generate a 4 diget random number and save it into diffrent arrays then trying to let the user try and guess the number
Generate a random four digit number. The player has to keep inputting four digit numbers until they guess the randomly generated number. After each unsuccessful try it should say how many numbers they got correct, but not which position they got right. At the end of the game should congratulate the user and say how many tries it took.
this is the breif im trying to follow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace _4_Number_Guess_Game
{
class Program
{
static void Main(string[] args)
{
int[] RanNums = new int[4];
for (int i = 0; i < 4; i++)
{
Random ran = new Random();
RanNums[i] = ran.Next(1, 9);
Thread.Sleep(11);
}
int[] Input = new int[4];
bool guess = false;
while (guess == false)
{
Console.WriteLine("Input a 4 digit number: ");
Console.
Console.ReadLine();
}
Console.ReadLine();
}
}
}
I trying to capture fingerprint via my C# code. I have referenced the secugen FDx Pro SDK for windows version 2.3.3.0 and have installed the drivers. Though I can not see the sgfplib.dll among the other dll files.
when I run the code trying to initialize the SGFingerPrintManager which uses sgfplib.dll, it complains that "sgfplib.dll can not be found" I have read the previous suggestion on this platform, but none solved the problems.
Please I appreciate in advance any suggestion to solve this problem sonnest.
Thanks in advance.
a) I have downloaded FDx Pro SDK for windows and the secugen drivers version 6.7 (64).
b) Also, I tried to copying the sgfplid.dll to the bin folder of my project but problem persists.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LuggageTrackingAndBillingSystem.models;
using SecuGen.FDxSDKPro.Windows;
namespace LuggageTrackingAndBillingSystem
{
public partial class TsetForSecugen : Form
{
Int32 image_width = 200;
Int32 image_height = 300;
Int32 image_dpi = 500;
public TsetForSecugen()
{
InitializeComponent();
InitializeDedive();
}
private SGFingerPrintManager m_FPM;
SGFPMDeviceName device_name = SGFPMDeviceName.DEV_FDU03;
private void InitializeDedive()
{
//Step 1
if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully)
{
// This will load a CLR 2 mixed mode assembly
m_FPM = new SGFingerPrintManager();
}
var err = m_FPM.InitEx(image_width, image_height, image_dpi);
}
private void DrawImage(Byte[] imgData, PictureBox picBox)
{
int colorval;
Bitmap bmp = new Bitmap(image_width, image_height);
picBox.Image = (Image)bmp;
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
colorval = (int)imgData[(j * image_width) + i];
bmp.SetPixel(i, j, Color.FromArgb(colorval, colorval, colorval));
}
}
picBox.Refresh();
}
private void GetImage()
{
//Capturing Finger Image Step 3
Byte[] fp_image = new Byte[image_width * image_height];
Int32 iError;
iError = m_FPM.GetImage(fp_image);
if (iError == (Int32)SGFPMError.ERROR_NONE)
{
DrawImage(fp_image, pictureBox1);
}
}
private void TsetForSecugen_Load(object sender, EventArgs e)
{
InitializeDedive();
GetDevieInfo();
}
private void GetDevieInfo()
{
#region Det Device Info step 2
//Get Device info
SGFPMDeviceInfoParam pInfo = new SGFPMDeviceInfoParam();
pInfo = new SGFPMDeviceInfoParam();
Int32 iError = m_FPM.GetDeviceInfo(pInfo);
if (iError == (Int32)SGFPMError.ERROR_NONE)
{
image_width = pInfo.ImageWidth;
image_height = pInfo.ImageHeight;
}
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
GetImage();
}
}
}
Actual result:The expected result is to be able to capture fingerpring
Error Meaage: Header[FDx SDK Pro.NET] . Body[can't find sgfplib.dll]
I have solved the issue by gotten the executable file of FDx SDK Pro for windows and installed.
The problem is caused by the FDx SDK Pro For windows not being able to see the sgfplib.dll file which is automatically installed in windows/System32 and Windows/sysWOW64 directories during installation.
Thanks to all.
I am learning JUCE and I am writing a program that just reads the input from the audio card and plays it back. Obviously this is just for learning purposes. I am using the audio application template. This is the code inside the getNextAudioBlock() function:
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
if(true) // this is going to be replaced by checking the value of a button
{
const int channel = 0;
if(true) // this is going to be replaced too
{
const float* inBuffer = bufferToFill.buffer->getReadPointer(channel, bufferToFill.startSample);
float* outBuffer = bufferToFill.buffer->getWritePointer(channel, bufferToFill.startSample);
for(int sample = 0; sample < bufferToFill.numSamples; ++sample)
outBuffer[sample] = inBuffer[sample];
}
else
{
bufferToFill.buffer->clear(0, bufferToFill.startSample, bufferToFill.numSamples);
}
}
else
{
bufferToFill.buffer->clear(0, bufferToFill.startSample, bufferToFill.numSamples);
}
}
The code is really simple: the content from the input buffer is copied directly to the output buffer. However, I am not hearing anything. What am I doing wrong?
I've made a little app for printing Dymo labels and it works as intended, but when I run it on my Surface tablet it disables scalling af I've pushed the print button. If reopened the scalling is correct till print button is pushed again.
Can anybody tell me why?
The print button only runs my LabelPrinter class which has the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dymo;
using System.Windows.Forms;
namespace LabelPrinter
{
class LabelPrinter
{
private DymoAddInClass dymoAddIn;
private DymoLabelsClass dymoLabels;
private string labelPath, classRoom;
private int labelCount;
public LabelPrinter(string labelPath, string classRoom, int labelCount)
{
dymoAddIn = new DymoAddInClass();
dymoLabels = new DymoLabelsClass();
this.labelPath = labelPath;
this.classRoom = classRoom;
this.labelCount = labelCount;
}
public void printLabels()
{
dymoAddIn.SelectPrinter(dymoAddIn.GetDymoPrinters().Split('|')[0]);
if (dymoAddIn.Open(labelPath))
{
int firstNo = StaticMethods.getNextCodeEntryWithString(classRoom), lastNo = firstNo + labelCount;
dymoAddIn.StartPrintJob();
for (int i = firstNo; i < lastNo; i++)
{
foreach (string objName in dymoLabels.GetObjectNames(true).Split('|'))
{
dymoLabels.SetField(objName, classRoom + "_" + i);
}
dymoAddIn.Print(1, false);
}
dymoAddIn.EndPrintJob();
}
}
}
}