Meyda feature extracted dat is different in command line and through nodejs - javascript
I tried to extract feature of an audio file using Meyda. But feature exytracted values are different when extracted using command line meyda and through javascript file
Command line
meyda 1600153162.63571.wav mfcc
Result
0.057628476337413304,0.025103534166687494,-0.0033402018588145435,-0.0050914619032203364,-0.005969043107840086,-0.009452339628697326,-0.0077781823184730455,-0.0024033016732702066,-0.0033368248853514206,-0.0012372591871309979,0.00003587458387239886,0.00293735474906295,0.0033156231532707036
1.4306079393227265,1.3713938566968262,1.2306487569743767,1.0515807610571597,0.8414768385049735,0.6060415036630535,0.37326507523088365,0.1654296077929904,-0.015392187854876595,-0.1652509593657213,-0.27571446879975764,-0.340564139593766,-0.36505384389267703
9.282664122642018,6.880236029290547,3.3870794551074606,1.7170683624244123,-0.29657914336587826,-3.18544774959499,-4.433789386396694,-3.7101490665746217,-2.816738558429225,-2.2736417577176273,-1.6636438686466657,-0.8537746809738949,-0.5116463089021152
25.65632094501052,22.93621824737729,16.08611763344924,7.490656903754756,-0.4358888007314062,-5.641656791337735,-7.229285832273481,-5.635856022395436,-2.1681330598896817,1.5575050036371694,4.296381195632149,5.45750656573637,5.059966218569768
23.469876801827922,20.984500104582974,15.80549272827184,10.064116650359527,3.957435073588796,-1.75506262168123,-5.904087102697238,-8.29320895086182,-9.037630914839253,-8.230609293960022,-6.604159793447832,-4.8505887091085365,-3.2390627275186175
29.14206006610766,25.50517483703385,19.279159148659456,13.806566615476791,7.715092034538,2.2364700767576764,-0.8129542016331001,-2.16772108652068,-2.6257181917727093,-2.1347711722007845,-1.0689231846996041,-0.6059541306434987,-0.9955650805848651
12.251538716256618,9.479093353756676,5.26726325506769,2.7893618666871762,0.10288164291841367,-3.121859159330943,-4.446188894370852,-3.5959072906303837,-2.5699522942015007,-1.8300687753240275,-0.7140618177046422,0.3644613187160053,0.8353185065087604
18.42772721964866,15.990459244575096,11.993313228734504,9.088259023158539,6.172351883886699,2.6632784721745213,-0.2683186290158647,-1.948275236251193,-2.793271841187546,-3.305172382572076,-3.6707865100252994,-3.638999985131219,-2.863263822827715
15.915610973257571,13.737073178518493,9.801934620786648,5.779403187264773,1.358952752885622,-2.5086411759816647,-5.187471901991046,-7.0034704192058985,-7.837140523422115,-7.437282553994893,-6.058991955183858,-4.360246975428363,-2.680629805396066
15.75090683856979,13.497089898444463,9.489154494388005,5.578283842168272,1.4567586737610156,-2.141429668615224,-4.380728646793593,-5.364310654901947,-5.049848052960211,-4.214979808517224,-3.9745754014376224,-4.076571890925421,-4.020714303609834
23.361729244701564,20.83103985201436,16.592090051349242,12.885538444506535,8.373777248668826,2.9768227085972145,-1.399644048138661,-4.090450595665753,-5.574775136428788,-5.787457864519816,-4.693018758118676,-3.100993535744204,-1.5625546377399475
12.793707716744393,10.197116076581045,5.819041457288568,2.4679725003955064,-0.33041998215142393,-2.607817847380143,-4.086120143731154,-4.779218992884254,-4.374465394096245,-3.118504640745296,-1.9666020102081847,-1.5140789377241373,-1.5130647010950313
3.2732387410942465,2.2453286293698977,0.9749483649128511,0.472715919767034,0.11249067642678866,-0.19927487640318992,-0.24286230087509772,-0.3645480814147444,-0.5060378572115204,-0.4793996411313405,-0.45934442588699886,-0.4680761055711004,-0.2967506177492105
Through javscript file
var express = require('express');
var app = express();
const fs = require('fs');
var Meyda = require('meyda');
var load = require('audio-loader')
let filename = "1600153162.63571.wav"
load(filename).then(function (buffer) {
const channelData = buffer.getChannelData(0)
const PaddingLength = (Math.pow(2,Math.round(Math.log2(channelData.length)+1)) - channelData.length)
let halfPaddingLength = parseInt(PaddingLength/2)
const pad1 = new Array(halfPaddingLength).fill(0);
const pad2 = new Array(PaddingLength - halfPaddingLength).fill(0);
let finalBbuffer = [...pad1,...channelData,...pad2]
console.log(finalBbuffer.length)
let mfccData = Meyda.extract('mfcc', finalBbuffer)
console.log("mfccData : ",mfccData);
});
Result :
[
249.11783051490784,
-90.61751411189829,
-12.253094024524968,
19.88245460444982,
-11.661965456271869,
-14.795375019626466,
7.19298966815922,
8.362884489124907,
3.9941283332736557,
-3.2158388656478287,
-2.0395393071161063,
-0.48849176751482837,
-3.6199623273626695
]
Why this difference . How to make them same ?
They're different because you're padding the buffer in your code, whereas the command line tool is splitting your file into multiple buffers with the default buffer size and returning MFCCs for each segment.
To make them the same, you could replicate the buffer chunking that the CLI performs in your code, rather than zero-padding the whole buffer, or you could make a CLI that zero pads the input. If you do the latter, we would certainly be interested in adding that functionality to the bundled CLI, so feel free to open an issue on our issue tracker to discuss if you go that route and wish to contribute.
Thanks for using Meyda!
Related
Node.js not only opening my code in a specific place [duplicate]
This question already has answers here: Proper way to reference files relative to application root in Node.JS (5 answers) Closed 1 year ago. My question is, is it possible to do so that when I have a code and a text file, but when I want to open or locate the text file, I dont need to specify it everytime where it is or like when I send it to someone else, they dont need to change the let file = C:\Users\etc... My code: const { readFile, readFileSync } = require('fs'); let file = 'C:\\Users\\eeroj\\Desktop\\word-counter\\TextFile2.txt'; function countRepeatedWords(sentence) { const words = sentence.split(" ").filter(word => !!word); const wordMap = {}; for (let word of words) { const key = word.trim().toLowerCase(); const currentWordCount = wordMap[key]; wordMap[key] = (currentWordCount ?? 0) + 1; } const sortedEntries = Object.entries(wordMap).sort(([a,], [b,]) => a.localeCompare(b)); const sortedWordMap = Object.fromEntries(sortedEntries); //returning the wordMap and the sorting of the wordMap return sortedWordMap return wordMap; } words = readFileSync(file).toString(); console.log(countRepeatedWords(words));
Yes you can, through the path (built-in Nodejs) module and the __dirname constant. The __dirname environment variable will automatically get the current folder's path for you, and then you can use path.join to join the current directory with the filename you want to access. Something like this const path = require("path"); // Assuming that the current script file are inside the 'C:\\Users\\eeroj\\Desktop\\word-counter' folder const file = path.join(__dirname, "TextFile2.txt"); // file = 'C:\\Users\\eeroj\\Desktop\\word-counter\\TextFile2.txt' If you put "TextFile2.txt" in a relative position not in the current folder, you can also use path.join with the folder traversal syntax such as .. to go back a folder const path = require("path"); // Assuming that the current script file are inside the 'C:\\Users\\eeroj\\Desktop\\word-counter' folder const file = path.join(__dirname,"../../", "TextFile2.txt"); // file = "C:\\Users\\eeroj\\TextFile2.txt" As a result, this way should work regardless of anywhere you put the project, even on a different machine, as long as the project folder structure stays relatively the same and the "TextFile2.txt" file is in the right place relative to the script file.
If your txt file is in the same directory as the script you can simply use the relative path with a '.' as let file = "./TextFile2.txt" Just note that the textfile will have to always have the same name.
build bitwise OR string from array for SSL crypto constants in NodeJS
I have an app that uses a separate configuration file for options that a user might want to set on their own. Recently I was asked to include the ability to define TLS settings. I would like my configuration file to have an array variable that allows the user to input the desired crypto constants and have my app take that array variable and build a bitwise OR string for the server instance. The following is a simplified version of what I'm trying to do. In practice 'httpsOpts' will be in a separate file, but that's not relevant for the example. How would I go about achieved the desired result? const https = require('https'); const crypto = require('crypto'); const express = require('express'); const sslOpts = ['SSL_OP_NO_TLSv1','SSL_OP_NO_TLSv1_1']; let httpsOpts = {}; httpsOpts.secureOptions = ''; // build the following: crypto.SSL_OP_NO_TLSv1 | crypto.SSL_OP_NO_TLSv1_1 const app = express(); const webServer = https.createServer(httpsOpts, app);
So this is what I came up with encase anyone else has the same issue. I'm sure there are better answers since this involves the dreaded 'eval' but it's what I got for now. let _opts = ''; sslOpts.forEach((v,i) => { _opts+='crypto.constants.'+v; if (i < sslOpts.length-1) _opts+=' | '; }); httpsOpts.secureOptions = eval(_opts);
Is it possible to write text in the middle of a file with fs.createWriteStream ? (or in nodejs in general)
I'm trying to write in a text file, but not at the end like appendFile() do or by replacing the entiere content... I saw it was possible to chose where you want to start with start parameter of fs.createwritestream() -> https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options But there is no parameter to say where to stop writting, right ? So it remove all the end of my file after I wrote with this function. const fs = require('fs'); var logger = fs.createWriteStream('result.csv', { flags: 'r+', start: 20 //start to write at the 20th caracter }) logger.write('5258,525,98951,0,1\n') //example a new line to write Is there a way to specify where to stop writting in the file to have something like: .... data from begining .... 5258,525,98951,0,1 ... data till the end ...
I suspect you mean, "Is it possible to insert in the middle of the file." The answer to that is: No, it isn't. Instead, to insert, you have to: Determine how big what you're inserting is Copy the data at your insertion point to that many bytes later in the file Write your data Obviously when doing #2 you need to be sure that you're not overwriting data you haven't copied yet (either by reading it all into memory first or by working in blocks, from the end of the file toward the insertion point). (I've never looked for one, but there may be an npm module out there that does this for you...)
You could read/parse your file at first. Then apply the modifications and save the new file. Something like: const fs = require("fs"); const fileData = fs.readFileSync("result.csv", { encoding: "utf8" }); const fileDataArray = fileData.split("\n"); const newData = "5258,525,98951,0,1"; const index = 2; // after each row to insert your data fileDataArray.splice(index, 0, newData); // insert data into the array const newFileData = fileDataArray.join("\n"); // create the new file fs.writeFileSync("result.csv", newFileData, { encoding: "utf8" }); // save it
How do I set cryptoJS.sha256 output to binary in Postman pre-request script
I am trying to create an HMAC signature in Postman using a pre-request script. Without going too far into the details of implementation, I have confirmed that my means for generating the signature is messed up. I can see what the expected result should be with a proof of concept example but I’m missing something somewhere and cannot tell if it is in the conversion. I’ve read around from other questions on SO that binary is the default provided by cryptojs internally and that simply calling for the hash is the equivalent of asking for the digest with conversions completed for you. Here is the code I’m trying to run in postman and the working implementation code as shown in nodeJS. var CryptoJS = require("crypto-js"); const d = new Date(); const timestamp = d.getTime(); const postData = {}; postData.nonce = 100; //timestamp * 1000; //nanosecond postman.setEnvironmentVariable('nonce', postData.nonce); const secret = CryptoJS.enc.Base64.parse(pm.environment.get("apiSecret")); const path = pm.globals.get("balanceMethod"); const message = CryptoJS.SHA256( encodeURI(postData.nonce + postData)) ; // ... const hmacDigest = CryptoJS.HmacSHA512(path + message, secret); postman.setEnvironmentVariable('API-Signature', CryptoJS.enc.Base64.stringify(hmacDigest)); console.log(CryptoJS.enc.Base64.stringify(hmacDigest)); Does this apply to my situation in that I’d need to convert my sha256 message into a bytes array in order to work? Reference code for building implementation that does work with nodeJS: const getMessageSignature = (path, request, secret, nonce) => { const message = qs.stringify(request); const secret_buffer = new Buffer(secret, 'base64'); const hash = new crypto.createHash('sha256'); const hmac = new crypto.createHmac('sha512', secret_buffer); const hash_digest = hash.update(nonce + message).digest('binary'); const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64'); return hmac_digest; }; Same reference code for building implementation in python3: req['nonce'] = 100 #int(1000*time.time()) postdata = urllib.parse.urlencode(req) # Unicode-objects must be encoded before hashing encoded = (str(req['nonce']) + postdata).encode() message = urlpath.encode() + hashlib.sha256(encoded).digest() signature = hmac.new(base64.b64decode(self.secret), message, hashlib.sha512) sigdigest = base64.b64encode(signature.digest()) The only post data I'm sending is the Nonce at this time and I've purposely set it to 100 to be able to replicate the result to fix the generated signature. Seems close but not matching result. The python and nodeJS do match expected results and work properly.
Check out the answer in this thread. It helped me with the my problem and may be what is happening in your case also. All it is necessary is break the input of the HMAC into two parts.
How to access named pipes using JavaScript in Firefox add-on?
I'm trying to access a named pipe in a Firefox add-on. My code, based on solution 2 to this CodeProject question, is: var file = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); file.initWithPath("\\\\.\\pipe\\test"); var text = "Some text to be written"; var writer = Components.classes["#mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream); // Open file for read/write access, and create it if it does not exist. writer.init (file, 0x04 | 0x08, -1, 0); writer.write (text, text.length); writer.flush (); writer.close (); When I run this is Firefox Scratchpad, I get: /* Exception: Component returned failure code: 0x80520012 (NS_ERROR_FILE_NOT_FOUND) [nsIFileOutputStream.init] #6 */ Line 6 is the line where I call writer.init. I've played with passing different flags to writer.init, but no luck. I'm able to write to a normal file path with this code, just not the named pipe path. I've been searching for more information for most of a day. The only other relevant thing I've found is this Bugzilla bug that mentions the same problem, but it's dated 2009. Thank you in advance for any suggestions. Edit: Based on the replies I tried the following: var encoder = new TextEncoder(); var array = encoder.encode("This is some text"); var path = "\\\\.\\pipe\\test"; Task.spawn(function() { let pfh = yield OS.File.open(path, {write: true}); yield pfh.write(array); yield pfh.close(); }); which lets me write to a file (if I change the path accordingly) but does not seem to send anything to the pipe. Here is the (admittedly crude) code I'm using to read the pipe on the .NET side: let server = new NamedPipeServerStream ("\\\\.\\pipe\\test", PipeDirection.InOut) let reader = new StreamReader (server) do printfn "Listening..." server.WaitForConnection () printfn "Connected." while true do while reader.EndOfStream = false do reader.ReadLine () |> printfn "%s" And the code I threw together to verify that I could at least write to the pipe in .NET: let client = new NamedPipeClientStream ("\\\\.\\pipe\\test") do client.Connect () let writer = new StreamWriter (client) do writer.AutoFlush <- true // Wait for connection System.Threading.Thread.Sleep (1000) let message = "hello" do message |> writer.WriteLine
Try OS.File: let encoder = new TextEncoder(); // This encoder can be reused for several writes let array = encoder.encode("This is some text"); // Convert the text to an array let promise = OS.File.writeAtomic("\\\\.\\pipe\\test", array, // Write the array atomically to "file.txt", using as temporary {tmpPath: "file.txt.tmp"}); // buffer "file.txt.tmp".