最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - play pcm data by webAudio API - Stack Overflow

programmeradmin2浏览0评论

Hi I am working on WebAudio API . I read HTML5 Web Audio API, porting from javax.sound and getting distortion link but not getting goodquality as in java API.I am getting PCM data from server in signed bytes . Then I have to changed this into it 16 bit format . for changing I am using ( firstbyte<<8 | secondbyte ) but I am not able to get good quality of sound . is there any problem in conversion or any other way to do for getting good quality of sound ?

Hi I am working on WebAudio API . I read HTML5 Web Audio API, porting from javax.sound and getting distortion link but not getting goodquality as in java API.I am getting PCM data from server in signed bytes . Then I have to changed this into it 16 bit format . for changing I am using ( firstbyte<<8 | secondbyte ) but I am not able to get good quality of sound . is there any problem in conversion or any other way to do for getting good quality of sound ?

Share Improve this question edited May 23, 2017 at 12:31 CommunityBot 11 silver badge asked Jan 3, 2012 at 10:28 user894554user894554 2685 silver badges18 bronze badges 3
  • 1 Code snippets are always helpful. – ebidel Commented Jan 3, 2012 at 16:58
  • can anyone tell me how to convert signed byte array to Float32Array ? – user894554 Commented Jan 4, 2012 at 9:28
  • Not too proud to upvote this, but this is a good question, as the official documentation skips the play PCM part, which would be the direct function, and focus on decodeAudioData(), which is more a 'helper'. – Léon Pelletier Commented Jan 20, 2013 at 20:04
Add a ment  | 

1 Answer 1

Reset to default 5

The Web Audio API uses 32-bit signed floats from -1 to 1, so that's what I'm going to (hopefully) show you how to do, rather than 16-bit as you mentioned in the question.

Assuming your array of samples is called samples and are stored as 2's pliment from -128 to 127, I think this should work:

var floats = new Float32Array(samples.length);
samples.forEach(function( sample, i ) {
  floats[i] = sample < 0 ? sample / 0x80 : sample / 0x7F;
});

Then you can do something like this:

var ac = new webkitAudioContext()
  , ab = ac.createBuffer(1, floats.length, ac.sampleRate)
  , bs = ac.createBufferSource();
ab.getChannelData(0).set(floats);
bs.buffer = ab;
bs.connect(ac.destination);
bs.start(0);
发布评论

评论列表(0)

  1. 暂无评论