ozdemirburak / morse-code-translator

Translate text to Morse code and vice versa, with the option to play Morse code audio.
https://morsecodetranslator.com
MIT License
216 stars 57 forks source link

Export Morse Code Audio to WAV #16

Closed ozdemirburak closed 4 years ago

ozdemirburak commented 4 years ago

Currently, the library misses a feature that is a bit challenging: exporting the Morse code audio to WAV.

Not sure how to do this effectively, but the gist below clearly shows how to create WAV from the buffer, which may help in the beginning.

Link to the Gist

chris--jones commented 4 years ago

This is the sort of challenge I'm up for.

ozdemirburak commented 4 years ago

I'd be more than happy to accept your PR, @chris--jones.

chris--jones commented 4 years ago

@ozdemirburak - I'm really close... I have a "working" wave export, branch is here: https://github.com/chris--jones/morsify/tree/wave-export

The complexity of wave conversion was taken largely from https://github.com/mattdiamond/Recorderjs/blob/master/src/recorder.js so a lot of credit to @mattdiamond

I'm not satisfied yet though, because:

I'm going to see if I can get this working via an OfflineAudioContext, but this will mean duplicating the audio nodes as you cannot connect them across contexts. This may not be a big deal, but if resource usage increases it might be better to expose the export interface as a separate construct so that the resources can be instantiated as required.

Finally I'd like to ask about the API itself - presently I'm returning the Blob object containing the wave data as this is the most flexible, however it will most likely be used via a call to URL.createObjectURL(blob); to either assign it to an audio player or start a download - so it might make sense to return this object url or even offer a convenience method to start a download.

ozdemirburak commented 4 years ago

@chris--jones It is definitely great progress for the initial commit.

I think when someone calls the export method, it should start a download by default. But, maybe there can be an optional parameter for returning the blob object.

I'll also have a detailed look tomorrow. Thanks again.

chris--jones commented 4 years ago

I have it working with OfflineAudioContext now and it works as I had hoped - exporting the file very quickly 👍

I will need to refactor a bit to maintain both contexts, and unfortunately OfflineAudioContext requires me to iterate over the morse to get the audio length to initialise the offline audio context, only for it to then have to iterate over the morse again to set the gain levels which I can't do until I get the gain from the context 😠 ...oh well.

If it sounds ok to you, I'll submit my PR once it's working via offline audio context and defer working on the migration to the newer AudioWorkletNode (https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletNode) as a separate issue as it looks like it may require externalising the processing audio node script so that it can be run as a worker thread which may mean multiple scripts in the solution (I'm guessing you prefer the simplicity of a simple file).

ozdemirburak commented 4 years ago

Sure, sounds great. About the AudioWorkletNode, I am not familiar with it, but that definitely looks like an ideal solution.

chris--jones commented 4 years ago

@ozdemirburak - I managed to solve it without using the deprecated script node as the offline audio context provides a buffer in the complete event. This site was a great help: http://joesul.li/van/tale-of-no-clocks/

I've added more details in the PR #17