Standardize debugging, reindent (sorry, Plan 9 got me into some weird habits)

This commit is contained in:
Neale Pickett 2017-03-18 14:45:39 -06:00
parent 0893c5a302
commit c9ca950835
6 changed files with 448 additions and 639 deletions

View File

@ -1,7 +1,7 @@
CFLAGS += -Wall
CFLAGS += -Werror
TARGETS = hdjd aac123 explore
CFLAGS += -g
TARGETS = hdjd explore
#CFLAGS += -g -DDEBUG
all: $(TARGETS)
@ -15,13 +15,5 @@ explore.o: CFLAGS += $(shell pkg-config --cflags libusb-1.0)
alsa.o: CFLAGS += $(shell pkg-config --cflags alsa)
usb.o: CFLAGS += $(shell pkg-config --cflags libusb-1.0)
aac123: CFLAGS += $(shell pkg-config --cflags alsa)
aac123: LDLIBS += $(shell pkg-config --libs alsa)
aac123: LDLIBS += -lfaad -lmp4v2
aactest: CFLAGS += $(shell pkg-config --cflags alsa)
aactest: LDLIBS += $(shell pkg-config --libs alsa)
aactest: LDLIBS += -lfaad -lmp4v2
clean:
rm -f $(TARGETS) *.o

156
aac123.c
View File

@ -1,156 +0,0 @@
#include <stdio.h>
#include <malloc.h>
#include <alsa/asoundlib.h>
#include <neaacdec.h>
#include <mp4v2/mp4v2.h>
#include "dump.h"
static int
GetAACTrack(MP4FileHandle *infile)
{
/* find AAC track */
MP4TrackId numTracks = MP4GetNumberOfTracks(infile, NULL, 0);
MP4TrackId i;
for (i = 1; i <= numTracks; i++)
{
uint8_t obj_type;
const char *track_type = MP4GetTrackType(infile, i);
if (! track_type) continue;
if (!MP4_IS_AUDIO_TRACK_TYPE(track_type)) continue;
/* MP4GetTrackAudioType */
obj_type = MP4GetTrackEsdsObjectTypeId(infile, i);
if (obj_type == MP4_INVALID_AUDIO_TYPE)
continue;
if (obj_type == MP4_MPEG4_AUDIO_TYPE) {
obj_type = MP4GetTrackAudioMpeg4Type(infile, i);
if (MP4_IS_MPEG4_AAC_AUDIO_TYPE(obj_type))
return i;
} else {
if (MP4_IS_AAC_AUDIO_TYPE(obj_type))
return i;
}
}
/* can't decode this */
return -1;
}
int
play_file(snd_pcm_t *snd, char *fn)
{
int track;
MP4FileHandle infile;
NeAACDecHandle hDecoder;
NeAACDecConfigurationPtr config;
unsigned char *buffer;
uint32_t buffer_size;
unsigned long samplerate;
unsigned char channels;
long sampleId, numSamples;
void *sample_buffer;
infile = MP4Read(fn);
if (! infile) {
fprintf(stderr, "Unable to open stream\n");
return 1;
}
if ((track = GetAACTrack(infile)) < 0) {
fprintf(stderr, "GetAACTrack\n");
return 1;
}
hDecoder = NeAACDecOpen();
config = NeAACDecGetCurrentConfiguration(hDecoder);
config->outputFormat = FAAD_FMT_16BIT;
config->downMatrix = 1;
config->defObjectType = LC;
NeAACDecSetConfiguration(hDecoder, config);
buffer = NULL;
buffer_size = 0;
MP4GetTrackESConfiguration(infile, track, &buffer, &buffer_size);
if (NeAACDecInit2(hDecoder, buffer, buffer_size, &samplerate, &channels) < 0) {
fprintf(stderr, "Initializing decoder\n");
return 1;
}
if (snd_pcm_set_params(snd,
SND_PCM_FORMAT_S16,
SND_PCM_ACCESS_RW_INTERLEAVED,
channels,
samplerate,
1,
500000) < 0) {
fprintf(stderr, "Set ALSA params\n");
return 1;
}
if (buffer) {
free(buffer);
}
DUMP_d(MP4GetTrackMaxSampleSize(infile, track));
numSamples = MP4GetTrackNumberOfSamples(infile, track);
DUMP_d(numSamples);
for (sampleId = 1; sampleId <= numSamples; sampleId++) {
int rc;
unsigned int sample_count;
NeAACDecFrameInfo frameInfo;
buffer = NULL;
buffer_size = 0;
rc = MP4ReadSample(infile, track, sampleId, &buffer, &buffer_size, NULL, NULL, NULL, NULL);
if (rc == 0) {
fprintf(stderr, "Read failed\n");
return 1;
}
sample_buffer = NeAACDecDecode(hDecoder, &frameInfo, buffer, buffer_size);
sample_count = frameInfo.samples;
snd_pcm_writei(snd, sample_buffer, sample_count / channels);
}
return 0;
}
int
main(int argc, char *argv[])
{
int i;
snd_pcm_t *snd;
if (snd_pcm_open(&snd, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
fprintf(stderr, "Opening ALSA\n");
return 1;
}
for (i = 1; i < argc; i += 1) {
char *fn = argv[i];
printf("%s\n", fn);
play_file(snd, fn);
}
return 0;
}

16
alsa.c
View File

@ -106,28 +106,28 @@ alsa_read_ready()
converted = snd_midi_event_decode(midi_event_parser, buf, ALSA_BUFSIZE, ev);
if (converted < 0) {
if (ev->type == SND_SEQ_EVENT_CLIENT_START ) {
printf("Client started \n");
DUMPf("Client started \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_CLIENT_EXIT) {
printf("Client exited \n");
DUMPf("Client exited \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_CLIENT_CHANGE) {
printf("Client status/info changed \n");
DUMPf("Client status/info changed \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_START) {
printf("Port created \n");
DUMPf("Port created \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
printf("Port deleted \n");
DUMPf("Port deleted \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
printf("Port status/info changed \n");
DUMPf("Port status/info changed \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_SUBSCRIBED) {
printf("Port connected \n");
DUMPf("Port connected \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_UNSUBSCRIBED) {
printf("Port disconnected \n");
DUMPf("Port disconnected \n");
continue;
} else {
warn("Can't decode MIDI event type 0x%02x", ev->type);

2
dump.h
View File

@ -6,7 +6,7 @@
#include <string.h>
/* Some things I use for debugging */
#ifdef NODUMP
#ifdef DEBUG
# define DUMPf(fmt, args...)
#else
# define DUMPf(fmt, args...) fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##args)

81
usb.c
View File

@ -40,6 +40,18 @@ struct libusb_transfer *xfer_in2;
static int writes_pending = 0;
static int reads_pending = 0;
void
usb_debug_msg(char *action, int ep, uint8_t data[], size_t datalen)
{
#ifdef DEBUG
fprintf(stderr, "%s on ep%02x:", action, ep);
for (int i = 0; i < datalen; i += 1) {
fprintf(stderr, " %02x", data[i]);
}
fprintf(stderr, "\n");
#endif
}
void usb_xfer_done(struct libusb_transfer *xfer);
void usb_xfer_done_additional(struct libusb_transfer *xfer);
@ -93,14 +105,7 @@ usb_xfer_done(struct libusb_transfer *xfer)
int datalen = xfer->actual_length;
reads_pending -= 1;
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
#ifndef NDEBUG
printf("Receiving on %02x:", dev_info->ep_in);
int i;
for (i = 0; i < datalen; i += 1) {
printf("%02x ", data[i]);
}
printf("\n");
#endif
usb_debug_msg("Receiving", dev_info->ep_in, data, datalen);
alsa_write(data, datalen);
}
free(data);
@ -114,27 +119,20 @@ usb_xfer_done(struct libusb_transfer *xfer)
void
usb_xfer_done_additional(struct libusb_transfer *xfer)
{
uint8_t *data = xfer->buffer;
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
//We don't need to use the information of this call, but it is needed that we
//poll this, or else it hangs and doesn't receive more data.
#ifndef NDEBUG
int datalen = xfer->actual_length;
printf("Receiving on %02x:", dev_info->ep_in2);
int i;
for (i = 0; i < datalen; i += 1) {
printf("%02x ", data[i]);
// We don't need to use the information of this call, but it is needed that we
// poll this, or else it hangs and doesn't receive more data.
usb_debug_msg("Receiving", dev_info->ep_in2, xfer->buffer, xfer->actual_length);
}
printf("\n");
#endif
}
free(data);
libusb_free_transfer(xfer);
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
usb_initiate_transfer_additional();
} else if ( xfer->status != LIBUSB_TRANSFER_CANCELLED ) {
fatal("Stopping EP_IN2, because of status %d\nSoftware needs restarting", xfer->status);
}
free(xfer->buffer);
libusb_free_transfer(xfer);
}
int
@ -149,9 +147,9 @@ usb_setup(char *name, size_t namelen)
//----------------------------------------------------------------------------
// Enable debug
//----------------------------------------------------------------------------
#ifndef NDEBUG
#ifdef DEBUG
libusb_set_debug(context, LIBUSB_LOG_LEVEL_WARNING);
#endif
#endif
if (libusb_pollfds_handle_timeouts(context) == 0) {
fatal("I'm too dumb to handle events on such an old system.");
@ -308,10 +306,10 @@ usb_check_fds(fd_set *rfds, fd_set *wfds)
}
}
#if LIBUSB_API_VERSION >= 0x01000104
#if LIBUSB_API_VERSION >= 0x01000104
libusb_free_pollfds(usb_fds);
usb_fds = NULL;
#endif // LIBUSB_API_VERSION >= 0x01000104
#endif // LIBUSB_API_VERSION >= 0x01000104
}
@ -320,30 +318,12 @@ usb_write_done(struct libusb_transfer *xfer)
{
if ( xfer->status == LIBUSB_TRANSFER_TIMED_OUT ) {
warn("Send timed out");
#ifndef NDEBUG
uint8_t *data = xfer->buffer;
int datalen = xfer->actual_length;
int i;
for (i = 0; i < datalen; i += 1) {
printf("%02x ", data[i]);
}
printf("\n");
#endif
} else if ( xfer->status && xfer->status != LIBUSB_TRANSFER_CANCELLED) {
warn("USB Write status %d", xfer->status);
}
writes_pending -= 1;
uint8_t *data = xfer->buffer;
#ifndef NDEBUG
int datalen = xfer->actual_length;
printf("Sent on %02x:", dev_info->ep_out);
int i;
for (i = 0; i < datalen; i += 1) {
printf("%02x ", data[i]);
}
printf("\n");
#endif
free(data);
usb_debug_msg("Sent", dev_info->ep_out, xfer->buffer, xfer->actual_length);
free(xfer->buffer);
libusb_free_transfer(xfer);
}
@ -361,13 +341,6 @@ usb_write(uint8_t *data, size_t datalen)
memcpy(buf, data, datalen);
libusb_fill_bulk_transfer(xfer, usb_dev, dev_info->ep_out, buf, datalen, usb_write_done, NULL, 0);
libusb_submit_transfer(xfer);
#ifndef NDEBUG
printf("Preparing to send on %02x:", dev_info->ep_out);
int i;
for (i = 0; i < datalen; i += 1) {
printf("%02x ", data[i]);
}
printf("\n");
#endif
usb_debug_msg("Preparing to send", dev_info->ep_out, data, datalen);
}