0

だから私は私が単純化したこの素晴らしいCFFMpegの公式のを見つけました:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#include "libavcodec/avcodec.h"
#include "libavutil/mathematics.h"

#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

/*
 * Audio encoding example
 */
static void audio_encode_example(const char *filename)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int frame_size, i, j, out_size, outbuf_size;
    FILE *f;
    short *samples;
    float t, tincr;
    uint8_t *outbuf;

    printf("Audio encoding\n");

    /* find the MP2 encoder */
    codec = avcodec_find_encoder(CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c= avcodec_alloc_context();

    /* put sample parameters */
    c->bit_rate = 64000;
    c->sample_rate = 44100;
    c->channels = 2;

    /* open it */
    if (avcodec_open(c, codec) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    /* the codec gives us the frame size, in samples */
    frame_size = c->frame_size;
    samples = malloc(frame_size * 2 * c->channels);
    outbuf_size = 10000;
    outbuf = malloc(outbuf_size);

    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename);
        exit(1);
    }

    /* encode a single tone sound */
    t = 0;
    tincr = 2 * M_PI * 440.0 / c->sample_rate;
    for(i=0;i<200;i++) {
        for(j=0;j<frame_size;j++) {
            samples[2*j] = (int)(sin(t) * 10000);
            samples[2*j+1] = samples[2*j];
            t += tincr;
        }
        /* encode the samples */
        out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
        fwrite(outbuf, 1, out_size, f);
    }
    fclose(f);
    free(outbuf);
    free(samples);

    avcodec_close(c);
    av_free(c);
}

int main(int argc, char **argv)
{

    /* must be called before using avcodec lib */
    avcodec_init();

    /* register all the codecs */
    avcodec_register_all();

    audio_encode_example("test.mp2");

    return 0;
}

どのように聞こえますか?何かがわからないかもしれませんが、それはひどい音に聞こえます=(オーディオ生成のサウンドをより良くする方法/より面白くする/慎重な方法でメロディックな方法(このコードを変更してサウンドをより良くする方法だけの特別な機能はありません)?

4

2 に答える 2

1

私がコードを正しく理解していれば、それは純音か上昇音であるはずです (よくわかりません)。いずれにせよ、あまりいい音にはならないでしょう。自然界で純粋な正弦波の音を聞くことはめったにありません。私たちが「音楽的」に聞こえると考えるもののほとんどは、多くの異なる周波数で構成されている傾向があります。

それを変更することに関しては、おそらく、複数の正弦波の観点からピッチを変化させるために使用できるさまざまなサウンド生成アルゴリズムを調べることができます。

于 2010-07-23T10:34:57.767 に答える