-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass encoder to VoIPMediaSession audio extras (#1015)
Currently audio extras in VoIPMediaSession use AudioEncoder for encoding audio extras. It leads to problems if we are using custom encoder: AudioEncoder.EncodeAudio method throws an exception because of unsupported audio format. So we need a way to inject a custom encoder for VoIPMediaSession audio extras. Co-authored-by: name <[email protected]>
- Loading branch information
Showing
5 changed files
with
48 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace SIPSorcery.Media | ||
{ | ||
internal static class PcmResampler | ||
{ | ||
public static short[] Resample(short[] pcm, int inRate, int outRate) | ||
{ | ||
if (inRate == outRate) | ||
{ | ||
return pcm; | ||
} | ||
else if (inRate == 8000 && outRate == 16000) | ||
{ | ||
// Crude up-sample to 16Khz by doubling each sample. | ||
return pcm.SelectMany(x => new short[] { x, x }).ToArray(); | ||
} | ||
else if (inRate == 8000 && outRate == 48000) | ||
{ | ||
// Crude up-sample to 48Khz by 6x each sample. This sounds bad, use for testing only. | ||
return pcm.SelectMany(x => new short[] { x, x, x, x, x, x }).ToArray(); | ||
} | ||
else if (inRate == 16000 && outRate == 8000) | ||
{ | ||
// Crude down-sample to 8Khz by skipping every second sample. | ||
return pcm.Where((x, i) => i % 2 == 0).ToArray(); | ||
} | ||
else if (inRate == 16000 && outRate == 48000) | ||
{ | ||
// Crude up-sample to 48Khz by 3x each sample. This sounds bad, use for testing only. | ||
return pcm.SelectMany(x => new short[] { x, x, x }).ToArray(); | ||
} | ||
else | ||
{ | ||
throw new ApplicationException($"Sorry don't know how to re-sample PCM from {inRate} to {outRate}. Pull requests welcome!"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters