Jacajack/cmorse

Generate Audio From Morse Code

jconard3 opened this issue · 7 comments

A feature I would like to see is the ability to translate a piece of text into morse code and a corresponding audio file. Could be as simple as generating a raw WAV file or if we wanted to get fancy, piping straight to default audio device.

Actually, I thought about that before, it's a pretty nice idea 👍

I've been generating WAV files using C before - it's relatively easy to implement. As you said, beeping using some kind of sound library may be little harder. I've used Port Audio before, but I think it's a good idea to question ourselves if it's really worth including it in such small project (obviously, that doesn't mean that we can't at least try! 😝)

@Jacajack did you implement a WAV library from scratch or use a pre-exiting one?

It was very basic library - I implemented it by myself.
This was very useful for me: http://soundfile.sapp.org/doc/WaveFormat/

@jconard3, I found it. It's pretty messy, but there it is

#include <stdio.h>
#include <math.h>
#include <inttypes.h>
#include <stdlib.h>

typedef union
{
	struct 
	{
		//RIFF chunk descriptor
		char chunkID[4];
		uint32_t chunkSize;
		char format[4];
	
		//Data chunk
		char subchunk1ID[4];
		uint32_t subchunk1Size;
		uint16_t audioFormat;
		uint16_t numChannels;
		uint32_t sampleRate;
		uint32_t byteRate;
		uint16_t blockAlign;
		uint16_t bitsPerSample;
	

		//Data chunk
		char subchunk2ID[4];
		uint32_t subchunk2Size;
	};

	char raw[44];
		
} FileWAV;


void makeWAV( FileWAV *wav, uint8_t channels, uint8_t bitsPerSample, uint16_t sampleRate, float duration )
{
	wav->chunkID[0] = 'R';
	wav->chunkID[1] = 'I';
	wav->chunkID[2] = 'F';
	wav->chunkID[3] = 'F';
	wav->chunkSize = 36 + channels * sampleRate * bitsPerSample / 8 * duration;
	wav->format[0] = 'W';
	wav->format[1] = 'A';
	wav->format[2] = 'V';
	wav->format[3] = 'E';

	wav->subchunk1ID[0] = 'f';
	wav->subchunk1ID[1] = 'm';
	wav->subchunk1ID[2] = 't';
	wav->subchunk1ID[3] = ' ';
	wav->subchunk1Size = 16;
	wav->audioFormat = 1;
	wav->numChannels = channels;
	wav->sampleRate = sampleRate;
	wav->byteRate = sampleRate * bitsPerSample / 8;
	wav->blockAlign = 2;
	wav->bitsPerSample = bitsPerSample;

	wav->subchunk2ID[0] = 'd';
	wav->subchunk2ID[1] = 'a';
	wav->subchunk2ID[2] = 't';
	wav->subchunk2ID[3] = 'a';
	wav->subchunk2Size = channels * sampleRate * bitsPerSample / 8 * duration;
}

int main( )
{
	float duration = 60.0f, frequency = 440.0f;

	FileWAV wav;
	uint32_t i;
	uint16_t *sine = malloc( 44100 * 2 * duration );
	char *sineb = (char *) sine;

	//Samples: 44100
	//Channels: 1
	//SamplesPerSec: 44100
	//ByteRate: 88200
	//BitsPerSample: 16 
	
	makeWAV( &wav, 1, 16, 44100, 60.0f ); 
	
	for ( i = 0; i < 44100 * duration; i++ )
		sine[i] = 20000.0 * sin( i / ( 44100.0f / frequency ) * 2.0 * M_PI );

	for ( i = 0; i < sizeof( FileWAV ); i++ )
		putchar( wav.raw[i] );

	for ( i = 0; i < 88200 * duration; i++ )
		putchar( sineb[i] );
		
	return 0;
}

Ok thanks @Jacajack. I'll see if I can clean this up a bit and then add it to this feature.

A w drugą stronę? czytanie wav i odczytywanie kodów?
przy okazji możesz zobaczyc lcwo.net/ tez Polak pisze ;)

@letarg0 Aktualnie mam ambitniejsze projekty na głowie, więc nie liczyłbym na taką funkcjonalność w niedalekiej przyszłości, ale oczywiście zawsze możesz otworzyć nowy pull request.

Translation for clarity (if somebody ever reads this):

I currently have more ambitious projects to develop, so I wouldn't expect such functionality to be added anytime soon, but feel free to open a new pull request if you want.