wave convert

本文介绍如何使用C#读取音频文件并将其转换为适用于DirectSound播放的格式。通过复制原始音频数据来实现音频文件的扩展,并利用DirectSound API创建音频缓冲区,最终保存为WAV格式。

 

FileStream fs = new FileStream(@"c:/x/t.txt.bak", FileMode.Open, FileAccess.Read);

byte[] sbuf = new byte[fs.Length];

BinaryReader br = new System.IO.BinaryReader(fs);

br.Read(sbuf, 0, (

int)fs.Length);

int times = 2;

byte[] buf = new byte[sbuf.Length * times];

for (int i = 0; i < sbuf.Length; i++)

{

for (int n = 0; n < times; n++)

{

buf[i * times + n] = sbuf[i];

}

}

DxVBLibA.

DirectSoundSecondaryBuffer8 dsToneBuffer = WavUtility.CreateWav(this.Handle.ToInt32(), buf);

dsToneBuffer.SaveToFile(

@"c:/x/wavtest.wav");

MessageBox.Show("done");

 

======================

 

using

System;

using

System.Collections.Generic;

using

System.Text;

 

namespace

WaveConvert

{

public class WavUtility

{

const int SRATE = 44100;//Sampling Rate

const double DUR = 1;//Tone duration

const double FREQ = 500;//Tone frequency

public static DxVBLibA.DirectSoundSecondaryBuffer8 CreateWav(int hwnd, byte[] orgdata)

{

byte[] data = new byte[orgdata.Length + 455];

for (int i = 0; i < 255; i++)

{

data[i] = (

byte)i;

}

for (int i = 255; i < 100 + 255; i++)

{

data[i] =

byte.MaxValue;

}

for (int i = data.Length - 100; i < data.Length; i++)

{

data[i] =

byte.MaxValue;

}

Array.Copy(orgdata, 0, data, 355, orgdata.Length);

 

DxVBLibA.

DirectX8 DX = new DxVBLibA.DirectX8();

DxVBLibA.

DirectSound8 DS = DX.DirectSoundCreate(string.Empty);

DS.SetCooperativeLevel(hwnd, DxVBLibA.

CONST_DSSCLFLAGS.DSSCL_NORMAL);

DxVBLibA.

DSBUFFERDESC desc = new DxVBLibA.DSBUFFERDESC();

#region

wFormatTag

// Waveform-audio format type.

// Format tags are registered with Microsoft Corporation for many compression algorithms.

// A complete list of format tags can be found in the Mmreg.h header file.

// For one- or two-channel PCM data, this value should be WAVE_FORMAT_PCM.

#endregion

desc.fxFormat.nFormatTag = (

short)DxVBLibA.CONST_DSOUND.WAVE_FORMAT_PCM;

#region

cbSize

// Size, in bytes, of extra format information appended to the end of the WAVEFORMATEX structure.

// This information can be used by non-PCM formats to store extra attributes for the wFormatTag.

// If no extra information is required by the wFormatTag, this member must be set to zero.

// For WAVE_FORMAT_PCM formats, this member is ignored.

#endregion

desc.fxFormat.nSize = 0;

desc.fxFormat.lExtra = 0;

#region

nChannels

// Number of channels in the waveform-audio data. Monaural data uses one channel and stereo data uses two channels.

#endregion

desc.fxFormat.nChannels = 1;

#region

nSamplesPerSec

// Sample rate, in samples per second (hertz).

// If wFormatTag is WAVE_FORMAT_PCM,

// then common values for nSamplesPerSec are 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz. For non-PCM formats,

// this member must be computed according to the manufacturer's specification of the format tag.

#endregion

desc.fxFormat.lSamplesPerSec = SRATE;

#region

wBitsPerSample

// Bits per sample for the wFormatTag format type. If wFormatTag is WAVE_FORMAT_PCM,

// then wBitsPerSample should be equal to 8 or 16.

// If wFormatTag is WAVE_FORMAT_EXTENSIBLE, this value can be any integer multiple of 8.

#endregion

desc.fxFormat.nBitsPerSample = 16;

#region

nBlockAlign

// Block alignment, in bytes. The block alignment is the minimum atomic unit of data for the wFormatTag format type.

// If wFormatTag is WAVE_FORMAT_PCM or WAVE_FORMAT_EXTENSIBLE,

// nBlockAlign must be equal to the product of nChannels and wBitsPerSample divided by 8 (bits per byte).

// For non-PCM formats, this member must be computed according to the manufacturer's specification of the format tag.

// Software must process a multiple of nBlockAlign bytes of data at a time.

// Data written and read from a device must always start at the beginning of a block.

// For example, it is illegal to start playback of PCM data in the middle of a sample (that is, on a non-block-aligned boundary).

#endregion

desc.fxFormat.nBlockAlign = (

short)(desc.fxFormat.nChannels * desc.fxFormat.nBitsPerSample / 8);

#region

nAvgBytesPerSec

// Required average data-transfer rate, in bytes per second, for the format tag.

// If wFormatTag is WAVE_FORMAT_PCM, nAvgBytesPerSec should be equal to the product of nSamplesPerSec and nBlockAlign.

// For non-PCM formats, this member must be computed according to the manufacturer's specification of the format tag.

#endregion

desc.fxFormat.lAvgBytesPerSec = desc.fxFormat.lSamplesPerSec * desc.fxFormat.nBlockAlign;

desc.lFlags = 0;

desc.lBufferBytes = data.Length;

DxVBLibA.

DirectSoundSecondaryBuffer8 dsToneBuffer = DS.CreateSoundBuffer(ref desc);

System.Runtime.InteropServices.

GCHandle hmem = System.Runtime.InteropServices.GCHandle.Alloc(data, System.Runtime.InteropServices.GCHandleType.Pinned);

IntPtr aaa = hmem.AddrOfPinnedObject();

dsToneBuffer.WriteBuffer(0, data.Length, aaa, DxVBLibA.

CONST_DSBLOCKFLAGS.DSBLOCK_DEFAULT);

hmem.Free();

return dsToneBuffer;

}

}

}

### Simulink中Convert模块的使用方法及功能介绍 #### 1. Convert模块的基本功能 Convert模块在Simulink中用于将信号线的数据类型从一种类型转换为另一种类型。这种转换对于确保模型中不同部分之间的数据兼容性至关重要[^1]。例如,当一个模块输出双精度浮点数(`double`),而另一个模块需要单精度浮点数(`single`)时,可以使用Convert模块进行数据类型的转换。 #### 2. 数据类型转换的方法 Convert模块支持多种数据类型转换方法,包括但不限于以下几种: - **直接转换**:将输入信号的数据类型直接转换为目标数据类型。 - **继承转换**:通过上下文推断目标数据类型[^2]。 - **舍入模式**:在转换过程中,可以选择不同的舍入模式以适应特定需求。常见的舍入模式包括向上、向下、最邻近值等[^3]。 #### 3. 舍入模式的详细说明 舍入模式决定了如何处理无法精确表示的数值。以下是几种常见的舍入模式及其特点: - **向上**:将所有数值朝正无穷大方向舍入。 - **收敛**:将数值舍入到最近的可表示值;若出现对等,则舍入到最接近的偶数整数。 - **向下**:将所有数值朝负无穷大方向舍入[^3]。 - **最邻近值**:将数值舍入到最近的可表示值;若出现对等,则朝正无穷大的方向舍入[^3]。 - **零**:将数值向零方向舍入[^3]。 #### 4. 使用示例 以下是一个简单的示例,展示如何在Simulink模型中使用Convert模块将双精度信号转换为单精度信号。 ```matlab % 创建Sine Wave模块生成双精度信号 sineWave = simscape.SineWave; sineWave.OutputDataType = 'double'; % 添加Convert模块进行数据类型转换 convertModule = simscape.Convert; convertModule.OutputDataType = 'single'; ``` #### 5. 注意事项 在使用Convert模块时,需要注意以下几点: - 确保目标数据类型与下游模块的要求一致,否则可能导致错误或不准确的结果。 - 在高精度计算场景中,选择合适的舍入模式以减少数值误差[^1]。 - 对于嵌套子系统中的Convert模块,需特别注意数据流的方向和类型匹配问题。 #### 6. 示例代码:完整的Simulink模型配置 以下是一个完整的MATLAB脚本,用于配置包含Convert模块的Simulink模型: ```matlab function configureModel() % 创建Sine Wave模块 sineWave = add_block('simulink/Sources/Sine Wave', 'myModel/Sine Wave'); set_param(sineWave, 'OutputDataType', 'double'); % 添加Convert模块 convertModule = add_block('simulink/Signal Attributes/Data Type Conversion', 'myModel/Convert'); set_param(convertModule, 'OutputDataType', 'single'); % 连接模块 connect_blocks('myModel/Sine Wave/1', 'myModel/Convert/1'); end ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值