首页 > 其他分享 >经典c代码集合

经典c代码集合

时间:2022-09-19 10:25:17浏览次数:60  
标签:代码 argv wave header riff file 经典 集合 chunk

1 解析命令行输入指令

 /* parse command line arguments */
    argv += 2;
    while (*argv) {
        if (strcmp(*argv, "-d") == 0) {
            argv++;
            if (*argv)
                device = atoi(*argv);
        }
        if (strcmp(*argv, "-p") == 0) {
            argv++;
            if (*argv)
                period_size = atoi(*argv);
        }
        if (strcmp(*argv, "-n") == 0) {
            argv++;
            if (*argv)
                period_count = atoi(*argv);
        }
        if (strcmp(*argv, "-D") == 0) {
            argv++;
            if (*argv)
                card = atoi(*argv);
        }
        if (strcmp(*argv, "-m") == 0) {
            argv++;
            if (*argv)
                loop_minutes = atoi(*argv);
        }
        if (strcmp(*argv, "-i") == 0) {
            argv++;
            if (*argv)
                loop_num = atoi(*argv);
        }
        if (*argv)
            argv++;
    }

 

 

2 关于文件的操作字节读取的操作

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>

#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT  0x20746d66
#define ID_DATA 0x61746164

struct riff_wave_header {
    uint32_t riff_id;
    uint32_t riff_sz;
    uint32_t wave_id;
};

struct chunk_header {
    uint32_t id;
    uint32_t sz;
};

struct chunk_fmt {
    uint16_t audio_format;
    uint16_t num_channels;
    uint32_t sample_rate;
    uint32_t byte_rate;
    uint16_t block_align;
    uint16_t bits_per_sample;
};




FILE *file;
char *filename;
int more_chunks = 1;
struct riff_wave_header riff_wave_header;
struct chunk_header chunk_header;
struct chunk_fmt chunk_fmt;

filename = argv[1];
file = fopen(filename, "rb");
if (!file) {
    fprintf(stderr, "Unable to open file '%s'\n", filename);
    return 1;
}

fread(&riff_wave_header, sizeof(riff_wave_header), 1, file);
if ((riff_wave_header.riff_id != ID_RIFF) ||
    (riff_wave_header.wave_id != ID_WAVE)) {
    fprintf(stderr, "Error: '%s' is not a riff/wave file\n", filename);
    fclose(file);
    return 1;
}

do {
    fread(&chunk_header, sizeof(chunk_header), 1, file);

    switch (chunk_header.id) {
    case ID_FMT:
        fread(&chunk_fmt, sizeof(chunk_fmt), 1, file);
        /* If the format header is larger, skip the rest */
        if (chunk_header.sz > sizeof(chunk_fmt))
            fseek(file, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
        break;
    case ID_DATA:
        /* Stop looking for chunks */
        more_chunks = 0;
        break;
    default:
        /* Unknown chunk, skip bytes */
        fseek(file, chunk_header.sz, SEEK_CUR);
    }
} while (more_chunks);

fclose(file);

 

 

 

 

 

//

标签:代码,argv,wave,header,riff,file,经典,集合,chunk
From: https://www.cnblogs.com/RYSBlog/p/16706789.html

相关文章