question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Long Audio - Python Sample to C Sharp Sample?

See original GitHub issue

I am trying to use the Long Audio API. For some reason, the sample in the documentation is for Python instead of C Sharp. There is a C Sharp sample linked, and I am unable to understand it.

Here is the Python code.


def submit_synthesis():
    region = '<region>'
    key = '<your_key>'
    input_file_path = '<input_file_path>'
    locale = '<locale>'
    url = 'https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis'.format(region)
    header = {
        'Ocp-Apim-Subscription-Key': key
    }

    voice_identities = [
        {
            'voicename': '<voice_name>'
        }
    ]

    payload = {
        'displayname': 'long audio synthesis sample',
        'description': 'sample description',
        'locale': locale,
        'voices': json.dumps(voice_identities),
        'outputformat': 'riff-16khz-16bit-mono-pcm',
        'concatenateresult': True,
    }

    filename = ntpath.basename(input_file_path)
    files = {
        'script': (filename, open(input_file_path, 'rb'), 'text/plain')
    }

    response = requests.post(url, payload, headers=header, files=files)
    print('response.status_code: %d' % response.status_code)
    print(response.headers['Location'])

submit_synthesis()

I dont quiet understand python, but, I recognize the code to some extent. I was able to use this, and refer the linked sample, and came up with the following.

C Sharp Samples : https://github.com/Azure-Samples/Cognitive-Speech-TTS/tree/master/CustomVoice-API-Samples/CSharp


public async Task LongAudioTestOne()
{
    var tempStuff = new ExtraStuff();
    //path for the input text file
    var workingDirectory = Directory.GetCurrentDirectory();
    var inputFileName = "samplefile.txt";
    var tempFileName = Path.Combine(workingDirectory, inputFileName);

    string subscriptionKey = tempStuff.subscriptionKey;

    using (FileStream fstream = new FileStream(tempFileName, FileMode.Open))
    using (var client = new HttpClient())
    using (var content = new MultipartFormDataContent())
    {
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

        content.Add(new StringContent("sample display name"), "displayname");
        content.Add(new StringContent("sample description"), "description");
        content.Add(new StringContent("en-GB"), "locale");
        content.Add(new StringContent("[{\"voiceName\": \"en - GB - LibbyNeural\"}]"), "voices");
        content.Add(new StringContent("riff-16khz-16bit-mono-pcm"), "outputformat");

        //add file
        content.Add(new StreamContent(fstream), inputFileName, inputFileName);

        string url = @"https://centralindia.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis";
        var response = client.PostAsync(url, content).Result;

        if (response.StatusCode != HttpStatusCode.Accepted)
        {
            //APIHelper.PrintErrorMessage(response);
            //return false;
            var something = response.StatusCode;
        }

        var something2 = response.StatusCode;
    }
}

I keep getting BAD REQUEST 400. I wish to know what is the mistake I am making here.

I feel like, this specific section,


    files = {
        'script': (filename, open(input_file_path, 'rb'), 'text/plain')
    }

is where I am losing out.


On a related note, I am curious, why is there isn’t a C # sample code for the same?

The linked sample project is not that easy to use, and it merges two scenarios - training and long audio, into a single function, which is hard to follow.

Or, is this one of those things where, this simply works better on Python?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Ling-Caocommented, Jan 27, 2022

@Jay-study-nildana, Thank you for sharing, I believe it would be helpful too, BTW, maybe the add file part can be removed~

//add file
//content.Add(new StreamContent(fstream), inputFileName, inputFileName);

Thanks, Ling

1reaction
Jay-study-nildanacommented, Jan 26, 2022

@Ling-Cao

It worked like magic! I would not have been successfull without you pointing me in the right direction.

I have put the full code here if someone else comes here looking for this.

Note : This is NOT the most elegant piece of code, but putting here anyway 😃

Placing A Request for Long Audio Service


public async Task LongAudioTestOne()
{
    var tempStuff = new ExtraStuff();
    //path for the input text file
    var workingDirectory = Directory.GetCurrentDirectory();
    var inputFileName = "samplefile.txt";
    var tempFileName = Path.Combine(workingDirectory, inputFileName);

    string subscriptionKey = tempStuff.subscriptionKey;

    using (FileStream fstream = new FileStream(tempFileName, FileMode.Open))
    using (var client = new HttpClient())
    using (var content = new MultipartFormDataContent())
    {
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

        content.Add(new StringContent("sample display name"), "displayname");
        content.Add(new StringContent("sample description"), "description");
        content.Add(new StringContent("en-GB"), "locale");
        content.Add(new StringContent("[{\"voiceName\": \"en-GB-LibbyNeural\"}]"), "voices");
        content.Add(new StringContent("riff-16khz-16bit-mono-pcm"), "outputformat");

        //add file
        //content.Add(new StreamContent(fstream), inputFileName, inputFileName);

        var scriptContent = new StreamContent(fstream);
        scriptContent.Headers.Add("Content-Disposition", $@"form-data; name=""script""; filename=""{inputFileName}""");
        scriptContent.Headers.Add("Content-Type", "text/plain");
        scriptContent.Headers.Add("Content-Length", $"{fstream.Length}");
        content.Add(scriptContent, "script", inputFileName);

        string url = @"https://centralindia.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis";
        var response = client.PostAsync(url, content).Result;

        if (response.StatusCode != HttpStatusCode.Accepted)
        {
            //APIHelper.PrintErrorMessage(response);
            //return false;
            var something = response.StatusCode;
        }

        var something2 = response.StatusCode;
    }
}

Checking for status

public async Task CheckStatus(string tempURLOfStatus)
{
    var tempStuff = new ExtraStuff();
    string subscriptionKey = tempStuff.subscriptionKey;
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

        string url = tempURLOfStatus;
        //var response = client.PostAsync(url, content).Result;
        var response = await client.GetAsync(url);
        var responseStringJSON = await client.GetStringAsync(url);

        if (response.StatusCode != HttpStatusCode.Accepted)
        {
            //APIHelper.PrintErrorMessage(response);
            //return false;
            var something = response.StatusCode;
            var tempJSON = response.ToString();
            string responseBody = await response.Content.ReadAsStringAsync();

            string jsonString = responseBody;
            var longAudioStatus = LongAudioStatus.FromJson(jsonString);
        }

        var something2 = response.StatusCode;
    }
}

Getting the Files

public async Task GetAudioResult(string tempRequestID)
{
    var tempStuff = new ExtraStuff();
    string subscriptionKey = tempStuff.subscriptionKey;
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

        string url = tempRequestID;
        //var response = client.PostAsync(url, content).Result;
        var response = await client.GetAsync(url);
        var responseStringJSON = await client.GetStringAsync(url);

        if (response.StatusCode != HttpStatusCode.Accepted)
        {
            //APIHelper.PrintErrorMessage(response);
            //return false;
            var something = response.StatusCode;
            var tempJSON = response.ToString();
            string responseBody = await response.Content.ReadAsStringAsync();

            string jsonString = responseBody;
            var longAudioStatusFile = LongAudioStatusFile.FromJson(jsonString);
        }

        var something2 = response.StatusCode;
    }
}

Again, not the most elegant code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python | Speech recognition on large audio files
For example, we can take an audio file which is 10 minutes long and split it into 60 chunks each of length 10...
Read more >
Transcribe long audio files into text
This page demonstrates how to transcribe long audio files (longer than 1 minute) to text using the Speech-to-Text API and asynchronous speech recognition....
Read more >
C# create 24/32bit sound wav file
The wBitsPerSample set the bit depth of the generated function, pretty simple till now. Then, running the program, all is working with this ......
Read more >
How to convert audio to different formats using C# and ...
Learn how to use FFmpeg with the FFMpegCore library to convert audio files from one format to another using .NET.
Read more >
long audio c# sample: whow to select standard neural voice
Hello, Docs state that long audio api can use bot public and custom neural voices. when I try to use the batchsyntesis action...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found