Changeset 183


Ignore:
Timestamp:
12/21/06 13:37:08 (17 years ago)
Author:
r2d
Message:
  • added peak calculation
  • gain results are now written to sv8 files
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libmpc/branches/r2d/mpcgain/mpcgain.c

    r182 r183  
    3333*/
    3434#include <stdio.h>
     35#include <math.h>
    3536#include <mpcdec/mpcdec.h>
     37#include <mpc/minimax.h>
    3638#include <replaygain/gain_analysis.h>
     39
     40#include "../libmpcdec/internal.h"
     41
     42// mpc_bits_reader.c
     43int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block);
     44unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size);
     45
     46// crc32.c
     47unsigned long crc32(unsigned char *buf, int len);
    3748
    3849static void usage(const char *exename)
     
    4859        mpc_status err;
    4960        MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
     61        MPC_SAMPLE_FORMAT album_max = 0;
     62        mpc_uint16_t album_gain;
     63        mpc_uint16_t album_peak;
     64        mpc_uint16_t * title_gain;
     65        mpc_uint16_t * title_peak;
     66        mpc_uint32_t * header_pos;
    5067        Float_t left_samples[MPC_FRAME_LENGTH * sizeof(Float_t)];
    5168        Float_t right_samples[MPC_FRAME_LENGTH * sizeof(Float_t)];
     
    5774        }
    5875
     76        title_gain = malloc((sizeof(mpc_uint16_t) * 2 + sizeof(mpc_uint32_t)) * (argc - 1));
     77        title_peak = title_gain + (argc - 1);
     78        header_pos = (mpc_uint32_t *) (title_peak + (argc - 1));
     79
    5980        for( j = 1; j < argc; j++){
     81                MPC_SAMPLE_FORMAT title_max = 0;
     82
    6083                err = mpc_reader_init_stdio(&reader, argv[j]);
    6184                if(err < 0) return !MPC_STATUS_OK;
     
    78101                                left_samples[i] = sample_buffer[2 * i] * (1 << 15);
    79102                                right_samples[i] = sample_buffer[2 * i + 1] * (1 << 15);
     103                                title_max = maxf(title_max, sample_buffer[2 * i]);
     104                                title_max = maxf(title_max, sample_buffer[2 * i + 1]);
    80105                        }
    81106
     
    83108                }
    84109
    85                 printf("%s : %f\n", argv[j], GetTitleGain());
     110                title_gain[j-1] = (mpc_uint16_t) (GetTitleGain() * 256);
     111                title_peak[j-1] = (mpc_uint16_t) (log10(title_max * (1 << 15)) * 20 * 256);
     112                header_pos[j-1] = si.header_position + 4;
     113
     114                album_max = maxf(album_max, title_max);
    86115
    87116                mpc_demux_exit(demux);
     
    89118        }
    90119
    91         printf("album : %f\n", GetAlbumGain());
     120        album_gain = (mpc_uint16_t) (GetAlbumGain() * 256);
     121        album_peak = (mpc_uint16_t) (log10(album_max * (1 << 15)) * 20 * 256);
     122
     123        for( j = 0; j < argc - 1; j++) {
     124                unsigned char buffer[64];
     125                mpc_bits_reader r;
     126                mpc_block b;
     127                mpc_uint64_t size;
     128                mpc_uint32_t crc_pos, crc;
     129                FILE * file;
     130
     131                file = fopen( argv[j + 1], "r+");
     132                fseek(file, header_pos[j] - 4, SEEK_SET);
     133                fread(buffer, 1, 16, file);
     134                if (memcmp(buffer, "MPCK", 4) != 0) {
     135                        fprintf(stderr, "Unsupported file format, not a sv8 file : %s\n", argv[j + 1]);
     136                        fclose(file);
     137                        continue;
     138                }
     139                r.buff = buffer + 4;
     140                r.count = 8;
     141
     142                size = mpc_bits_get_block(&r, &b);
     143
     144                while( memcmp(b.key, "SI", 2) != 0 ) {
     145                        header_pos[j] += b.size;
     146                        fseek(file, header_pos[j], SEEK_SET);
     147                        fread(buffer, 1, 16, file);
     148                        r.buff = buffer;
     149                        r.count = 8;
     150                        size = mpc_bits_get_block(&r, &b);
     151                }
     152
     153                header_pos[j] += size;
     154
     155                fread(buffer + 16, 1, 48, file);
     156
     157                crc_pos = r.buff - buffer + (8 > r.count); // save position of CRC
     158                r.buff += 4; // skip CRC
     159                mpc_bits_get_size(&r, &size);
     160                size += 4 + crc_pos;
     161
     162                buffer[size] = 1; // replaygain version
     163                buffer[size + 1] = title_gain[j] >> 8;
     164                buffer[size + 2] = title_gain[j] & 0xFF;
     165                buffer[size + 3] = title_peak[j] >> 8;
     166                buffer[size + 4] = title_peak[j] & 0xFF;
     167                buffer[size + 5] = album_gain >> 8;
     168                buffer[size + 6] = album_gain & 0xFF;
     169                buffer[size + 7] = album_peak >> 8;
     170                buffer[size + 8] = album_peak & 0xFF;
     171
     172                crc = crc32(buffer + crc_pos + 4, (int)b.size - 4);
     173                buffer[crc_pos] = crc >> 24;
     174                buffer[crc_pos + 1] = (crc >> 16) & 0xFF;
     175                buffer[crc_pos + 2] = (crc >> 8) & 0xFF;
     176                buffer[crc_pos + 3] = crc & 0xFF;
     177
     178                fseek(file, header_pos[j], SEEK_SET);
     179                fwrite(buffer + crc_pos, 1, b.size, file);
     180                fclose(file);
     181        }
     182
     183        free(title_gain);
    92184
    93185    return 0;
Note: See TracChangeset for help on using the changeset viewer.