mirror of https://github.com/nealey/hdjd.git
Standardize debugging, reindent (sorry, Plan 9 got me into some weird habits)
This commit is contained in:
parent
0893c5a302
commit
c9ca950835
12
Makefile
12
Makefile
|
@ -1,7 +1,7 @@
|
||||||
CFLAGS += -Wall
|
CFLAGS += -Wall
|
||||||
CFLAGS += -Werror
|
CFLAGS += -Werror
|
||||||
TARGETS = hdjd aac123 explore
|
TARGETS = hdjd explore
|
||||||
CFLAGS += -g
|
#CFLAGS += -g -DDEBUG
|
||||||
|
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
||||||
|
@ -15,13 +15,5 @@ explore.o: CFLAGS += $(shell pkg-config --cflags libusb-1.0)
|
||||||
alsa.o: CFLAGS += $(shell pkg-config --cflags alsa)
|
alsa.o: CFLAGS += $(shell pkg-config --cflags alsa)
|
||||||
usb.o: CFLAGS += $(shell pkg-config --cflags libusb-1.0)
|
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:
|
clean:
|
||||||
rm -f $(TARGETS) *.o
|
rm -f $(TARGETS) *.o
|
||||||
|
|
156
aac123.c
156
aac123.c
|
@ -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
16
alsa.c
|
@ -106,28 +106,28 @@ alsa_read_ready()
|
||||||
converted = snd_midi_event_decode(midi_event_parser, buf, ALSA_BUFSIZE, ev);
|
converted = snd_midi_event_decode(midi_event_parser, buf, ALSA_BUFSIZE, ev);
|
||||||
if (converted < 0) {
|
if (converted < 0) {
|
||||||
if (ev->type == SND_SEQ_EVENT_CLIENT_START ) {
|
if (ev->type == SND_SEQ_EVENT_CLIENT_START ) {
|
||||||
printf("Client started \n");
|
DUMPf("Client started \n");
|
||||||
continue;
|
continue;
|
||||||
} else if (ev->type == SND_SEQ_EVENT_CLIENT_EXIT) {
|
} else if (ev->type == SND_SEQ_EVENT_CLIENT_EXIT) {
|
||||||
printf("Client exited \n");
|
DUMPf("Client exited \n");
|
||||||
continue;
|
continue;
|
||||||
} else if (ev->type == SND_SEQ_EVENT_CLIENT_CHANGE) {
|
} else if (ev->type == SND_SEQ_EVENT_CLIENT_CHANGE) {
|
||||||
printf("Client status/info changed \n");
|
DUMPf("Client status/info changed \n");
|
||||||
continue;
|
continue;
|
||||||
} else if (ev->type == SND_SEQ_EVENT_PORT_START) {
|
} else if (ev->type == SND_SEQ_EVENT_PORT_START) {
|
||||||
printf("Port created \n");
|
DUMPf("Port created \n");
|
||||||
continue;
|
continue;
|
||||||
} else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
|
} else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
|
||||||
printf("Port deleted \n");
|
DUMPf("Port deleted \n");
|
||||||
continue;
|
continue;
|
||||||
} else if (ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
|
} else if (ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
|
||||||
printf("Port status/info changed \n");
|
DUMPf("Port status/info changed \n");
|
||||||
continue;
|
continue;
|
||||||
} else if (ev->type == SND_SEQ_EVENT_PORT_SUBSCRIBED) {
|
} else if (ev->type == SND_SEQ_EVENT_PORT_SUBSCRIBED) {
|
||||||
printf("Port connected \n");
|
DUMPf("Port connected \n");
|
||||||
continue;
|
continue;
|
||||||
} else if (ev->type == SND_SEQ_EVENT_PORT_UNSUBSCRIBED) {
|
} else if (ev->type == SND_SEQ_EVENT_PORT_UNSUBSCRIBED) {
|
||||||
printf("Port disconnected \n");
|
DUMPf("Port disconnected \n");
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
warn("Can't decode MIDI event type 0x%02x", ev->type);
|
warn("Can't decode MIDI event type 0x%02x", ev->type);
|
||||||
|
|
2
dump.h
2
dump.h
|
@ -6,7 +6,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/* Some things I use for debugging */
|
/* Some things I use for debugging */
|
||||||
#ifdef NODUMP
|
#ifdef DEBUG
|
||||||
# define DUMPf(fmt, args...)
|
# define DUMPf(fmt, args...)
|
||||||
#else
|
#else
|
||||||
# define DUMPf(fmt, args...) fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##args)
|
# define DUMPf(fmt, args...) fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##args)
|
||||||
|
|
81
usb.c
81
usb.c
|
@ -40,6 +40,18 @@ struct libusb_transfer *xfer_in2;
|
||||||
static int writes_pending = 0;
|
static int writes_pending = 0;
|
||||||
static int reads_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(struct libusb_transfer *xfer);
|
||||||
void usb_xfer_done_additional(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;
|
int datalen = xfer->actual_length;
|
||||||
reads_pending -= 1;
|
reads_pending -= 1;
|
||||||
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
|
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
|
||||||
#ifndef NDEBUG
|
usb_debug_msg("Receiving", dev_info->ep_in, data, datalen);
|
||||||
printf("Receiving on %02x:", dev_info->ep_in);
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < datalen; i += 1) {
|
|
||||||
printf("%02x ", data[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
#endif
|
|
||||||
alsa_write(data, datalen);
|
alsa_write(data, datalen);
|
||||||
}
|
}
|
||||||
free(data);
|
free(data);
|
||||||
|
@ -114,27 +119,20 @@ usb_xfer_done(struct libusb_transfer *xfer)
|
||||||
void
|
void
|
||||||
usb_xfer_done_additional(struct libusb_transfer *xfer)
|
usb_xfer_done_additional(struct libusb_transfer *xfer)
|
||||||
{
|
{
|
||||||
uint8_t *data = xfer->buffer;
|
|
||||||
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
|
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
|
||||||
//We don't need to use the information of this call, but it is needed that we
|
// 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.
|
// poll this, or else it hangs and doesn't receive more data.
|
||||||
#ifndef NDEBUG
|
usb_debug_msg("Receiving", dev_info->ep_in2, xfer->buffer, xfer->actual_length);
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
free(data);
|
|
||||||
libusb_free_transfer(xfer);
|
|
||||||
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
|
if ( xfer->status == LIBUSB_TRANSFER_COMPLETED ) {
|
||||||
usb_initiate_transfer_additional();
|
usb_initiate_transfer_additional();
|
||||||
} else if ( xfer->status != LIBUSB_TRANSFER_CANCELLED ) {
|
} else if ( xfer->status != LIBUSB_TRANSFER_CANCELLED ) {
|
||||||
fatal("Stopping EP_IN2, because of status %d\nSoftware needs restarting", xfer->status);
|
fatal("Stopping EP_IN2, because of status %d\nSoftware needs restarting", xfer->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(xfer->buffer);
|
||||||
|
libusb_free_transfer(xfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -149,9 +147,9 @@ usb_setup(char *name, size_t namelen)
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
// Enable debug
|
// Enable debug
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
#ifndef NDEBUG
|
#ifdef DEBUG
|
||||||
libusb_set_debug(context, LIBUSB_LOG_LEVEL_WARNING);
|
libusb_set_debug(context, LIBUSB_LOG_LEVEL_WARNING);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (libusb_pollfds_handle_timeouts(context) == 0) {
|
if (libusb_pollfds_handle_timeouts(context) == 0) {
|
||||||
fatal("I'm too dumb to handle events on such an old system.");
|
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);
|
libusb_free_pollfds(usb_fds);
|
||||||
usb_fds = NULL;
|
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 ) {
|
if ( xfer->status == LIBUSB_TRANSFER_TIMED_OUT ) {
|
||||||
warn("Send 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) {
|
} else if ( xfer->status && xfer->status != LIBUSB_TRANSFER_CANCELLED) {
|
||||||
warn("USB Write status %d", xfer->status);
|
warn("USB Write status %d", xfer->status);
|
||||||
}
|
}
|
||||||
writes_pending -= 1;
|
writes_pending -= 1;
|
||||||
uint8_t *data = xfer->buffer;
|
usb_debug_msg("Sent", dev_info->ep_out, xfer->buffer, xfer->actual_length);
|
||||||
#ifndef NDEBUG
|
free(xfer->buffer);
|
||||||
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);
|
|
||||||
libusb_free_transfer(xfer);
|
libusb_free_transfer(xfer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,13 +341,6 @@ usb_write(uint8_t *data, size_t datalen)
|
||||||
memcpy(buf, data, datalen);
|
memcpy(buf, data, datalen);
|
||||||
libusb_fill_bulk_transfer(xfer, usb_dev, dev_info->ep_out, buf, datalen, usb_write_done, NULL, 0);
|
libusb_fill_bulk_transfer(xfer, usb_dev, dev_info->ep_out, buf, datalen, usb_write_done, NULL, 0);
|
||||||
libusb_submit_transfer(xfer);
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue