filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.15k stars 451 forks source link

Added SoundTouch to be able to adjust pitch and tempo with effecting the audio with side effects. #348

Closed ArizonaInkStudios closed 6 years ago

filoe commented 6 years ago

Great! But could you please move it to a seperate project within the Samples folder? Would like to keep main CSCore project clean from dependencies. Similar to the https://github.com/filoe/cscore/tree/master/Samples/NVorbisIntegration sample.

ArizonaInkStudios commented 6 years ago

Sure thing!

ArizonaInkStudios commented 6 years ago

I moved it.

filoe commented 6 years ago

Great, I've added two comments. Maybe we can fix that and merge it afterwards. Thanks!

ArizonaInkStudios commented 6 years ago

All complete. Let me know if we have any other oddities.

Did you give the sample a go?

filoe commented 6 years ago

Sample looks good to me. Just one question. Why is there a coefficient of 8? See https://github.com/ArizonaInkStudios/cscore/blob/master/SoundTouchPitchAndTempo/SoundTouchSource.cs#L74 and following.

Also one last please: Move the sample to the Samples folder. Thanks! :+1.

ArizonaInkStudios commented 6 years ago

The number of bytes read. It may not be correct and I am open for changes!

filoe commented 6 years ago

I don't think that this will really work. There are different input pcm formats like 8, 16, 24, 32 bit. Also the underlying WaveSource might provide Ieeefloat. I would suggest the following:

  1. Inherit from SampleAggregatorBase instead of WaveAggregatorBase
  2. The Read method will provide floating point values automatically.
  3. If SoundTouch provides more samples than you can read, you would have to handle that differently. Before putting new samples to SoundTouch you would have to make sure that you already received all samples.
  4. Have you thought about seeking? When the position gets changed, you would have to reset the SoundTouch buffer (flush or what ever - don't know SoundTouch). Also does SoundTouch change the number samples of a track by processing them? Let's say you've got 100 input samples. Is the output always 100 samples? If not, is there a way for handling that when seeking?
ArizonaInkStudios commented 6 years ago

Those are things to consider absolutely. I do have a separate project with this code working and it seeks without issue. Nonetheless, perhaps more testing with other PCM formats is in order to ensure that works.

Most of the tests I have done are with MP3 that are 16bit I believe. I will create some test wave files and do some hard testing to see what may break or cause issues.

The SoundTouch examples are not very well done and I had to use the source and the examples and the lack of documentation to make this work. I am definitely not an expert on how some of their library works.

I will take your suggestions and see what changes can be made. You know your project better than anyone. Thank you for your help! Hopefully we can get a good sample working that fulfills all the sources.

ArizonaInkStudios commented 6 years ago

Here is my results from testing.

I used: 8bits-48khz.wav 16bits-48khz.wav 24bits-48khz.wav 32bits-IEEE-48khz.wav

All of these worked with the code as is.

I also did seeking and position changing while tempo/pitch change and it all worked as expected.

I tried to convert to SampleAggregatorBase and use float buffers as the base and that was bad; VERY bad. Using the float buffer in many different attempts I either got an exception and bad explosions or the audio was choppy, had static, and was not audio that was good in any way. I am all for updating this but I am a bit stumped on how to accomplish it. I am open for suggestions.

Sorry this is such an ordeal. I will not give up. If there is a better answer, I would like to find it.

filoe commented 6 years ago

Sorry for the late response. I've merged it. Thanks!

ArizonaInkStudios commented 6 years ago

No worries! Glad I could help and contribute something. I hope to do more in the future! Great working with you.