菜鸟教程小白 发表于 2022-12-13 02:18:32

iphone - 如何在ios中将FLAC文件转换为wav文件?


                                            <p><p><em>我想创建一个支持 Flac 格式的音频文件的音频播放器。为此我尝试实现 flac 到 wav 转换的算法,如下所示</em></p>

<p>请帮帮我。</p>

<p>它总是给我错误</p>

<p>错误:初始化解码器:FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE</p>

<pre><code>static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
{
return
fputc(x, f) != EOF &amp;&amp;
fputc(x &gt;&gt; 8, f) != EOF
;
}

static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
{
return write_little_endian_uint16(f, (FLAC__uint16)x);
}

static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
{
return
fputc(x, f) != EOF &amp;&amp;
fputc(x &gt;&gt; 8, f) != EOF &amp;&amp;
fputc(x &gt;&gt; 16, f) != EOF &amp;&amp;
fputc(x &gt;&gt; 24, f) != EOF
;
}

int main(int argc, char *argv[])
{
const char *input = &#34;demo_audio_shaer.flac&#34;;
printf(&#34;aa====%s&#34;,argv);

FLAC__bool ok = true;
FLAC__StreamDecoder *decoder = 0;
FLAC__StreamDecoderInitStatus init_status;
FILE *fout;

if((fout = fopen(argv, &#34;wb&#34;)) == NULL) {
    fprintf(stderr, &#34;ERROR: opening %s for output\n&#34;, argv);
    return 1;
}

if((decoder = FLAC__stream_decoder_new()) == NULL) {
    fprintf(stderr, &#34;ERROR: allocating decoder\n&#34;);
    fclose(fout);
    return 1;
}

(void)FLAC__stream_decoder_set_md5_checking(decoder, true);
init_status = FLAC__stream_decoder_init_file(decoder, input, write_callback, metadata_callback, error_callback, /*client_data=*/fout);
if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
    fprintf(stderr, &#34;ERROR: initializing decoder: %s\n&#34;, FLAC__StreamDecoderInitStatusString);
    ok = false;
}

if(ok) {
    ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
    fprintf(stderr, &#34;decoding: %s\n&#34;, ok? &#34;succeeded&#34; : &#34;FAILED&#34;);

    fprintf(stderr, &#34;   state: %s\n&#34;, FLAC__StreamDecoderStateString);
}

FLAC__stream_decoder_delete(decoder);
fclose(fout);

return 0;
}

FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const               FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
FILE *f = (FILE*)client_data;
const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
size_t i;

(void)decoder;

if(total_samples == 0) {
    fprintf(stderr, &#34;ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n&#34;);
    return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if(channels != 2 || bps != 16) {
    fprintf(stderr, &#34;ERROR: this example only supports 16bit stereo streams\n&#34;);
    return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}

/* write WAVE header before we write the first frame */
if(frame-&gt;header.number.sample_number == 0) {
    if(
       fwrite(&#34;RIFF&#34;, 1, 4, f) &lt; 4 ||
       !write_little_endian_uint32(f, total_size + 36) ||
       fwrite(&#34;WAVEfmt &#34;, 1, 8, f) &lt; 8 ||
       !write_little_endian_uint32(f, 16) ||
       !write_little_endian_uint16(f, 1) ||
       !write_little_endian_uint16(f, (FLAC__uint16)channels) ||
       !write_little_endian_uint32(f, sample_rate) ||
       !write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
       !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
       !write_little_endian_uint16(f, (FLAC__uint16)bps) ||
       fwrite(&#34;data&#34;, 1, 4, f) &lt; 4 ||
       !write_little_endian_uint32(f, total_size)
       )
    {
      fprintf(stderr, &#34;ERROR: write error\n&#34;);
      return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
    }
}

/* write decoded PCM samples */
for(i = 0; i &lt; frame-&gt;header.blocksize; i++) {
    if(
       !write_little_endian_int16(f, (FLAC__int16)buffer) ||/* left channel */
       !write_little_endian_int16(f, (FLAC__int16)buffer)   /* right channel */
       ) {
      fprintf(stderr, &#34;ERROR: write error\n&#34;);
      return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
    }
}

return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
(void)decoder, (void)client_data;

/* print some stats */
if(metadata-&gt;type == FLAC__METADATA_TYPE_STREAMINFO) {
    /* save for later */
    total_samples = metadata-&gt;data.stream_info.total_samples;
    sample_rate = metadata-&gt;data.stream_info.sample_rate;
    channels = metadata-&gt;data.stream_info.channels;
    bps = metadata-&gt;data.stream_info.bits_per_sample;

    fprintf(stderr, &#34;sample rate    : %u Hz\n&#34;, sample_rate);
    fprintf(stderr, &#34;channels       : %u\n&#34;, channels);
    fprintf(stderr, &#34;bits per sample: %u\n&#34;, bps);
#ifdef _MSC_VER
    fprintf(stderr, &#34;total samples: %I64u\n&#34;, total_samples);
#else
    fprintf(stderr, &#34;total samples: %llu\n&#34;, total_samples);
#endif
}
}

void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
(void)decoder, (void)client_data;

fprintf(stderr, &#34;Got error callback: %s\n&#34;, FLAC__StreamDecoderErrorStatusString);
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我可以提出我的解决方案。
<a href="https://github.com/Krivoblotsky/KSAudioPlayer" rel="noreferrer noopener nofollow">https://github.com/Krivoblotsky/KSAudioPlayer</a> </p>

<p>它支持 *.flac 和许多其他文件格式。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 如何在ios中将FLAC文件转换为wav文件?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/16080888/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/16080888/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 如何在ios中将FLAC文件转换为wav文件?