Click here to Skip to main content
16,021,687 members
Home / Discussions / C#
   

C#

 
GeneralRe: Would this be a valid Extension? Pin
charles henington25-Sep-24 17:14
charles henington25-Sep-24 17:14 
AnswerRe: Would this be a valid Extension? Pin
Richard Deeming18-Sep-24 21:40
mveRichard Deeming18-Sep-24 21:40 
GeneralRe: Would this be a valid Extension? Pin
charles henington19-Sep-24 5:19
charles henington19-Sep-24 5:19 
GeneralRe: Would this be a valid Extension? Pin
charles henington25-Sep-24 17:05
charles henington25-Sep-24 17:05 
QuestionVisual Studio Weirdness Pin
Richard Andrew x6415-Sep-24 12:38
professionalRichard Andrew x6415-Sep-24 12:38 
AnswerRe: Visual Studio Weirdness Pin
jschell17-Sep-24 12:37
jschell17-Sep-24 12:37 
GeneralRe: Visual Studio Weirdness Pin
Richard Andrew x6418-Sep-24 11:11
professionalRichard Andrew x6418-Sep-24 11:11 
QuestionConverting ulaw to alaw Pin
charles henington14-Sep-24 14:26
charles henington14-Sep-24 14:26 
I've been trying to convert ulaw to alaw following a few examples that I found online. I'm able to covert the alaw to ulaw quite well but when converting back to alaw the output becomes distorted. If anyone knows a better solution please do share. Here is the code that is producing the distorted alaw.

C#
public static NativePointer Encode(G711.Ulaw ulaw)
{                        
    if (ulaw == null || !ulaw.ContainsAudio()) throw new Exception(nameof(ulaw));            
    List<byte> bytes = [];
    NativePointer pointer = ulaw._nativePointer!;
    byte[] result = new byte[pointer.Size];
    Marshal.Copy(pointer, result, 0, pointer.Size);
    foreach(byte byt in result)
    {
        bytes.Add(LinearToALawSample(MuLawToLinearSample(byt)));
    }
    
    return new(bytes);
    static byte LinearToALawSample(short pcm_val)
    {          

        int mask;
        int seg;
        byte aval;

        if (pcm_val >= 0)
        {
            mask = 0xD5; // sign (7th) bit = 1
        }
        else
        {                    
            mask = 0x55; // sign bit = 0
            pcm_val = (short)-pcm_val;
            if (pcm_val > EncoderInfo.ALAW_MAX)
            {
                pcm_val = EncoderInfo.ALAW_MAX;
            }
        }

        if (pcm_val < 256)
        {
            aval = (byte)(pcm_val >> 4);

        }
        else
        {
            seg = 1;
            for (int i = pcm_val; i > 256; i >>= 1)
            {
                seg++;
            }
            aval = (byte)((seg << EncoderInfo.SEG_SHIFT) | ((pcm_val >> (seg + 3)) & EncoderInfo.QUANT_MASK));
        }

        return (byte)(((aval & EncoderInfo.SEG_MASK) ^ mask) & EncoderInfo.SIGN_BIT);
    }
    static short MuLawToLinearSample(byte muLaw)
    {                
        int sign = (muLaw & EncoderInfo.SIGN_BIT) >> 7;
        int exponent = (muLaw & EncoderInfo.SEG_MASK) >> 4;
        int mantissa = muLaw & 0x0F;
        int sample = ((mantissa << 3) + EncoderInfo.BIAS) << (exponent + 2);
        return (short)(sign == 0 ? sample : -sample);
    }
}


Here is the EncoderInfo class.

C#
internal static class EncoderInfo
{
    public const int BIAS = 0x84;
    public const int SEG_MASK = 0x70;
    public const int SIGN_BIT = 0x80;
    public const int ALAW_MAX = 0xFFF;
    public const int QUANT_MASK = 0xF;
    public const int SEG_SHIFT = 4;
}


modified 14-Sep-24 20:37pm.

AnswerRe: Converting ulaw to alaw Pin
trønderen14-Sep-24 18:01
trønderen14-Sep-24 18:01 
GeneralRe: Converting ulaw to alaw Pin
charles henington15-Sep-24 13:39
charles henington15-Sep-24 13:39 
AnswerRe: Converting ulaw to alaw Pin
Peter_in_278015-Sep-24 14:07
professionalPeter_in_278015-Sep-24 14:07 
AnswerRe: Converting ulaw to alaw Pin
jeron115-Sep-24 14:11
jeron115-Sep-24 14:11 
GeneralRe: Converting ulaw to alaw Pin
charles henington18-Sep-24 16:10
charles henington18-Sep-24 16:10 
Questionhow to get image Thumbnail without open it? Pin
Le@rner10-Sep-24 18:34
Le@rner10-Sep-24 18:34 
AnswerRe: how to get image Thumbnail without open it? Pin
Richard Deeming10-Sep-24 21:28
mveRichard Deeming10-Sep-24 21:28 
AnswerRe: how to get image Thumbnail without open it? Pin
OriginalGriff10-Sep-24 23:05
mveOriginalGriff10-Sep-24 23:05 
AnswerRe: how to get image Thumbnail without open it? Pin
Le@rner10-Sep-24 23:29
Le@rner10-Sep-24 23:29 
GeneralRe: how to get image Thumbnail without open it? Pin
Dave Kreskowiak11-Sep-24 3:17
mveDave Kreskowiak11-Sep-24 3:17 
GeneralRe: how to get image Thumbnail without open it? Pin
OriginalGriff11-Sep-24 4:03
mveOriginalGriff11-Sep-24 4:03 
GeneralRe: how to get image Thumbnail without open it? Pin
lmoelleb12-Sep-24 19:38
lmoelleb12-Sep-24 19:38 
AnswerRe: how to get image Thumbnail without open it? Pin
jschell12-Sep-24 13:08
jschell12-Sep-24 13:08 
Questionhow set cutom paper size? Pin
Le@rner28-Aug-24 0:38
Le@rner28-Aug-24 0:38 
AnswerRe: how set cutom paper size? Pin
RedDk28-Aug-24 9:18
RedDk28-Aug-24 9:18 
Questionbutton event handler where it drops it? Pin
geomeo12326-Aug-24 11:12
geomeo12326-Aug-24 11:12 
AnswerRe: button event handler where it drops it? Pin
OriginalGriff26-Aug-24 19:17
mveOriginalGriff26-Aug-24 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.