How to modify a Transform stream in Node.js whose parameters are part of the source stream - node.js

Say I have a transform stream that is created with a single parameter - e.g. createTransform(a).
It expects to read from a stream, do its work, and output a transformed stream. But I have a situation where the parameter a is the first x bytes of the source stream. x is always fixed and known.
How can I create my own transform stream that expects a stream with a as the first x bytes?
i.e. original scenario:
source.pipe(createTransform(a)).pipe(destination)
desired scenario:
sourceWithAPrepended.pipe(createTransformReadingA()).pipe(destination)
I'm afraid I've been unable to work out how to even approach this. Assume that createTransform is a black box, and I want to create the function createTransformReadingA
Many thanks for any guidance

Related

NodeJS: Every audio file created through fs.writeFile and Blob has excessive length

I'm creating an app which needs to take the user's voice and convert it to text, but the audio file seems to have something wrong with the length after it's creation.
Here is how I'm gathering the audio and converting the data to a Blob. I'm just using the getUserMedia method to record the audio and convert it to a Blob when its stopped.
This is the beginning of the function I pass the Blob object to. I convert the Blob to a buffer and write that buffer to a file in the root directory of my project. But once the file has been written and I go to listen to it, the length of the audio is 435:13:24 no matter how long the original audio was. But even though the length is that long, the file sounds exactly like it should and ends at the correct time.
Img Heres a picture of what the file looks like when viewed.
Although this may not seem like a big deal since listening to the file provides the correct audio, I'm passing the file to an API that converts it to text, and it almost always gives either the wrong translation or an error about the file containing nothing. I've tried different ways of writing the Blob data to the file, and tried cutting off the excess length audio, but nothing has worked.
This is my first post on stack overflow since I ran out of options to fix this, so I'm sorry if the question is kind of vague or I formatted it incorrectly in some way.

Node.js Streams readable.readableLength not returning file size

I have been trying to upload a file by creating a readStream with fs.createReadStream(filePath) and piping to a writeStream. I need the file size to implement a progress bar.
In docs I see there is the readable.readableLength field, which to my understanding returns the file size but it returns zero in my case. I already got the file size with fs.statSync() but was curious.
The explanation in the documents was not really clear for me so I wanted to ask what readable.readableLength field represents? If it is the file size why it is valued zero?
readableLenght represents the amount of bytes in the stream that can still be read from it. If it is zero, the stream has probably already been read and thus there are zero bytes left for reading.

piping node.js object streams to multiple destinations is producing bizarre results -- why?

When piping one transform stream to two other transform streams, occasionally I'm getting a few of the objects from one destination stream appearing in place of the proper objects in the other destination stream. In a stream of 90,000 objects, in about 1 out of 3 runs about 10 objects starting at the sequence number about 10,000 are from the wrong stream (the start position of number of anomolous objects varies). What in the world could account for such bizarre results?
The setup:
sourceStream.pipe(processingStream1).pipe(check1);
processingStream1.pipe(check2).pipe(destinationStream1);
processingStream1.pipe(processingStream2).pipe(destinationStream2);
The sourceStream is a transform stream fed by a file read. The two destination streams are transform streams leading to file writes. Both the file read and file write are through the fs streaming API. All the streams rely on node.js automatic backpressure in piping.
Occasionally objects from processingStream2 are leaking into destinationStream1, as described above.
The checking streams (check1 a sink, check2 a passthrough) show the anomalous objects exist in the stream through check2 but not in the stream into check1.
The file reads and writes are of text (csv) files. I'm using Node.js version 8.6 on Windows 7 (though deserved, please don't throw rocks at me for the latter).
Suggestions on how to better isolate the problem also welcomed. The anomoly is structured enought that it doesn't seem like a generic memory leak, but not consistent enough to be a code error. I'm mystified.
Ugh! processingStream2 modifies the object in the stream coming through it (actually modifies a property of a sub-object). Apparently you can't count on the order of the pipes as controlling the order in changes in the streamed objects. Very occassionally, after sending the source objects through processingStream2, the input object to processingStream2 goes into processingStream1 via node internals. Probably as part of some optimization under the hood.
Lesson learned: don't change the input streamed object when piping to multiple destinations, even if you think you're making the change downstream. May you never have to learn this lesson the hard way!

How do I change the volume of a PCM audio stream in node?

I am trying to figure out how to adjust the volume level of a PCM audio stream in node.
I have looked all over npmjs.org at all of the modules that I could find for working with audio, but haven't found anything that will take a stream in, change the volume, and give me a stream out.
Are there any modules that exist that can do this, perhaps even even something that wasn't made specifically for it?
If not, then I could create a module, if someone can point me in the right direction for modifying a stream byte by byte.
Here is what I am trying to accomplish:
I am writing a program to receive several PCM audio streams, and mix them for several outputs with varying volume levels. Example:
inputs vol output
music 25% output 1
live audio 80% output 1
microphone 0% output 1
music 100% output 2
live audio 0% output 2
microphone 0% output 2
What type of connection are you using? (Would make it easier to give example code)
What you basically want to do, is create a connection. Then on the connection or request object add a listener for the 'data' event. If you don't set an encoding, the data parameter on the callback should be a Buffer. The data event is triggered after each chunk is delivered through the network.
The Buffer gives you byte-size access to the data-stream using regular javascript number values. You can then parse that chunk, keep them in memory over multiple data-events using a closure (in order to buffer multiple chunks). And when appropriate write the parsed and processed data to a socket (another socket or the same in case of bi-directional sockets). Don't forget to manage your closure in order to avoid memory leaks!
This is just an abstract description. Let me know if anything needs clarification.

Reading from a Network Stream in C#

I just have this issue with reading from a network stream in C#. Since am more of a Java developer I came across this issue.
In java I have this option of knowing the length of the received packet using the following code
int length = dataiInputStream.read(rcvPacket);
eventhough the size of the byte array rcvPacket assigned is larger than the amount of elements contained in it. which will allow me to read only the required length of elements so that i do not have elements in the byte array containing zeros.
While i was trying to use a similar thing on C# which was
long len = networkStream.length;
but the docucmentation says that this property is not supported. is there a workaround for this?
Thank you
The Java code doesn't show you the length of a packet. It shows you the amount of data read within that read call, which could have come from multiple packets.
NetworkStream.Read returns the same information, except using 0 to indicate the end of the stream instead of -1.

Resources