I am a Software Development Engineer in Test working for the Windows Sound team. You can contact me via email: mateer at microsoft dot com
Friend key: 28904932216450_59cd9d55374be03d8167d37c8ff4196b
A while ago I wrote a post on Implementing a "say" command using ISpVoice from the Microsoft Speech API which showed how to use Speech API to do text-to-speech, but was limited to playing the generated audio out of the default audio device.
Recently on the Windows Pro Audio forums, user falven asked a question about how to grab the output of the text-to-speech engine as a stream for further processing.
Here's how to do it.
The key part is to use ISpStream::BindToFile to save the audio data to a .wav file, and ISpStream::SetBaseStream to save to a given IStream. Then call ISpVoice::SetOutput with the ISpStream, prior to calling ISpVoice::Speak.
ISpStream *pSpStream = nullptr; hr = CoCreateInstance( CLSID_SpStream, nullptr, CLSCTX_ALL, __uuidof(ISpStream), (void**)&pSpStream ); if (FAILED(hr)) { ERR(L"CoCreateInstance(ISpVoice) failed: hr = 0x%08x", hr); return -__LINE__; } ReleaseOnExit rSpStream(pSpStream); if (File == where) { hr = pSpStream->BindToFile( file, SPFM_CREATE_ALWAYS, &SPDFID_WaveFormatEx, &fmt, 0 ); if (FAILED(hr)) { ERR(L"ISpStream::BindToFile failed: hr = 0x%08x", hr); return -__LINE__; } } else { // stream pStream = SHCreateMemStream(NULL, 0); if (nullptr == pStream) { ERR(L"SHCreateMemStream failed"); return -__LINE__; } hr = pSpStream->SetBaseStream( pStream, SPDFID_WaveFormatEx, &fmt ); if (FAILED(hr)) { ERR(L"ISpStream::SetBaseStream failed: hr = 0x%08x", hr); return -__LINE__; } } hr = pSpVoice->SetOutput(pSpStream, TRUE); if (FAILED(hr)) { ERR(L"ISpVoice::SetOutput failed: hr = 0x%08x", hr); return -__LINE__; }
Updated source and binaries attached.
Usage:
>say.exesay "phrase" [--file <filename> | --stream]runs phrase through text-to-speech engineif --file is specified, writes to .wav fileif --stream is specified, captures to a streamif neither is specified, plays to default output
Here's how to generate a .wav file (uh.wav attached)
>say.exe "uh" --file uh.wavStream is 1
And here's how to generate an output stream. The app consumes this and prints the INT16 sample values to the console. uh.txt attached.
>say.exe "uh" --streamStream is 1 0 0; 0 0; 0 0; 0 0 0 0; 0 0; 0 0; 0 0... 86 86; -1052 -1052; -2839 -2839; -3774 -3774 -4199 -4199; -4581 -4581; -4284 -4284; -3640 -3640 -3100 -3100; -2011 -2011; -393 -393; 533 533...
Stamatis Pap asked in a forum thread how to use a Speech API ISpVoice with a non-default audio device. This MSDN article shows how to use SpEnumTokens to list all the currently active audio outputs, but the number and order of audio outputs is subject to change as things come and go, or as the default audio device changes.
I spent some time poking around the Speech API documentation and discovered that each audio output object has a DeviceId string value which is the WASAPI endpoint ID; this is the way to recognize a given audio output rather than relying on enumeration order.
As part of figuring this out, as a side effect I created a command-line tool to dump all the speech objects and all of their properties.
Source and binaries attached.
Pseudocode:
Here's the output on my system. Note the audio output has a DeviceId string value which matches the WASAPI endpoint ID.
>speech-attributes.exe
-- SPCAT_AUDIOOUT -- #1: [[Speakers] ([High Definition Audio Device])] Attributes Vendor = Microsoft Technology = MMSys (default) = [[Speakers] ([High Definition Audio Device])] CLSID = {A8C680EB-3D32-11D2-9EE7-00C04F797396} DeviceName = [[Speakers] ([High Definition Audio Device])] DeviceId = {0.0.0.00000000}.{c2cbdacb-a70d-4629-8368-542a00f5a4b0}
-- SPCAT_AUDIOIN --
-- SPCAT_VOICES -- #1: Microsoft Zira Desktop - English (United States) Attributes Version = 10.4 Language = 409 Gender = Female Age = Adult SharedPronunciation = Name = Microsoft Zira Desktop Vendor = Microsoft (default) = Microsoft Zira Desktop - English (United States) LangDataPath = C:\Windows\Speech\Engines\TTS\en-US\MSTTSLocEnUS.dat VoicePath = C:\Windows\Speech\Engines\TTS\en-US\M1033ZIR 409 = Microsoft Zira Desktop - English (United States) CLSID = {C64501F6-E6E6-451f-A150-25D0839BC510}
-- SPCAT_RECOGNIZERS --
-- SPCAT_APPLEXICONS --
-- SPCAT_PHONECONVERTERS -- #1: Simplified Chinese Phone Converter Attributes Language = 804 (default) = Simplified Chinese Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8} #2: English Phone Converter Attributes Language = 409 (default) = English Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8} #3: French Phone Converter Attributes Language = 40C (default) = French Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8} #4: German Phone Converter Attributes Language = 407 (default) = German Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8} #5: Japanese Phone Converter Attributes Language = 411 NumericPhones = NoDelimiter = (default) = Japanese Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8} #6: Spanish Phone Converter Attributes Language = 40A;C0A (default) = Spanish Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8} #7: Traditional Chinese Phone Converter Attributes Language = 404 NumericPhones = NoDelimiter = (default) = Traditional Chinese Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8} #8: Universal Phone Converter Attributes Language = (lengthy value redacted) (default) = Universal Phone Converter PhoneMap = (lengthy value redacted) CLSID = {9185F743-1143-4C28-86B5-BFF14F20E5C8}
-- SPCAT_RECOPROFILES -- None found.
I had a need to write a script that would give me a random first name. I grabbed the top 200 first names for baby boys in the US from 2000-2009, and the same list for baby girls:
http://www.ssa.gov/OACT/babynames/decades/names2000s.html
My initial implementation just printed out the name, but I quickly realized I needed to print out the gender if I wanted to talk about what the (fictitious) person did. So I updated it to print out the gender as well.
In the course of this I realized that some names appeared on both lists. In particular they are:
The script is called like this:
>perl -w name.plWesley (male)
And here's the source:
use strict;
# prints a randomly chosen name
sub read_words();
my @words = read_words();print $words[ rand(@words) ];
sub read_words() { my @words = <DATA>;
chomp @words;
return @words;}__DATA__Aaliyah (female)Aaron (male)Abby (female)Abigail (female)Abraham (male)Adam (male)Addison (female)Adrian (male)Adriana (female)Adrianna (female)Aidan (male)Aiden (male)Alan (male)Alana (female)Alejandro (male)Alex (male)Alexa (female)Alexander (male)Alexandra (female)Alexandria (female)Alexia (female)Alexis (female)Alexis (male)Alicia (female)Allison (female)Alondra (female)Alyssa (female)Amanda (female)Amber (female)Amelia (female)Amy (female)Ana (female)Andrea (female)Andres (male)Andrew (male)Angel (female)Angel (male)Angela (female)Angelica (female)Angelina (female)Anna (female)Anthony (male)Antonio (male)Ariana (female)Arianna (female)Ashley (female)Ashlyn (female)Ashton (male)Aubrey (female)Audrey (female)Austin (male)Autumn (female)Ava (female)Avery (female)Ayden (male)Bailey (female)Benjamin (male)Bianca (female)Blake (male)Braden (male)Bradley (male)Brady (male)Brandon (male)Brayden (male)Breanna (female)Brendan (male)Brian (male)Briana (female)Brianna (female)Brittany (female)Brody (male)Brooke (female)Brooklyn (female)Bryan (male)Bryce (male)Bryson (male)Caden (male)Caitlin (female)Caitlyn (female)Caleb (male)Cameron (male)Camila (female)Carlos (male)Caroline (female)Carson (male)Carter (male)Cassandra (female)Cassidy (female)Catherine (female)Cesar (male)Charles (male)Charlotte (female)Chase (male)Chelsea (female)Cheyenne (female)Chloe (female)Christian (male)Christina (female)Christopher (male)Claire (female)Cody (male)Colby (male)Cole (male)Colin (male)Collin (male)Colton (male)Conner (male)Connor (male)Cooper (male)Courtney (female)Cristian (male)Crystal (female)Daisy (female)Dakota (male)Dalton (male)Damian (male)Daniel (male)Daniela (female)Danielle (female)David (male)Delaney (female)Derek (male)Destiny (female)Devin (male)Devon (male)Diana (female)Diego (male)Dominic (male)Donovan (male)Dylan (male)Edgar (male)Eduardo (male)Edward (male)Edwin (male)Eli (male)Elias (male)Elijah (male)Elizabeth (female)Ella (female)Ellie (female)Emily (female)Emma (female)Emmanuel (male)Eric (male)Erica (female)Erick (male)Erik (male)Erin (female)Ethan (male)Eva (female)Evan (male)Evelyn (female)Faith (female)Fernando (male)Francisco (male)Gabriel (male)Gabriela (female)Gabriella (female)Gabrielle (female)Gage (male)Garrett (male)Gavin (male)Genesis (female)George (male)Gianna (female)Giovanni (male)Giselle (female)Grace (female)Gracie (female)Grant (male)Gregory (male)Hailey (female)Haley (female)Hannah (female)Hayden (male)Hector (male)Henry (male)Hope (female)Hunter (male)Ian (male)Isaac (male)Isabel (female)Isabella (female)Isabelle (female)Isaiah (male)Ivan (male)Jack (male)Jackson (male)Jacob (male)Jacqueline (female)Jada (female)Jade (female)Jaden (male)Jake (male)Jalen (male)James (male)Jared (male)Jasmin (female)Jasmine (female)Jason (male)Javier (male)Jayden (male)Jayla (female)Jazmin (female)Jeffrey (male)Jenna (female)Jennifer (female)Jeremiah (male)Jeremy (male)Jesse (male)Jessica (female)Jesus (male)Jillian (female)Jocelyn (female)Joel (male)John (male)Johnathan (male)Jonah (male)Jonathan (male)Jordan (female)Jordan (male)Jordyn (female)Jorge (male)Jose (male)Joseph (male)Joshua (male)Josiah (male)Juan (male)Julia (female)Julian (male)Juliana (female)Justin (male)Kaden (male)Kaitlyn (female)Kaleb (male)Karen (female)Karina (female)Kate (female)Katelyn (female)Katherine (female)Kathryn (female)Katie (female)Kayla (female)Kaylee (female)Kelly (female)Kelsey (female)Kendall (female)Kennedy (female)Kenneth (male)Kevin (male)Kiara (female)Kimberly (female)Kyle (male)Kylee (female)Kylie (female)Landon (male)Laura (female)Lauren (female)Layla (female)Leah (female)Leonardo (male)Leslie (female)Levi (male)Liam (male)Liliana (female)Lillian (female)Lilly (female)Lily (female)Lindsey (female)Logan (male)Lucas (male)Lucy (female)Luis (male)Luke (male)Lydia (female)Mackenzie (female)Madeline (female)Madelyn (female)Madison (female)Makayla (female)Makenzie (female)Malachi (male)Manuel (male)Marco (male)Marcus (male)Margaret (female)Maria (female)Mariah (female)Mario (male)Marissa (female)Mark (male)Martin (male)Mary (female)Mason (male)Matthew (male)Max (male)Maxwell (male)Maya (female)Mckenzie (female)Megan (female)Melanie (female)Melissa (female)Mia (female)Micah (male)Michael (male)Michelle (female)Miguel (male)Mikayla (female)Miranda (female)Molly (female)Morgan (female)Mya (female)Naomi (female)Natalia (female)Natalie (female)Nathan (male)Nathaniel (male)Nevaeh (female)Nicholas (male)Nicolas (male)Nicole (female)Noah (male)Nolan (male)Oliver (male)Olivia (female)Omar (male)Oscar (male)Owen (male)Paige (female)Parker (male)Patrick (male)Paul (male)Payton (female)Peter (male)Peyton (female)Peyton (male)Preston (male)Rachel (female)Raymond (male)Reagan (female)Rebecca (female)Ricardo (male)Richard (male)Riley (female)Riley (male)Robert (male)Ruby (female)Ryan (male)Rylee (female)Sabrina (female)Sadie (female)Samantha (female)Samuel (male)Sara (female)Sarah (female)Savannah (female)Sean (male)Sebastian (male)Serenity (female)Sergio (male)Seth (male)Shane (male)Shawn (male)Shelby (female)Sierra (female)Skylar (female)Sofia (female)Sophia (female)Sophie (female)Spencer (male)Stephanie (female)Stephen (male)Steven (male)Summer (female)Sydney (female)Tanner (male)Taylor (female)Thomas (male)Tiffany (female)Timothy (male)Travis (male)Trenton (male)Trevor (male)Trinity (female)Tristan (male)Tyler (male)Valeria (female)Valerie (female)Vanessa (female)Veronica (female)Victor (male)Victoria (female)Vincent (male)Wesley (male)William (male)Wyatt (male)Xavier (male)Zachary (male)Zoe (female)Zoey (female)
I wrote a selfhost tool which allows me to add a folder (for example, C:\music) to a shell library (for example, the Music library.)
This was before I found out about the shlib shell library sample which Raymond Chen blogged about. If you're looking for a sample on how to manipulate shell libraries, prefer that one to this.
CoInitializepShellLibrary = SHLoadLibraryFromKnownFolder(library GUID)SHAddFolderPathToLibrary(pShellLibrary, path)pShellLibrary->Commit()CoUninitialize
>shelllibraryshelllibrary add <path> to <library> <path> must already exist <library> must be one of: documents music pictures videos recorded tv>shelllibrary add C:\music to MusicAdded C:\music to Music library
About a year ago I wrote about how to change the desktop wallpaper using SystemParametersInfo(SPI_SETDESKWALLPAPER).
Windows 8 desktop apps (not Store apps) can use the new IDesktopWallpaper API to get a more fine level of control. So I wrote an app which uses the new API, though I just set the background on all monitors to the same image path, and I don't exercise any of the advanced features of the API.
CoInitializeCoCreateInstance(DesktopWallpaper)pDesktopWallpaper->SetWallpaper(NULL, full-path-to-image-file)pDesktopWallpaper->Release()CoUninitialize
>desktopwallpaper.exe "%userprofile%\pictures\theda-bara.bmp"Setting the desktop wallpaper to C:\Users\MatEer\pictures\theda-bara.bmp succeeded.
Source and binaries attached
Last time I blogged about an O(n log n) solution to finding the longest duplicated substring in a given piece of text; I have since found an O(n) algorithm, which I linked to in the comments.
But my blog post used an O(n2) algorithm to read the text from STDIN! It looked something like this:
while (!done) { grab 2 KB of text allocate a new buffer which is 2 KB bigger copy the old text and the new text together into the new buffer free the old buffer}
There are two better algorithms:
while (!done) { grab an amount of text equal to the amount we've grabbed so far allocate a new buffer which is twice as large as the last buffer copy the old text and the new text together into the new buffer free the old buffer}
And:
while (!done) { grab 2 KB of text add this to the end of a linked list of text chunks}
allocate a buffer whose size is the total size of all the chunks added togetherwalk the linked list and copy the text of each chunk into the buffer
Both "better" algorithms are O(n) but the latter wastes less space.
Here's the improved code:
struct Chunk { WCHAR text[1024]; Chunk *next; Chunk() : next(nullptr) { text[0] = L'\0'; }};
class DeleteChunksOnExit {public: DeleteChunksOnExit() : m_p(nullptr) {} ~DeleteChunksOnExit() { Chunk *here = m_p; while (here) { Chunk *next = here->next; delete here; here = next; } } void Set(Chunk *p) { m_p = p; } private: Chunk *m_p;};
...
LPWSTR ReadFromStdIn() { Chunk *head = nullptr; Chunk *tail = nullptr;
DeleteChunksOnExit dcoe; size_t total_length = 0; bool done = false; while (!done) { Chunk *buffer = new Chunk(); if (nullptr == buffer) { LOG(L"Could not allocate memory for buffer"); return nullptr; } if (nullptr == head) { // this runs on the first pass only head = buffer; tail = buffer; dcoe.Set(buffer); } else { tail->next = buffer; tail = buffer; } if (fgetws(buffer->text, ARRAYSIZE(buffer->text), stdin)) { total_length += wcslen(buffer->text); } else if (feof(stdin)) { done = true; } else { LOG(L"Error reading from STDIN"); return nullptr; } } // gather all the allocations into a single string size_t size = total_length + 1; WCHAR *text = new WCHAR[size]; if (nullptr == text) { LOG(L"Could not allocate memory for text"); return nullptr; } DeleteArrayOnExit<WCHAR> deleteText(text); WCHAR *temp = text; for (Chunk *here = head; here; here = here->next) { if (wcscpy_s(temp, size, here->text)) { LOG(L"wcscpy_s returned an error"); return nullptr; } size_t len = wcslen(here->text); temp += len; size -= len; } deleteText.Cancel(); return text;}
I'm reading Jon Bentley's Programming Pearls and one of the interesting exercises was to find the longest substring which occurs twice in a given string of length n.
There's a naïve solution where you look at every pair of (distinct) indexes (i, j), and calculate the length of the common prefix of the substrings starting at those locations; this is O(n2) assuming that the length of the eventual maximum substring is O(1) (that is, << n.)
Jon shows that there is an O(n log n) algorithm, which is a significant savings if n is large (e.g., War and Peace.) This involves building an array of all substrings, then sorting them lexically, then walking the sorted array; the trick is that now we only need to check pairs of indexes that correspond to adjacent entries in the array. That step can be done in O(n) time; the O(n log n) comes from the sorting step.
I wrote up a quick app that implements his suggested algorithm. Source follows.
// main.cpp
#include <windows.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <search.h>
#define LOG(format, ...) wprintf(format L"\n", __VA_ARGS__)
template<typename T>class DeleteArrayOnExit {public: DeleteArrayOnExit(T *p) : m_p(p), m_canceled(false) {} ~DeleteArrayOnExit() { if (!m_canceled) { delete [] m_p; } } void Cancel() { m_canceled = true; } void Swap(T *p) { m_p = p; }private: T *m_p; bool m_canceled;};
// caller must delete [] the buffer when done with itLPWSTR ReadFromStdIn();int __cdecl pwcscmp(const void *pstra, const void *pstrb) { return wcscmp(*(LPWSTR*)pstra, *(LPWSTR*)pstrb);}
// returns length of longest common substring// don't include the null terminator if both strings are identical// e.g., comlen(banana, bananas) == comlen(banana, banana) == 6int comlen(LPCWSTR a, LPCWSTR b) { int i = 0;
// keep going while the strings are the same and not ended while (a[i] && (a[i] == b[i])) { i++; }
return i;}
int _cdecl wmain() { LPWSTR text = ReadFromStdIn(); if (nullptr == text) { return -__LINE__; } DeleteArrayOnExit<WCHAR> deleteText(text); size_t size = wcslen(text) + 1; LPWSTR *suffixes = new LPWSTR[size]; if (nullptr == suffixes) { LOG(L"Could not allocate memory for suffixes"); return -__LINE__; } DeleteArrayOnExit<LPWSTR> deleteSuffixes(suffixes); for (size_t i = 0; i < size; i++) { suffixes[i] = &text[i]; } qsort(suffixes, size, sizeof(LPWSTR), pwcscmp); // find the longest common adjacent pair LPWSTR szMax = suffixes[0]; size_t lenMax = 0; for (size_t i = 0; i < size - 1; i++) { size_t len = comlen(suffixes[i], suffixes[i + 1]); if (len > lenMax) { lenMax = len; szMax = suffixes[i]; } } WCHAR *substring = new WCHAR[lenMax + 1]; if (nullptr == substring) { LOG(L"Could not allocate memory for substring"); return -__LINE__; } DeleteArrayOnExit<WCHAR> deleteSubstring(substring); if (0 != wcsncpy_s(substring, lenMax + 1, szMax, lenMax)) { LOG(L"wcsncpy_s failed"); return -__LINE__; } substring[lenMax] = L'\0'; // intentionally not using LOG to avoid trailing newline wprintf(L"%s", substring); return 0;}
LPWSTR ReadFromStdIn() { WCHAR *text = new WCHAR[1]; if (nullptr == text) { LOG(L"Could not allocate memory for text"); return nullptr; } DeleteArrayOnExit<WCHAR> deleteText(text); text[0] = L'\0';
// read a 2 KB chunk const size_t buffer_size = 1024; WCHAR *buffer = new WCHAR[buffer_size]; if (nullptr == buffer) { LOG(L"Could not allocate memory for buffer"); return nullptr; } DeleteArrayOnExit<WCHAR> deleteBuffer(buffer); bool done = false; do { if (fgetws(buffer, buffer_size, stdin)) { size_t size = wcslen(text) + wcslen(buffer) + 1; WCHAR *new_text = new WCHAR[size]; if (nullptr == new_text) { LOG(L"Could not allocate memory for new text"); return nullptr; } DeleteArrayOnExit<WCHAR> deleteNewText(new_text); WCHAR *dest = new_text; if (0 != wcscpy_s(dest, size, text)) { LOG(L"wcscpy_s failed"); return nullptr; } dest += wcslen(text); size -= wcslen(text); if (0 != wcscpy_s(dest, size, buffer)) { LOG(L"wcscpy_s failed"); return nullptr; }
// that should do it for copying // now swap new_text => text delete [] text; text = new_text;
deleteText.Swap(new_text); deleteNewText.Cancel(); } else if (feof(stdin)) { done = true; } else { LOG(L"Error reading from STDIN"); return nullptr; } } while (!done); deleteText.Cancel(); return text;}
The WinMM multimedia APIs include an API for enumerating and controlling all the paths through the audio device; things like bass boost, treble control, pass-through audio from your CD player to your headphones, etc. This is called the "mixer" API and is the forerunner of the IDeviceTopology API.
I wrote a quick app to enumerate all the mixer devices on the system; for each mixer device, enumerate each mixer line (that is, each source and destination); for each mixer line, enumerate all the controls (volume, mute, equalization, etc.); and for each control, query the associated text (if any) and the current value.
mixerGetNumDevs()for each mixer device mixerGetDevCaps(dev) for each destination (line) on the device mixerGetLineInfo(dest) mixerGetLineControls(dest) for each control on the line if the control supports per-item description mixerGetControlDetails(control, MIXER_GETCONTROLDETAILSF_LISTTEXT) log the per-item description mixerGetControlDetails(control, MIXER_GETCONTROLDETAILSF_VALUE) log the value(s)
>mixerenum.exeMixer devices: 5-- 0: Contoso headset -- Device ID: 0 Manufacturer identifier: 1 Product identifier: 104 Driver version: 6.2 Product name: Contoso headset Support: 0x0 Destinations: 1 -- Destination 0: Master Volume -- Destination: 0 Source: -1 Line ID: 0xffff0000 Status: MIXERLINE_LINEF_ACTIVE (1) User: 0x00000000 Component Type: MIXERLINE_COMPONENTTYPE_DST_HEADPHONES (5) Channels: 1 Connections: 2 Controls: 2 Short name: Volume Long name: Master Volume -- Target: -- Type: MIXERLINE_TARGETTYPE_UNDEFINED (0) Device ID: 0 Manufacturer identifier: 65535 Product identifier: 65535 Driver version: 0.0 Product name: -- Control 1: Mute -- Type: MIXERCONTROL_CONTROLTYPE_MUTE (0x20010002) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Mute Long name: Mute -- Values -- FALSE -- Control 2: Volume -- Type: MIXERCONTROL_CONTROLTYPE_VOLUME (0x50030001) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Volume Long name: Volume -- Values -- 0xffff on a scale of 0x0 to 0xffff-- 1: HDMI Audio (Contoso -- Device ID: 1 Manufacturer identifier: 1 Product identifier: 104 Driver version: 6.2 Product name: HDMI Audio (Contoso Support: 0x0 Destinations: 1 -- Destination 0: Master Volume -- Destination: 0 Source: -1 Line ID: 0xffff0000 Status: MIXERLINE_LINEF_ACTIVE (1) User: 0x00000000 Component Type: MIXERLINE_COMPONENTTYPE_DST_DIGITAL (1) Channels: 1 Connections: 2 Controls: 2 Short name: Volume Long name: Master Volume -- Target: -- Type: MIXERLINE_TARGETTYPE_UNDEFINED (0) Device ID: 0 Manufacturer identifier: 65535 Product identifier: 65535 Driver version: 0.0 Product name: -- Control 1: Mute -- Type: MIXERCONTROL_CONTROLTYPE_MUTE (0x20010002) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Mute Long name: Mute -- Values -- FALSE -- Control 2: Volume -- Type: MIXERCONTROL_CONTROLTYPE_VOLUME (0x50030001) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Volume Long name: Volume -- Values -- 0xffff on a scale of 0x0 to 0xffff-- 2: Speakers (Contoso -- Device ID: 2 Manufacturer identifier: 1 Product identifier: 104 Driver version: 6.2 Product name: Speakers (Contoso Support: 0x0 Destinations: 1 -- Destination 0: Master Volume -- Destination: 0 Source: -1 Line ID: 0xffff0000 Status: MIXERLINE_LINEF_ACTIVE (1) User: 0x00000000 Component Type: MIXERLINE_COMPONENTTYPE_DST_SPEAKERS (4) Channels: 1 Connections: 2 Controls: 2 Short name: Volume Long name: Master Volume -- Target: -- Type: MIXERLINE_TARGETTYPE_UNDEFINED (0) Device ID: 0 Manufacturer identifier: 65535 Product identifier: 65535 Driver version: 0.0 Product name: -- Control 1: Mute -- Type: MIXERCONTROL_CONTROLTYPE_MUTE (0x20010002) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Mute Long name: Mute -- Values -- FALSE -- Control 2: Volume -- Type: MIXERCONTROL_CONTROLTYPE_VOLUME (0x50030001) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Volume Long name: Volume -- Values -- 0xffff on a scale of 0x0 to 0xffff-- 3: Contoso headset -- Device ID: 3 Manufacturer identifier: 1 Product identifier: 104 Driver version: 6.2 Product name: Contoso headset Support: 0x0 Destinations: 1 -- Destination 0: Master Volume -- Destination: 0 Source: -1 Line ID: 0xffff0000 Status: MIXERLINE_LINEF_ACTIVE (1) User: 0x00000000 Component Type: MIXERLINE_COMPONENTTYPE_DST_WAVEIN (7) Channels: 1 Connections: 1 Controls: 2 Short name: Volume Long name: Master Volume -- Target: Contoso headset -- Type: MIXERLINE_TARGETTYPE_WAVEIN (2) Device ID: 0 Manufacturer identifier: 1 Product identifier: 101 Driver version: 6.2 Product name: Contoso headset -- Control 1: Mute -- Type: MIXERCONTROL_CONTROLTYPE_MUTE (0x20010002) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Mute Long name: Mute -- Values -- FALSE -- Control 2: Volume -- Type: MIXERCONTROL_CONTROLTYPE_VOLUME (0x50030001) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Volume Long name: Volume -- Values -- 0xf332 on a scale of 0x0 to 0xffff-- 4: Microphone (Contoso -- Device ID: 4 Manufacturer identifier: 1 Product identifier: 104 Driver version: 6.2 Product name: Microphone (Contoso Support: 0x0 Destinations: 1 -- Destination 0: Master Volume -- Destination: 0 Source: -1 Line ID: 0xffff0000 Status: MIXERLINE_LINEF_ACTIVE (1) User: 0x00000000 Component Type: MIXERLINE_COMPONENTTYPE_DST_WAVEIN (7) Channels: 1 Connections: 1 Controls: 2 Short name: Volume Long name: Master Volume -- Target: Microphone (Contoso -- Type: MIXERLINE_TARGETTYPE_WAVEIN (2) Device ID: 1 Manufacturer identifier: 1 Product identifier: 101 Driver version: 6.2 Product name: Microphone (Contoso -- Control 1: Mute -- Type: MIXERCONTROL_CONTROLTYPE_MUTE (0x20010002) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Mute Long name: Mute -- Values -- FALSE -- Control 2: Volume -- Type: MIXERCONTROL_CONTROLTYPE_VOLUME (0x50030001) Status: MIXERCONTROL_CONTROLF_UNIFORM (0x1) Item count: 0 Short name: Volume Long name: Volume -- Values -- 0xf332 on a scale of 0x0 to 0xffff
In addition to audio playback and recording, Windows Multimedia (WinMM) provides a Musical Instrument Digital Interface (MIDI) API.
Here's how to make a list of all the MIDI devices on the system, their capabilities, and the hardware device interface associated with each of them.
midiInGetNumDevs or midiOutGetNumDevsfor each device midiInGetDevCaps or midiOutGetDevCaps log device capabilities midiInMessage or midiOutMessage with DRV_QUERYDEVICEINTERFACESIZE and DRV_QUERYDEVICEINTERFACE log the device interface
Output:
>midienum.exemidiIn devices: 1-- 0: USB2.0 MIDI Device -- Device ID: 0 Manufacturer identifier: 65535 Product identifier: 65535 Driver version: 1.6 Product name: USB2.0 MIDI Device Support: 0x0 Device interface: "\\?\usb#vid_xxxx&pid_yyyy&..."midiOut devices: 2-- 0: Microsoft GS Wavetable Synth -- Device ID: 0 Manufacturer identifier: 1 Product identifier: 27 Driver version: 1.0 Product name: Microsoft GS Wavetable Synth Technology: 7 (MOD_SWSYNTH) Voices: 32 Notes: 32 Channel mask: 0xffff Support: 0x1 MIDICAPS_VOLUME Device interface: ""-- 1: USB2.0 MIDI Device -- Device ID: 1 Manufacturer identifier: 65535 Product identifier: 65535 Driver version: 1.6 Product name: USB2.0 MIDI Device Technology: 1 (MOD_MIDIPORT) Voices: 0 Notes: 0 Channel mask: 0xffff Support: 0x0 Device interface: "\\?\usb#vid_xxxx&pid_yyyy&..."
(Actual device interface string suppressed.)
Note the Microsoft GS Wavetable Synth device, which is always present.
Why would you want to know the device interface? In our case, because we want to test all the audio-related interfaces of a particular device on the system.
Earlier today I posted a quick "say.exe" sample app which you give text and it speaks the text aloud using the text-to-speech part of the Windows Speech API. It was very straightforward - only 67 lines of C++ code.
It took me a little longer to figure out how to do this "listen.exe" sample app; you run it, speak into the microphone, and it uses the speech-to-text part of the Windows Speech API to print what you're saying to the console. This is a little more involved: 202 lines of C++ code.
CoInitialize()CoCreateInstance(ISpRecoContext)pSpRecoContext->SetInterest(recognition events only, thanks)pSpRecoContext->CreateGrammar()pSpRecoGrammar->LoadDictation()pSpRecoGrammar->SetDictationState(active)while(...) { wait for a speech event (or the user to press Enter) pSpRecoContext->GetEvents() for each speech event { make sure SPEVENT.eEventId is SPEI_RECOGNITION event.lParam is an ISpRecoResult pSpRecoResult->GetText() print the text }}
>listen.exeSpeak into the microphone naturally; I will print what I understand.Press ENTER to quit.(At this point you start talking into the microphone. Text shows up here shortly after you say it.)
I've known for a while that Microsoft Windows comes with text-to-speech and speech-to-text APIs, which power the Narrator and Speech Recognition features respectively.
This forum post prompted me to mess around with them a little.
I came up with this implementation of a say.exe command which takes a single argument as text, and then uses the ISpVoice text-to-speech API to have the computer speak it aloud.
CoInitialize(nullptr);CoCreateInstance(ISpVoice)pSpVoice->Speak(text);
>say.exe "Daisy, Daisy; give me your answer, do."
More information on the Speech APIs available here: http://msdn.microsoft.com/en-us/library/ms723627(v=vs.85).aspx
I have a selfhost tool that I use to mute all audio outputs programmatically.
IMMDeviceEnumerator::EnumAudioEndpointsfor each device: IMMDevice::Activate(IAudioEndpointVolume) IAudioEndpointVolume::SetMute(TRUE)
The Windows Vista volume mixer shows a peak meter for the device. In Windows 7 we added a peak meter for each application.
The audio interface for both is IAudioMeterInformation; I've used this before in my post about the linearity of Windows volume APIs. This post showed how an application can get the peak meter reading for the device meter.
In Windows 7 we also added APIs to allow applications to get a list of audio sessions. I wrote up a quick app which shows how to get a list of all audio sessions, filter it down to the active ones (sessions which have an active audio client), and then get a peak meter reading.
>meters.exe{0.0.0.00000000}.{c05f2f54-7294-422a-bb0d-8d690c365b73}|#%b{917F8618-9C57-4720-9B7C-88CA45FC983B}: 0.215705Active sessions: 1
{0.0.0.00000000}.{c05f2f54-7294-422a-bb0d-8d690c365b73}|#%b{917F8618-9C57-4720-9B7C-88CA45FC983B} is the session identifier, which should be considered opaque. 0.215705 is the value of the peak meter, ranging from 0 to 1 linearly in amplitude. If you are populating a visual peak meter with this information, you will need to apply a curve.
Source and binaries attached. Pseudocode follows:
CoCreate(IMMDeviceEnumerator);IMMDeviceEnumerator::GetDefaultAudioEndpoint;IMMDevice::Activate(IAudioSessionManager2);IAudioSessionManager2::GetSessionEnumerator;for (each session) { IAudioSessionEnumerator::GetSession IAudioSessionControl::GetState if the state is anything but "active", skip to the next session QI IAudioSessionControl to IAudioSessionControl2 IAudioSessionControl2::GetSessionIdentifier QI IAudioSessionControl to IAudioMeterInformation IAudioMeterInformation::GetPeakValue Log the session identifier and the peak value}
Last Friday the Windows Sound test team went kayaking. We went to the Agua Verde paddle club and kayaked around Union Bay for a while.
Here's the route we took:
More detail:
http://connect.garmin.com/activity/179545084
Most of my test machines and my laptop have a single display; but I have two dev machines which are each connected to two displays.
When I clean install Windows, I sometimes need to rearrange the displays:
Since I clean install Windows frequently, I wrote myself a little C++ app which does this programmatically using EnumDisplayDevices / EnumDisplaySettings / ChangeDisplaySettingsEx.
for (each device returned by EnumDisplayDevices) {
grab the position and the height/width using EnumDisplaySettings
}
calculate the desired position of the secondary monitor
Set it using ChangeDisplaySettingsEx with DM_POSITION
Call as:
>swapmonitorsMoved secondary monitor to (1920, 0)
EDIT: Oops: 0 should be CDS_GLOBAL | CDS_UPDATEREGISTRY, to make the settings apply to all users, and to persist across display resets / reboots
LONG status = ChangeDisplaySettingsEx( nameSecondary, &mode, nullptr, // reserved CDS_GLOBAL | CDS_UPDATEREGISTRY, // was 0 nullptr // no video parameter);
In an earlier post I mentioned how the Cavendish experiment allowed us to weigh the Earth - to determine the mass of the Earth mE. Newton knew the acceleration due to gravity on the surface of the Earth and was able to use that to find the product G mE; Cavendish determined G directly, and was thus able to solve for mE. He would also have been able to find the mass of the sun as follows:
mE aE = G mE mS / rE2
G, rE, and aE = vE2 / rE are known, so we can solve for mS.
But calculating the mass of the moon is trickier.
Once we were able to put a satellite around the moon we could measure its orbital radius and speed, deduce the acceleration, and use that plus the known G to calculate the mass of the moon. But prior to that we were limited to techniques like:
The moon does not exactly orbit the Earth, but instead orbits the center of mass of the moon/Earth system. By careful observation we can determine where this center of mass is. We can then measure the distance between the center of mass and the Earth's center. This plus the known mass of the Earth and the distance of the Earth from the Moon allows us to determine the mass of the Moon.
If we're lucky enough to see a foreign object come close to the moon, we can determine how much it is accelerated by the Moon. This will allow us to determine the mass of the Moon using the technique above. (We won't be able to determine the mass of the foreign object, but we don't need it.)
When the USSR launched Sputnik, American scientists really wanted to know what its mass was. But because none of the techniques above were useful, they were unable to determine it.
Here are the portions of my unattend.xml file which are needed to turn on Remote Desktop automatically.
This piece flips the "no remote desktop" kill switch to "allow."
<settings pass="specialize"> ... <component name="Microsoft-Windows-TerminalServices-LocalSessionManager" ...> <fDenyTSConnections>false</fDenyTSConnections>
That's not enough; it is also necessary to poke a hole in the firewall to allow inbound connections. I use an indirect string for the Group name, to allow for installing localized builds. This points to the "Remote Desktop" feature group.
<settings pass="specialize"> ... <component name="Networking-MPSSVC-Svc" ...> <FirewallGroups> <FirewallGroup wcm:action="add" wcm:keyValue="RemoteDesktop"> <Active>true</Active> <Profile>all</Profile> <Group>@FirewallAPI.dll,-28752</Group>
If your user account is a member of the "Administrators" group, you're done:
<settings pass="oobeSystem"> <component name="Microsoft-Windows-Shell-Setup" ...> <UserAccounts> <LocalAccounts> <LocalAccount wcm:action="add"> <Password> <Value>#PASSWORD_ADMIN#</Value> <PlainText>true</PlainText> </Password> <Name>Admin</Name> <Group>Administrators</Group>
But if you're like me and you don't want to live in the Administrators group, you need to join the Remote Desktop Users group to be able to log in remotely:
<settings pass="oobeSystem"> <component name="Microsoft-Windows-Shell-Setup" ...> <UserAccounts> <DomainAccounts> <DomainAccountList wcm:action="add"> <Domain>redmond.corp.microsoft.com</Domain> <DomainAccount wcm:action="add"> <Group>RemoteDesktopUsers</Group> <Name>MatEer</Name>
This shows how to call waveInGetNumDevs, waveInGetDevCaps, waveOutGetNumDevs, and waveOutGetDevCaps.
#include <windows.h>#include <mmsystem.h>#include <stdio.h>
int _cdecl wmain() {
UINT devs = waveInGetNumDevs(); LOG(L"waveIn devices: %u", devs); for (UINT dev = 0; dev < devs; dev++) { WAVEINCAPS caps = {}; MMRESULT mmr = waveInGetDevCaps(dev, &caps, sizeof(caps)); if (MMSYSERR_NOERROR != mmr) { LOG(L"waveInGetDevCaps failed: mmr = 0x%08x", mmr); return mmr; } LOG( L"-- waveIn device #%u --\n" L"Manufacturer ID: %u\n" L"Product ID: %u\n" L"Version: %u.%u\n" L"Product Name: %s\n" L"Formats: 0x%x\n" L"Channels: %u\n" L"Reserved: %u\n" , dev, caps.wMid, caps.wPid, caps.vDriverVersion / 256, caps.vDriverVersion % 256, caps.szPname, caps.dwFormats, caps.wChannels, caps.wReserved1 ); }
devs = waveOutGetNumDevs(); LOG(L"waveOut devices: %u", devs); for (UINT dev = 0; dev < devs; dev++) { WAVEOUTCAPS caps = {}; MMRESULT mmr = waveOutGetDevCaps(dev, &caps, sizeof(caps)); if (MMSYSERR_NOERROR != mmr) { LOG(L"waveOutGetDevCaps failed: mmr = 0x%08x", mmr); return mmr; } LOG( L"-- waveOut device #%u --\n" L"Manufacturer ID: %u\n" L"Product ID: %u\n" L"Version: %u.%u\n" L"Product Name: %s\n" L"Formats: 0x%x\n" L"Channels: %u\n" L"Reserved: %u\n" L"Support: 0x%x\n" L"%s%s%s%s%s" , dev, caps.wMid, caps.wPid, caps.vDriverVersion / 256, caps.vDriverVersion % 256, caps.szPname, caps.dwFormats, caps.wChannels, caps.wReserved1, caps.dwSupport, ((caps.dwSupport & WAVECAPS_LRVOLUME) ? L"\tWAVECAPS_LRVOLUME\n" : L""), ((caps.dwSupport & WAVECAPS_PITCH) ? L"\tWAVECAPS_PITCH\n" : L""), ((caps.dwSupport & WAVECAPS_PLAYBACKRATE) ? L"\tWAVECAPS_PLAYBACKRATE\n" : L""), ((caps.dwSupport & WAVECAPS_VOLUME) ? L"\tWAVECAPS_VOLUME\n" : L""), ((caps.dwSupport & WAVECAPS_SAMPLEACCURATE) ? L"\tWAVECAPS_SAMPLEACCURATE\n" : L"") ); }
return 0;}
On my system this outputs:
waveIn devices: 3-- waveIn device #0 --Manufacturer ID: 1Product ID: 65535Version: 0.0Product Name: Microphone (High Definition AudFormats: 0xfffffChannels: 2Reserved: 0
-- waveIn device #1 --Manufacturer ID: 1Product ID: 65535Version: 0.0Product Name: Digital Audio (S/PDIF) (High DeFormats: 0xfffffChannels: 2Reserved: 0
-- waveIn device #2 --Manufacturer ID: 1Product ID: 65535Version: 0.0Product Name: CD Audio (High Definition AudioFormats: 0xfffffChannels: 2Reserved: 0
waveOut devices: 2-- waveOut device #0 --Manufacturer ID: 1Product ID: 65535Version: 0.0Product Name: Headphones (High Definition AudFormats: 0xfffffChannels: 2Reserved: 0Support: 0x2e WAVECAPS_LRVOLUME WAVECAPS_PLAYBACKRATE WAVECAPS_VOLUME WAVECAPS_SAMPLEACCURATE
-- waveOut device #1 --Manufacturer ID: 1Product ID: 65535Version: 0.0Product Name: Digital Audio (S/PDIF) (High DeFormats: 0xfffffChannels: 2Reserved: 0Support: 0x2e WAVECAPS_LRVOLUME WAVECAPS_PLAYBACKRATE WAVECAPS_VOLUME WAVECAPS_SAMPLEACCURATE
As many people, I have to deal with a lot of numbers all the time (bug numbers, changelist numbers, tracking numbers, ...) and as a mathematician I sometimes notice when numbers have peculiar properties.
For example, I recently ran into 152463, which is a straight (made up of consecutive digits, not necessarily in order.) I then ran into 35426, which is also a straight. Is this odd?
To put it another way - what proportion of numbers are straights?
We consider only non-negative integer numbers {0, 1, 2, ... }. No duplicate digits are allowed, so the very last straight is 9,876,543,210.
Break it down by the length of the number in digits. For example, how many four-digit numbers are there? We can choose any number from 1-9 for the first digit, and any number from 0-9 for each of the other three digits; so there are 9 * 103 numbers of length 4.
How many of these are straights? There are seven ways to choose a set of four consecutive digits to make up a four-digit straight: {0,1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}, {4, 5, 6, 7}, {5, 6, 7, 8}, {6, 7, 8, 9}.
Each of these seven ways can then be shuffled to make the straights themselves. For six of these seven sets of digits, all possible shufflings are allowed; there are 4! shufflings for each set. This gives 6 * 4! straights.
But for {0, 1, 2, 3}, we have to be careful not to end up with 0 as a leading digit. We choose the leading digit first out of the set {1, 2, 3} (there are three ways to do this) and then we have complete freedom to shuffle the three remaining digits (including 0.) This gives 3 * 3! straights.
So there are 6 * 4! + 3 * 3! straights which are four digits long.
Considering all possible lengths from 1 to 10 (we'll consider 0 as a number of length 1) gives the following table:
Since roughly 1% of five-digit numbers are straights, it is not surprising that I see them that often.
As a tester on Windows 8 I install Windows on my dev machine very frequently.
I use F12 to boot from the network into WinPE, then I run a .bat file which looks something like this:
@echo offsetlocal enabledelayedexpansionset PASSWORD_ADMIN=(redacted)set PASSWORD_MATEER=(redacted)set LICENSE_KEY=(redacted)set FANCYLANG=qps-plocmdel unattend-processed.xmlfor /f "usebackq delims=" %%l in (`type unattend-template.xml`) do ( set line=%%l set line=!line:#PASSWORD_ADMIN#=%PASSWORD_ADMIN%! set line=!line:#PASSWORD_MATEER#=%PASSWORD_MATEER%! set line=!line:#LICENSE_KEY#=%LICENSE_KEY%! set line=!line:#FANCY_LANG#=%FANCY_LANG%! echo !line! >> unattend-processed.xml)setup.exe /unattend:unattend-processed.xml
This takes my template unattend.xml file, injects various parameters, and calls setup.exe.
The template unattend.xml follows in all of its glory. We'll take a closer look at some of the things in future posts.
<?xml version="1.0" encoding="utf-8"?><unattend xmlns="urn:schemas-microsoft-com:unattend"> <settings pass="windowsPE"> <component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <UserData> <ProductKey> <WillShowUI>OnError</WillShowUI> <Key>#LICENSE_KEY#</Key> </ProductKey> <AcceptEula>true</AcceptEula> <FullName>Matthew van Eerde</FullName> <Organization>Microsoft</Organization> </UserData> <DiskConfiguration> <Disk wcm:action="add"> <CreatePartitions> <CreatePartition wcm:action="add"> <Order>1</Order> <Extend>true</Extend> <Type>Primary</Type> </CreatePartition> </CreatePartitions> <WillWipeDisk>true</WillWipeDisk> <DiskID>0</DiskID> </Disk> </DiskConfiguration> <ImageInstall> <OSImage> <WillShowUI>OnError</WillShowUI> <InstallToAvailablePartition>true</InstallToAvailablePartition> </OSImage> </ImageInstall> </component> <component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <SetupUILanguage> <UILanguage>#FANCY_LANG#</UILanguage> </SetupUILanguage> <InputLocale>0409:00000409</InputLocale> <UserLocale>en-US</UserLocale> <SystemLocale>en-US</SystemLocale> <UILanguage>#FANCY_LANG#</UILanguage> </component> </settings> <settings pass="specialize"> <component name="Microsoft-Windows-UnattendedJoin" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Identification> <Credentials> <Domain>redmond.corp.microsoft.com</Domain> <Password>#PASSWORD_MATEER#</Password> <Username>MatEer</Username> </Credentials> <JoinDomain>ntdev.corp.microsoft.com</JoinDomain> </Identification> </component> <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ComputerName>MATEER-Q</ComputerName> <RegisteredOwner>Matthew van Eerde</RegisteredOwner> <RegisteredOrganization>Microsoft</RegisteredOrganization> </component> <component name="Microsoft-Windows-TerminalServices-LocalSessionManager" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <fDenyTSConnections>false</fDenyTSConnections> </component> <component name="Networking-MPSSVC-Svc" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <FirewallGroups> <FirewallGroup wcm:action="add" wcm:keyValue="RemoteDesktop"> <Active>true</Active> <Profile>all</Profile> <Group>@FirewallAPI.dll,-28752</Group> </FirewallGroup> </FirewallGroups> </component> </settings> <settings pass="oobeSystem"> <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <OOBE> <HideEULAPage>true</HideEULAPage> <NetworkLocation>Work</NetworkLocation> <ProtectYourPC>3</ProtectYourPC> <HideOnlineAccountScreens>true</HideOnlineAccountScreens> <HideLocalAccountScreen>true</HideLocalAccountScreen> <HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE> </OOBE> <UserAccounts> <LocalAccounts> <LocalAccount wcm:action="add"> <Password> <Value>#PASSWORD_ADMIN#</Value> <PlainText>true</PlainText> </Password> <Name>Admin</Name> <Group>Administrators</Group> </LocalAccount> </LocalAccounts> <DomainAccounts> <DomainAccountList wcm:action="add"> <Domain>redmond.corp.microsoft.com</Domain> <DomainAccount wcm:action="add"> <Group>RemoteDesktopUsers</Group> <Name>MatEer</Name> </DomainAccount> </DomainAccountList> </DomainAccounts> </UserAccounts> <TimeZone>Pacific Standard Time</TimeZone> <AutoLogon> <Password> <Value>#PASSWORD_MATEER#</Value> <PlainText>true</PlainText> </Password> <Domain>redmond.corp.microsoft.com</Domain> <Enabled>true</Enabled> <LogonCount>1</LogonCount> <Username>MatEer</Username> </AutoLogon> </component> </settings></unattend>
As a Windows tester, I install Windows on my own machines a lot (this is known internally as "selfhosting", or "dogfooding", or "ice cream-ing".)
One of my little idiosyncracies is I like to run as a non-administrative user. That is, I don't add my domain account to the local Administrators group.
Instead, I create a local "Admin" account with a known (to me) password; every time I need to elevate, I get a prompt that asks for credentials rather than just "Yes/No". To this prompt I pass the credentials of the local "Admin" account.
Although I usually install fresh builds regularly (on my multiple machines), sometimes one machine gets a little stale. In fact, it happened once that my local .\Admin account got so stale that I had to change the password! This was annoying enough that I devoted some energy into figuring out how to check the "Password never expires" box on the local account properties programmatically.
The result was the following script: call as cscript.exe never-expire-admin-password.wsf This version hardcodes the username "Admin"; a production version would probably allow passing a username in via the command line.
If the Admin password already has the box checked, this script does nothing.
' LDAP doesn't work for controlling local users' (unless you're a domain controller, of course)'' have to use WinNT provider instead
Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
' hardcoding "Admin" usernameDim admin: Set admin = GetObject("WinNT://localhost/Admin,user")
WScript.Echo "Admin's userFlags are 0x" & Hex(admin.userFlags)
If Not admin.userFlags And ADS_UF_DONT_EXPIRE_PASSWD Then WScript.Echo "Setting local admin account to never expire password" admin.userFlags = (admin.userFlags Or ADS_UF_DONT_EXPIRE_PASSWD)
' Save admin.SetInfoEnd If
This morning my wife (whom I love and adore) woke me up at 3:00 AM with an urgent question.
"Hey!" she said, shaking me awake.
"Is 19 prime?"
Like a fool, I answered her. "Yes. Yes it is." Off she went.
This is a true response, but not a correct response. I realized shortly afterwards that a correct response would look more like:
I'm glad you asked me that. dear. Eratosthenes, the Greek mathematician, discovered a very efficient way to list primes in about 200 BC that is still in use today. You start by writing out all the numbers from 1 to 19: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19. 1 is a very special number (it's the multiplicative identity, or what algebraists would call a unit) so we put a square around it. The first number we didn't consider was 2, so we circle it - that means it's prime - and then cross out every multiple of 2 after that. Going back, the first number we didn't consider was 3... and so on until we get 1 2 3 X 5 X 7 X X X 11 X 13 X X X 17 X 19. A common optimization is to realize that after circling a prime p, the first number you cross out (that wasn't crossed out before) is always p2, which means that after circling a number you can immediately jump to its square, and also means you can stop crossing out altogether once you hit p2 > N...
This would allow her to fish rather than waking me up when she wanted a fish.
An even better response would have been:
It's funny you've asked me that. Number theorists and cryptanalysts have considered this question for thousands of years. Eratosthenes' method (see above) is a very simple way to find all the primes below a given number, but an efficient way to determine whether a given number is prime was found only very recently.
In practice, the test that is usually used is the randomized version of the Miller-Rabin test. Although this is nondeterministic, it is very fast indeed, and will tell you to a very high degree of certainty whether the given number is prime. This usually suffices.
There is a deterministic version of the Miller-Rabin test too, which is guaranteed to tell you with perfect certainty whether the given number is prime. But it only works if you believe in the generalized Riemann hypothesis. Most mathematicians nowadays believe the hypothesis, but no-one has (yet) been able to prove it.
Amazingly, in 2002 three mathematicians named Manindra Agrawal, Neeraj Kayal, and Nitin Saxena came up with a deterministic, proven, polynomial-time (specifically, polynomial in the number of digits in the input) method for telling whether a given number is prime. This is known as the AKS primality test. The most striking thing about this test is its simplicity - if something this straightforward can be found after thousands of years of looking, what else out there remains to be found?
Such a response would probably prevent her from waking me again for any mathematical problem at all. Boo-ya.
Here's Agrawal, Kayal, and Saxena's "PRIMES is in P" paper.
Here's Yves Gallot's C++ implementation of AKS.
My own Perl implementation follows:
Here's the output when I run it on 19:
And here's the output with a bigger input:
I was watching How the Grinch Stole Christmas and I was struck by this rhyme (from the song "You're a Mean One, Mr. Grinch:")
You're a nauseous super naus!You're a dirty crooked jockey, and you drive a crooked hoss -- Dr. Seuss, How the Grinch Stole Christmas
I tried to see what other particularly excruciating rhymes I could remember. I came up with two:
You know, that little guy, he's got me feeling all contempt-ey:He takes his boat out loaded up and brings it back in empty. -- Phil Vischer, Lyle the Kindly Viking
And then of course:
In short, when I've a smattering of elemental strategy,You'll say a better Major-General had never sat a-gee! -- W. S. Gilbert, The Pirates of Penzance
The dice game Yahtzee takes thirteen turns. Each turn involves rolling a set of five dice (with two possible rerolls, the player having the option to reroll only a subset of the dice), then marking one of thirteen boxes and scoring points according to whether the numbers on the dice meet the rules of the box. After each box is marked, the game is over.
The thirteen boxes and their rules are:
There are two bonuses possible:
So a perfect Yahtzee game looks like this. On the first turn...
... then on the following turns, in no particular order (but see below:)
Now we look at the bonuses and add it all up:
So a perfect score in Yahtzee is 1480 or 1505.
Well...
... not quite. There's another rule about Yahtzees (the "Joker" rule) which I didn't tell you about yet, because it's kind of complicated. If...
... you can score this roll in any unchecked box and it automatically meets the scoring criterion (wow!) So you would get 30 points in SMALL STRAIGHT or 40 points in LARGE STRAIGHT. (Or 25 points in FULL HOUSE.)1
This gives an additional 70 or 95 points, bring the total to 1575. (It also creates additional order dependencies; the boxes for FULL HOUSE, SMALL STRAIGHT, and LARGE STRAIGHT must be checked after the ONES through SIXES box for the corresponding number is checked.)
1 Does five-of-a-kind count as a full house? I'm not sure. Mathematically it would make sense. It turns out not to matter for this calculation because of the Joker rule, but it may matter in an actual game. The official rules say "Score in this box only if the dice show three of one number and two of another", so I guess not, but I would be comfortable adopting a "house rule" which allowed a Yahtzee to count as a "native" full house. Normally a house rule should be agreed upon by all players before the start of the game, but I'm pretty sure it would be possible to convince the other players to let you score your (3 3 3 3 3) in FULL HOUSE while YAHTZEE was still open. ("You want to throw away 25 points? Really? Um, go right ahead...")
2 The lowest possible Yahtzee score is 5: get (1 1 1 1 1) and score it in Chance, then get zeros in all the other boxes.
It's sometimes difficult to explain to people what my job actually is. "I test Windows sound." "Cool. How does that work?"
A product like Windows has a lot of components that interact with each other. If everything works, the user doesn't know that most of these components even exist; everything is invisible and seamless.
Most testing involves the connection ("interface") between two components. "I test APIs." To the uninitiated, this is just a word. It sounds like "I test wakalixes." "You test what, now?"
There are two interfaces which are easier to explain. There's the software-to-hardware interface, where the driver talks to the hardware. "I test the HD Audio, USB Audio, and Bluetooth audio class drivers." "Huh?" "They make the speakers and the microphone work." "Oh, cool. So you sit around and use Skype all day?"
But the easiest of all to explain is the user interface. "I make sure that the Sound Recorder app, the volume slider, and the Sound control panel work." "Oh, that! I had this annoying problem once where..."
What does the test result for an invisible interface look like? A lot of logging. "I expected this call to succeed; it returned this HRESULT." "I poked the hardware like this and got a bluescreen." "There seems to be an infinite loop here." Lots of text.
Boring.
UI testing has logging too. But with UI testing you can also... TAKE PICTURES! A UI bug is a lot easier to understand (triage, and fix) if there's a screenshot attached (preferably with a big red rectangle highlighting the problem.)
It is therefore valuable to have an automatable utility that can take a screenshot and dump it to a file. Here's one I cribbed together from the "Capturing an Image" sample code on http://msdn.microsoft.com/en-us/library/dd183402(v=VS.85).aspx. Source and binaries attached.
This version only captures the main display, and not secondary monitors (if any.)
screen_dc = GetDC(nullptr);memory_dc = CreateCompatibleDC(screen);rect = GetClientRect(GetDesktopWindow());hbmp = CreateCompatibleBitmap(screen_dc, rect);SelectObject(memory_dc, hbmp);BitBlt(memory_dc, rect, screen_dc);bmp = GetObject(hbmp);bytes = allocate enough memorybytes = GetDIBits(screen_dc, bmp, hbmp)file = CreateFile();WriteFile(bitmap header);WriteFile(bytes);