void MetadataCallback(IntPtr decoder, FLAC__StreamMetadata *metadata, void *client_data) { if (metadata->type == FLAC__MetadataType.FLAC__METADATA_TYPE_STREAMINFO) { m_pcm = new AudioPCMConfig( metadata->stream_info.bits_per_sample, metadata->stream_info.channels, metadata->stream_info.sample_rate, (AudioPCMConfig.SpeakerConfig) 0); m_sampleCount = metadata->stream_info.total_samples; } #if SUPPORTMETADATA if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { for (int tagno = 0; tagno < metadata->vorbis_comment.num_comments; tagno++) { char *field_name, *field_value; if (!FLACDLL.FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(metadata->vorbis_comment.comments[tagno], &field_name, &field_value)) { throw new Exception("Unable to parse vorbis comment."); } string name = Marshal::PtrToStringAnsi((IntPtr)field_name); free(field_name); array <Byte> ^ bvalue = new array <Byte>((int)strlen(field_value)); Marshal.Copy((IntPtr)field_value, bvalue, 0, (int)strlen(field_value)); free(field_value); UTF8Encoding enc = new UTF8Encoding(); string value = enc.GetString(bvalue); _tags.Add(name, value); } } #endif }
public Reader(DecoderSettings settings, string path, Stream IO) { m_settings = settings; m_writeCallback = WriteCallback; m_metadataCallback = MetadataCallback; m_errorCallback = ErrorCallback; m_readCallback = ReadCallback; m_seekCallback = SeekCallback; m_tellCallback = TellCallback; m_lengthCallback = LengthCallback; m_eofCallback = EofCallback; m_decoderActive = false; m_sampleOffset = 0; m_sampleBuffer = null; m_path = path; m_bufferOffset = 0; m_bufferLength = 0; m_stream = (IO != null) ? IO : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); m_decoder = FLACDLL.FLAC__stream_decoder_new(); if (0 == FLACDLL.FLAC__stream_decoder_set_metadata_respond(m_decoder, FLAC__MetadataType.FLAC__METADATA_TYPE_VORBIS_COMMENT)) { throw new Exception("unable to setup the decoder"); } FLAC__StreamDecoderInitStatus st = FLACDLL.FLAC__stream_decoder_init_stream( m_decoder, m_readCallback, m_stream.CanSeek ? m_seekCallback : null, m_stream.CanSeek ? m_tellCallback : null, m_stream.CanSeek ? m_lengthCallback : null, m_stream.CanSeek ? m_eofCallback : null, m_writeCallback, m_metadataCallback, m_errorCallback, null); if (st != FLAC__StreamDecoderInitStatus.FLAC__STREAM_DECODER_INIT_STATUS_OK) { throw new Exception(string.Format("unable to initialize the decoder: {0}", st)); } m_decoderActive = true; if (0 == FLACDLL.FLAC__stream_decoder_process_until_end_of_metadata(m_decoder)) { throw new Exception("unable to retrieve metadata"); } }
public Encoder(EncoderSettings settings, string path, Stream output = null) { m_path = path; m_stream = output; m_settings = settings; m_streamGiven = output != null; m_initialized = false; m_finalSampleCount = -1; m_samplesWritten = 0; m_write_callback = StreamEncoderWriteCallback; m_seek_callback = StreamEncoderSeekCallback; m_tell_callback = StreamEncoderTellCallback; if (m_settings.PCM.BitsPerSample < 16 || m_settings.PCM.BitsPerSample > 24) { throw new Exception("bits per sample must be 16..24"); } m_encoder = FLACDLL.FLAC__stream_encoder_new(); FLACDLL.FLAC__stream_encoder_set_bits_per_sample(m_encoder, (uint)m_settings.PCM.BitsPerSample); FLACDLL.FLAC__stream_encoder_set_channels(m_encoder, (uint)m_settings.PCM.ChannelCount); FLACDLL.FLAC__stream_encoder_set_sample_rate(m_encoder, (uint)m_settings.PCM.SampleRate); }