Audio speed up
This commit is contained in:
parent
49b0e69299
commit
483af34fa7
1 changed files with 40 additions and 27 deletions
|
@ -9,13 +9,13 @@ const FORMAT:&str = "WAVE";
|
|||
|
||||
//FMT sub-chunk
|
||||
const SUBCHUNK_1_ID:&str = "fmt ";
|
||||
const SUBCHUNK_1_SIZE:usize = 16;
|
||||
const AUDIO_FORMAT:usize = 1;
|
||||
const NUM_CHANNELS:usize = 1;
|
||||
const SAMPLE_RATE:usize = 44100;
|
||||
const BYTE_RATE:usize = SAMPLE_RATE * NUM_CHANNELS * (SUBCHUNK_1_SIZE / 8);
|
||||
const BLOACK_ALIGN:usize = NUM_CHANNELS * (SUBCHUNK_1_SIZE / 8);
|
||||
const BITS_PR_SAMPLE:usize = 16;
|
||||
const SUBCHUNK_1_SIZE:i32 = 16;
|
||||
const AUDIO_FORMAT:i16 = 1;
|
||||
const NUM_CHANNELS:i16 = 1;
|
||||
const SAMPLE_RATE:i32 = 44100;
|
||||
const BYTE_RATE:i32 = SAMPLE_RATE * NUM_CHANNELS as i32 * (SUBCHUNK_1_SIZE / 8);
|
||||
const BLOACK_ALIGN:i16 = NUM_CHANNELS * (SUBCHUNK_1_SIZE / 8) as i16;
|
||||
const BITS_PR_SAMPLE:i16 = 16;
|
||||
|
||||
//Data sub-chunk
|
||||
const SUBCHUNK_2_ID:&str = "data";
|
||||
|
@ -32,32 +32,45 @@ fn write_as_bytes(vec:&mut Vec<u8>, value:usize, byte_size:u8){
|
|||
|
||||
pub async fn generateTone(frequency: f32, duration:f32) -> Sound{
|
||||
|
||||
let mut soundFileBytes = Vec::new();
|
||||
// const CHUNK_ID:&str = "RIFF";
|
||||
// const CHUNK_SIZE:&str = "----";
|
||||
// const FORMAT:&str = "WAVE";
|
||||
|
||||
soundFileBytes.append(&mut CHUNK_ID.clone().as_bytes().to_vec());
|
||||
// //FMT sub-chunk
|
||||
// const SUBCHUNK_1_ID:&str = "fmt ";
|
||||
// const SUBCHUNK_1_SIZE:u32 = 16;
|
||||
// const AUDIO_FORMAT:u16 = 1;
|
||||
// const NUM_CHANNELS:u16 = 1;
|
||||
// const SAMPLE_RATE:u32 = 44100;
|
||||
// const BYTE_RATE:u32 = SAMPLE_RATE * NUM_CHANNELS as u32 * (SUBCHUNK_1_SIZE / 8);
|
||||
// const BLOACK_ALIGN:u16 = NUM_CHANNELS * (SUBCHUNK_1_SIZE / 8) as u16;
|
||||
// const BITS_PR_SAMPLE:u16 = 16;
|
||||
|
||||
soundFileBytes.append(&mut CHUNK_SIZE.clone().as_bytes().to_vec());
|
||||
// //Data sub-chunk
|
||||
// const SUBCHUNK_2_ID:&str = "data";
|
||||
// const SUBCHUNK_2_SIZE:&str = "----";
|
||||
|
||||
soundFileBytes.append(&mut FORMAT.clone().as_bytes().to_vec());
|
||||
|
||||
soundFileBytes.append(&mut SUBCHUNK_1_ID.clone().as_bytes().to_vec());
|
||||
// const MAX_AMPLITUDE:usize = 32760;
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, SUBCHUNK_1_SIZE, 4);
|
||||
|
||||
let mut soundFileBytes = [
|
||||
CHUNK_ID.as_bytes(),
|
||||
CHUNK_SIZE.as_bytes(),
|
||||
FORMAT.as_bytes(),
|
||||
SUBCHUNK_1_ID.as_bytes(),
|
||||
&SUBCHUNK_1_SIZE.to_le_bytes(),
|
||||
&AUDIO_FORMAT.to_le_bytes(),
|
||||
&NUM_CHANNELS.to_le_bytes(),
|
||||
&SAMPLE_RATE.to_le_bytes(),
|
||||
&BYTE_RATE.to_le_bytes(),
|
||||
&BLOACK_ALIGN.to_le_bytes(),
|
||||
&BITS_PR_SAMPLE.to_le_bytes(),
|
||||
SUBCHUNK_2_ID.as_bytes(),
|
||||
SUBCHUNK_2_SIZE.as_bytes()
|
||||
].concat();
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, AUDIO_FORMAT, 2) ;
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, NUM_CHANNELS, 2);
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, SAMPLE_RATE, 4);
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, BYTE_RATE, 4);
|
||||
|
||||
write_as_bytes(&mut soundFileBytes, BLOACK_ALIGN, 2);
|
||||
write_as_bytes(&mut soundFileBytes, BITS_PR_SAMPLE, 2);
|
||||
|
||||
soundFileBytes.append(&mut SUBCHUNK_2_ID.clone().as_bytes().to_vec());
|
||||
|
||||
soundFileBytes.append(&mut SUBCHUNK_2_SIZE.clone().as_bytes().to_vec());
|
||||
|
||||
|
||||
let startAudio = soundFileBytes.len();
|
||||
|
@ -68,7 +81,7 @@ pub async fn generateTone(frequency: f32, duration:f32) -> Sound{
|
|||
let amplitude = 500. * f32::sin((i as f32 - 300.) / 1200.);
|
||||
let value = f32::sin((2. * PI * (i as f32) * (frequency as f32)) / SAMPLE_RATE as f32);
|
||||
let channel = (amplitude * if i+100 > lim {0.} else {value});
|
||||
println!("{}", value);
|
||||
|
||||
collect.push(channel);
|
||||
soundFileBytes.append(&mut (channel as i16).to_le_bytes().to_vec());
|
||||
|
||||
|
|
Loading…
Reference in a new issue