Skip to content

Commit

Permalink
Added -h Option for HT2 Support
Browse files Browse the repository at this point in the history
The -h option can be used to generate inverted output for use in Houston Tracker 2.

Updated binaries for Linux & Windows to v0.8
  • Loading branch information
JeffAlyanak committed May 16, 2019
1 parent 774d96f commit 0f8f0eb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
9 changes: 7 additions & 2 deletions README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pcm2pwm 0.7 - a utility for 1-bit audio weirdos // code by: [email protected]
pcm2pwm 0.8 - a utility for 1-bit audio weirdos // code by: [email protected]
=========================================== ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pcm2pwm is a simply utility for converting 8-bit PCM wave audio into a differential
Expand Down Expand Up @@ -29,6 +29,10 @@ This would trigger the high and low states slightly earlier and might be useful
wav file isn't very loud. Moving these two values too close or too far from your average
amplitude will ruin timbre of the resulting output sound. Experiment to see what works best.

You may also add the -h option before the input file to toggle an inverted output format
suitable for Houston Tracker 2.

$ pcm2pwm -h input.wav <...>

Preparing your input audio
--------------------------
Expand Down Expand Up @@ -73,4 +77,5 @@ Version History

0.2 - Basic functionality
0.5 - Added the ability to set high and low crossover values.
0.7 - pcm2pwm now checks the input file's header to ensure compatibility.
0.7 - pcm2pwm now checks the input file's header to ensure compatibility.
0.8 - Added the -h option for outputting in a Houston Tracker 2 format.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pcm2pwm 0.7 - a utility for 1-bit audio weirdos // code by: [email protected]
# pcm2pwm 0.8 - a utility for 1-bit audio weirdos // code by: [email protected]
pcm2pwm is a simply utility for converting 8-bit PCM wave audio into a differential
PWM byte stream. It's output is intended for use with any 1-bit audio devices which
require a relatively high degree of compression.
Expand Down Expand Up @@ -26,6 +26,10 @@ This would trigger the high and low states slightly earlier and might be useful
wav file isn't very loud. Moving these two values too close or too far from your average
amplitude will ruin timbre of the resulting output sound. Experiment to see what works best.

You may also add the `-h` option before the input file to toggle an inverted output format suitable for [Houston Tracker 2](https://github.com/utz82/HoustonTracker2).

`$ pcm2pwm -h input.wav <...>`

##Preparing your input audio
With the addition - in version 0.5 - of custom high and low crossovers, a wider range of
audio can be run through the pcm2pwm tool. However, the following advice may still return
Expand Down Expand Up @@ -65,3 +69,4 @@ Pretty simple, eh!
* 0.2 - Basic functionality
* 0.5 - Added the ability to set high and low crossover values.
* 0.7 - pcm2pwm now checks the input file's header to ensure compatibility.
* 0.8 - Added the -h option for outputting in a Houston Tracker 2 format.
Binary file modified bin/Linux/pcm2pwm
Binary file not shown.
Binary file modified bin/Windows/pcm2pwm.exe
100644 → 100755
Binary file not shown.
6 changes: 4 additions & 2 deletions convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* code: jeff alyanak
*/

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include "convert.h"

int convert (FILE *fp, int length, int highValue, int lowValue)
int convert (FILE *fp, int length, int highValue, int lowValue, int format)
{
int count = 0; // Count period between zero cross-overs.
int flipState = 1; // High or low state.
Expand Down Expand Up @@ -50,7 +51,8 @@ int convert (FILE *fp, int length, int highValue, int lowValue)

if (count > 0xFF) count = 0xFF; // This hardcoded truncation will mess with any extremely low frequncy sounds you have by preventing the period time from being any higher than 255. This frequency floor will be different depending on your playback routine rate.

printf ("$%2.2X", count); // Print the period value in the correct format ($00-$FF).
printf ("$%02X", ( (1-format)*(count) + (format)*( (INT_MAX - count) - (INT_MAX - 0xFF) ) )); // Print the period value in the correct format ($00-$FF).

count = 0;
oldState = flipState;
}
Expand Down
2 changes: 1 addition & 1 deletion convert.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
int convert (FILE *fp, int length, int highValue, int lowValue);
int convert (FILE *fp, int length, int highValue, int lowValue, int format);
36 changes: 28 additions & 8 deletions pcm2pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "convert.h"
#include "header.h"

FILE *fp; // File pointer.
int length = 0; // File length in bytes.
int highValue = 0xFC; // Default high crossover.
int lowValue = 0x03; // Default low crossover
int format = 0; // Default (0) or HT2 format (1).

int main (int argc, char *argv[])
{
Expand All @@ -30,18 +32,36 @@ int main (int argc, char *argv[])
printf (" crossovers. Defaults are 252 and 3.\n");
printf (" (Values must be between 0 and 255.)\n");
printf ("ex: pcm2pwm sound.wav 230 20.\n\n");
printf ("You may also specify output formatted for\n");
printf ("Houston Tracker 2 using -h.\n");
printf ("For example: pcm2pwm -h input.wav\n");
printf (" This can be used with or without\n");
printf (" crossover values being specified.\n\n");
return 1;
}

if ((strncmp (argv[1], "-h", 2) == 0))
{
format = 1;
}
else if ((strncmp (argv[1], "-", 1) == 0))
{
printf ("Invalid argument. Refer to README.\n");
return 1;
}
// If crossover values are provided, convert them to integers and ensure they're between 0 and 255.
else if (argc == 4)
else if (argc > 3)
{
highValue = atoi (argv[2]);
lowValue = atoi (argv[3]);
highValue = atoi (argv[(2 + format)]);
lowValue = atoi (argv[(3 + format)]);

printf ("; The format is %d\n", format);
printf ("; High value is %d\n", highValue);
printf ("; Low value is %d\n", lowValue);

if (highValue > 255 || highValue < 0)
{
printf
("\nHigh crossover cannot be greater than 255 or lower than 0.\n\n");
printf ("\nHigh crossover cannot be greater than 255 or lower than 0.\n\n");
return 1;
}
else if (lowValue < 0 || lowValue > 255)
Expand All @@ -52,11 +72,11 @@ int main (int argc, char *argv[])
}

// Open file and ensure it opened correctly.
fp = fopen (argv[1], "rb");
fp = fopen (argv[1 + format], "rb");
if (fp == NULL)
{
fprintf (stderr, "Error opening file.\n");
perror (argv[1]);
perror (argv[1 + format]);
return EXIT_FAILURE;
}

Expand All @@ -74,7 +94,7 @@ int main (int argc, char *argv[])
length = ftell (fp);
rewind (fp);

convert (fp, length, highValue, lowValue);
convert (fp, length, highValue, lowValue, format);
printf ("\n");

return (EXIT_SUCCESS);
Expand Down

0 comments on commit 0f8f0eb

Please sign in to comment.