Merge pull request #984 from Diegoisawesome/master

Make aif2pcm looping not dependent on order of MARK and INST chunks
This commit is contained in:
Diegoisawesome 2020-02-19 09:18:16 -08:00 committed by GitHub
commit a85587d8c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,8 +34,8 @@ double ieee754_read_extended (uint8_t*);
#define FATAL_ERROR(format, ...) \
do \
{ \
fprintf(stderr, format, __VA_ARGS__); \
exit(1); \
fprintf(stderr, format, __VA_ARGS__); \
exit(1); \
} while (0)
#else
@ -43,8 +43,8 @@ do \
#define FATAL_ERROR(format, ...) \
do \
{ \
fprintf(stderr, format, ##__VA_ARGS__); \
exit(1); \
fprintf(stderr, format, ##__VA_ARGS__); \
exit(1); \
} while (0)
#endif // _MSC_VER
@ -65,8 +65,8 @@ struct Bytes {
};
struct Marker {
uint16_t id;
uint32_t position;
unsigned short id;
unsigned long position;
// don't care about the name
};
@ -174,7 +174,7 @@ void read_aif(struct Bytes *aif, AifData *aif_data)
}
struct Marker *markers = NULL;
unsigned short num_markers = 0;
unsigned short num_markers = 0, loop_start = 0, loop_end = 0;
unsigned long num_sample_frames = 0;
// Read all the Chunks to populate the AifData struct.
@ -272,42 +272,13 @@ void read_aif(struct Bytes *aif, AifData *aif_data)
unsigned short loop_type = (aif->data[pos++] << 8);
loop_type |= (uint8_t)aif->data[pos++];
if (loop_type && markers)
if (loop_type)
{
unsigned short marker_id = (aif->data[pos++] << 8);
marker_id |= (uint8_t)aif->data[pos++];
loop_start = (aif->data[pos++] << 8);
loop_start |= (uint8_t)aif->data[pos++];
struct Marker *cur_marker = markers;
// Grab loop start point.
for (int i = 0; i < num_markers; i++, cur_marker++)
{
if (cur_marker->id == marker_id)
{
aif_data->loop_offset = cur_marker->position;
aif_data->has_loop = true;
break;
}
}
marker_id = (aif->data[pos++] << 8);
marker_id |= (uint8_t)aif->data[pos++];
cur_marker = markers;
// Grab loop end point.
for (int i = 0; i < num_markers; i++, cur_marker++)
{
if (cur_marker->id == marker_id)
{
if (cur_marker->position < aif_data->loop_offset) {
aif_data->loop_offset = cur_marker->position;
aif_data->has_loop = true;
}
aif_data->num_samples = cur_marker->position;
break;
}
}
loop_end = (aif->data[pos++] << 8);
loop_end |= (uint8_t)aif->data[pos++];
}
else
{
@ -338,7 +309,40 @@ void read_aif(struct Bytes *aif, AifData *aif_data)
}
}
free(markers);
if (markers)
{
// Resolve loop points.
struct Marker *cur_marker = markers;
// Grab loop start point.
for (int i = 0; i < num_markers; i++, cur_marker++)
{
if (cur_marker->id == loop_start)
{
aif_data->loop_offset = cur_marker->position;
aif_data->has_loop = true;
break;
}
}
cur_marker = markers;
// Grab loop end point.
for (int i = 0; i < num_markers; i++, cur_marker++)
{
if (cur_marker->id == loop_end)
{
if (cur_marker->position < aif_data->loop_offset) {
aif_data->loop_offset = cur_marker->position;
aif_data->has_loop = true;
}
aif_data->num_samples = cur_marker->position;
break;
}
}
free(markers);
}
}
// This is a table of deltas between sample values in compressed PCM data.