                     unixcw Patch, version 0.1
          by Paolo Cravero (ik1zyw at yahoo! dot com)


This is a quick and dirty patch to Simon Baldwin's unixcw software.
Once the patch is applied and the software is recompiled, you'll be
able to hear the CW tones through the installed soundcard on your
Linux box. Some features of unixcw won't work with the patched
software (tone frequency control, basically).

Please read the collection of mails below. They illustrate both what
the patch does and how to apply it.

Happy CWing.
Paolo, IK1ZYW


-.-. --.-  -.-. --.-  -.-. --.-  -.-. --.-  -.-. --.-  -.-. --.-  -.- -.-


Date: Mon, 15 Oct 2001 12:33:57 +0200
From: Paolo Cravero
To: Simon Baldwin
Subject: Re: unixcw & soundcard, anyone?


On Thu, 11 Oct 2001 15:16:39 -0700, Simon Baldwin wrote:

Dear Simon,
I have a good news: unixcw is now able to send output to a soundcard!!!

> A few weeks back I received email from somebody calling himself Farnis,
> who was also looking for soundcard functionality on

I know Fabio, and he introduced me to unixcw. :-)

Let me tell you how I implemented the modification.

First of all, few words about soundcards. Although the OSS (Open Sound
System) linux/unix driver supports all possible features, sound hardware
does not.
So you might encounter a card that does not output simple PCM with 8 bit,
or the one that has no embedded mixer, or whatever. Adding a general
support for all cards is almost impossible. I think I used the most
available feature on cards: the hardware output mixer.

After thinking of generating the tone within your cwlib library (that would
lead to a huge work), I came to the idea that:
- I can generate the tone externally, with a software (that I already had
written, BTW :-)), continously
- I can modulate the soundcard output volume according to the CW keying: 0
key up, <>0 key down

I saw how you implemented the CW keying on the speaker, with a ioctl call
that starts and stops the tone. Changing levels on the audio mixer is also
made with a ioctl call, so very few changes were made to the code.

Yes, I did modify cwlib.c . My changes are marked on the source code, but
they do not maintain both the speaker and soundcard compatibility on the
code. I did not modify any makefile since I have no proper development
tools here.

> You'll probably also want to add a "volume" control to xcwcp, cwcp,
> and cw!

Because of my lazyness, I did not implement any sort of volume control.
Well, there are actually other three ways of changing the volume control:
- loudspeaker volume control (if using loudspeakers)
- soundcard mixer's PCM volume control (I change the global volume, could
be swapped actually)
- amplitude of the software generated tone

> Finally, I'd suggest that if you can, you make your changes so that
> the programs will still compile on, and run on, systems that don't
> have soundcards on them (for example, mine!).

My patch is really quick and dirty. I can add some documentation for the
features that I added/changed, but I doubt I will go any further. You can
actually release a unixcw-soundcard package, that simply has the modified
cwlib.c, probably. But that's up to you.

--------------Operating instructions follow--------------------

Replace the cwlib.c that comes with unixcw tarball with the one in this
patch. Recompile unixcw following author's directions: if you could
compile it before applying the patch, it compiles as well when patched. 

After compilation of unixcw package, you need to compile the tone-generator
with:

g++ tone-generator.c -o tone-generator

Make sure that tone-generator.h is in the same directory of the ".c".
You must ensure that you have a properly installed soundcard, which
supports mixer control: cat /dev/sndstat will show if there is a mixer
available or not. You must ensure that your unix/Linux user has
write access to both /dev/dsp and /dev/mixer !!!!

Once everything is properly compiled, launch ./tone-generator . Optional
command line parameters are sampling rate (8 kHz default) and tone
frequency (800 Hz default).
If your soundcard volume is not set to zero, you should hear a continous
tone at 800 Hz (or whatever you told the software to generate).

Start cw, cwcp or xcwcp and start enjoying the CW lessons!

NOTE: on cwcp and xcwcp the tone frequency control won't work!!!

---------------end-----------

> Would you also like to write a soundcard CW off-air reader and decoder
> using the library? :-)

:-)) This is actually more challenging. If I find out how Windows software
analyzes the audio input, I can try to do something on linux. In my final
project I wrote a tone recognition software for linux thru soundcard using
a FFT (Fourier Transform) routine. I think this is the way for decoding CW
too... who knows?!

I attach the modified cwlib.c as well as my tone-generator (I still have to
fix the comments on the source, please don't distribute it yet!).

Looking forward to hearing from you,
Paolo 9a/ik1zyw

PS: the CW keying I have implemented actually modulates with OOK (on-off
keying, CW) any audio output you can get out of the soundcard. I initially
tested the thing with an audio CD :-))


-.-. --.-  -.-. --.-  -.-. --.-  -.-. --.-  -.-. --.-  -.-. --.-  -.- -.-


Date: Wed, 27 Feb 2002 09:47:25 -0800
From: Simon Baldwin
To: John Kelly
CC: Paolo Cravero
Subject: Re: Fw: unixcw-2.0


John,

I think this might be correct, as far as it goes.  Although I've not
tried the patch myself, I think I can see from Paolo's mail that
accompanies the patch how it works.  I'm sure he'll correct me if I'm
wrong.

As I understand it, the modification to Unixcw actually comes in two
parts.  The first part is a change to cwlib.c that stops it from
generating KIOCSOUND tones on the PC speaker, and instead makes it
'interrupt' any audio to the soundcard.  The second part is a constant
tone generator, so that there is now some audio to 'interrupt'.

This probably isn't ideal, and I think Paolo makes the point that it's
a little quick and dirty.  But it would have the right effect if you
set things up correctly.

So... running 'tone_generator' will indeed do nothing more, on its own,
than create a constant tone through the soundcard.  To get CW through
the soundcard, what you'll then need to do is to run one of the Unixcw
tools (cwcp, cw, or xcwcp) while the tone_generator is running.  This
should then interrupt the constant tone, to create Morse code.  Don't
forget here to run cw/cwcp/xcwcp binaries that have been recompiled with
Paolo's cwlib.c patch, otherwise you've got only half of the changes.

One other consequence to note for this scheme is that the frequency
controls available to cwcp, xcwcp, and cw (command line flag) simply
won't do anything now - libcw is in charge only of gating tones to the
soundcard, and has no control over their creation.

I hope this is enough to get you going.  As I say, I haven't been able
to try the patch.  My home Linux system doesn't have a soundcard fitted.

Oddly, when I wrote Unixcw, although unable to use a soundcard, I'd
originally thought that using the PC speaker might actually be a slight
advantage, since it leaves main audio free for MP3 or other use more
worthwhile than simple beeps.  I may have been wrong - enough people
have requested it to partially convice me otherwise.  Unfortunately,
the tone generation problem makes is less than straightforward to retro-
fit.

I hope this helps.  Best regards,

--S

