ebook img

Audio Programming in C++. The Beginner Level PDF

520 Pages·2022·8.887 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Audio Programming in C++. The Beginner Level

Audio Programming in C++ The Beginner Level Håkan Blomqvist Thisbookisforsaleathttp://leanpub.com/thebeginnerlevelbookinaudioprogramming Thisversionwaspublishedon2022-12-05 ThisisaLeanpubbook.LeanpubempowersauthorsandpublisherswiththeLeanPublishing process.LeanPublishingistheactofpublishinganin-progressebookusinglightweighttoolsand manyiterationstogetreaderfeedback,pivotuntilyouhavetherightbookandbuildtractiononce youdo. ©2021-2022HåkanBlomqvist Dedicatedtoallmyfriendsaroundme.Youknowwhoyouare. Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 WhyC++20?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Sowhatisthedefinitionofsynthesis? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 WhatwasthefirstSoundSynthesizer?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Whatissound? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 WhatisTimbre? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 SynthesisChaptersinthebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 ThePhysicsofstring,windandpercussionInstruments. . . . . . . . . . . . . . . . . . . . . . 5 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Wind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Percussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 AdditiveSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 AdditiveSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 AdditiveSynthesiswithaKAWAIK5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 SubtractiveSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 SubtractiveSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 SubtractiveSynthesiswithaSequentialCircuitsProphet5 . . . . . . . . . . . . . . . . . . . 14 FormantSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 FormantSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 FormantSynthesiswithaYamahaFS1R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 GranularSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 GranularSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 GranularSynthesiswithaPortal byOutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 FrequencyModulation(FM)Synthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 FrequencyModulation(FM)SynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 FrequencyModulation(FM)SynthesiswithaYamahaDX7 . . . . . . . . . . . . . . . . . . . 18 LinearArithmeticSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 LinearArithmeticSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 LinearArithmeticSynthesiswithaRolandD-50 . . . . . . . . . . . . . . . . . . . . . . . . . . 19 CONTENTS PhaseDistortionSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 PhaseDistortionSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 PhaseDistortionSynthesiswithaCasioCZ-1000 . . . . . . . . . . . . . . . . . . . . . . . . . 20 ScannedSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ScannedSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ScannedSynthesiswithaQu-BitScanned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 VectorSynthesis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 VectorSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 VectorSynthesiswith aSequentialCircuitsProphetVS . . . . . . . . . . . . . . . . . . . . . 22 VirtualAnalog(VA)Synthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 VirtualAnalog(VA)SynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 VirtualAnalog(VA)SynthesiswithaAccessVirusTI . . . . . . . . . . . . . . . . . . . . . . 23 WavetableSynthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 WavetableSynthesisinC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 WavetableSynthesiswithaPPGwave2.2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 PhysicalModeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 PhysicalModelinginC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 PhysicalModelingwithaAodyoAnymaPhi . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 IntheRearviewMirror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 AppendixA:C++20Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 AppendixB:AudioEngineeringTerminology(0-9-A-Z). . . . . . . . . . . . . . . . . . . . 394 AppendixC:URLforSourceCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Introduction Welcome to the beginner book about audio Programming. In this book we will get to learn the differenttypesofsoundsynthesisthereare(aleastthepopularones)andgothroughsomeexample code using the different synthesis techniques. All code examples are written using C++20 and Clang++asacompilerandlinker. You’ll find all code examplesin Appendix A and all code will include what to type in a terminal to compileandlinktheprogram.Aftercompilationyoucanruntheprogrambytyping”./”(dotslash) beforethenameoftheprogram.ThisbookassumesthatyoualreadyknowhowtoprograminC++ and what a compiler and linker is. Fokus for the book is about the different synthesis methods and examples of them in C++20. The examples are not optimized for real time playback just to show how the synthesis method works. Therefore the sounds generated will be saved as Wave och Aiff file format. The examples will also be visualized as waveforms in a .png image. Just so that we can bothhearandseetheresult. Although this book is about audio programming in C++20, we suggest getting hardware synths or building hardware synths/modules to understand the software side of things better. Especially for Virtual Analog Synthesis where you emulate/simulate the components in the hardware synths. A modularsynthiswhatIrecommendandIknowthatitcanbeexpansivetobuildyourown.Playing around with software like the free VCV Rack (fromVCV Rack¹) instead of your own modular is an alternative,althoughonlyforthesoundandnotsomuchforthehardware. Why C++20? Well,youcouldprobablycompileandruntheexamplesasC++17.ButasC++20isthelateststandard, thenthatshouldbeused.SowhenC++23isout,thisbookwillbeupdatedtosupportthat,ifneeded. So what is the definition of synthesis? It’sawordusedinjusttwomajorcontexts:thecreationofchemicalcompoundsandproductionof electronicsounds. Therearealargenumberoftypesofsynthesis.LikeTexturesynthesizers,Videosynthesizers,Color synthesizers,andSpeechsynthesizers.Noneofthesewillbediscussedinthisbook. Cambridge Dictionary says regarding Synthesis: “the act of combining different ideas or things to makeawholethatisnewanddifferentfromtheitemsconsideredseparately.” ¹https://vcvrack.com/ Introduction 2 In this book Synthesis is about Sound Synthesis, as in the process of producing sound. Sound wherewecanreuseexistingsoundsbyprocessingthem,orwecangeneratesoundelectronicallyor mechanically.Soundsynthesiscanusemathematics,physicsorevenbiology;anditbringstogether artandscienceinamixofmusicalskillandtechnicalexpertise. What was the first Sound Synthesizer? In1896,Telharmoniumwasrealizedandpatentedin1897.TelharmoniumwasalsocalledDynamo- phoneandusedtonewheelstogeneratemusicalsoundsaselectricalsignalsbyAdditivesynthesis. Inventor behind the Telharmonium is Thaddeus Cahill, the eldest son of Dr. Timothy and Ellen Harrington Cahill, both immigrants from Ireland. Thaddeus was born in Iowa, USA on June 18:th 1867. He studied physics of music at Oberlin Conservatory in Oberlin, Ohio. He graduated from Columbian Law school. Thaddeus started to invent things at age 13 or 14, after Bell Company had refusedtosellhimtelephoneinstrumentstoexperimentwith. At age 18, he had invented and patented mechanism for typewriters. In 1893, at 26 years of age, he was struck by a grand new idea. The idea was to use electrical dynamos to build up complex tones such as instruments. An invention like this would replace the piano, organ, and violin as the preferredparlorinstruments.APianocouldnotbekeptintune.Furthermore,thevolumeofanote orchordwassoonlost.Theorganwasbetterintheserespects,buthadlimitedpowerofexpression. The violin did not posses these problems, but it had little chord capacity. So Cahill’s idea would fix thatandcouldevenhavethepowertosendmusicanywhereelectricitycouldbetransported.Overa networkoftelephonelines,Cahillcouldtransmitmusicfromhiscentralstationtotensofthousands ofplacesatonce. Cahill wanted Telharmonium music to be broadcast into hotels, restaurants, theaters, and even housesviathetelephoneline. He built three versions: the first version weighed 7 tons. The second and third version weighed almost 210 tons. Each was a considerable advancement over the features of its predecessor. Telharmoniumhad aheftypricetagof$200,000(approximately$5,514,000today). PerformancesofTelharmoniumweremadeatthe“TelharmonicHall”,on39:thstreetandBroadway. MarkTwainwasamongtheappreciativeofitsaudiences. The Telharmonium foreshadowed modern electronic musical equipment in a number of ways. For instance, its sound output came in the form of connecting ordinary telephone receivers to large paper cones—a primitive form of loudspeaker. Cahill was noted for saying that electromagnetic diaphragms were the most preferable means of outputting its distinctive sound. There are sadly no knownrecordingsofmusicmadewiththeTelharmonium. Telharmoniumtonesweredescribedas“clearandpure”—referringtotheelectronicsinewavetones itwascapableofproducing.However,itwasnotrestrictedtosuchsimplesounds.Eachtonewheelof the instrument corresponded to a single note, and, to broaden its possibilities, Cahill added several extratonewheelstoaddharmonicstoeachnote.This,combinedwithorgan-likestopsandmultiple Introduction 3 keyboards,aswellasanumberoffootpedals,meantthateverysoundcouldbesculptedandreshaped —theinstrumentwasnotedforitsabilitytoreproducethesoundsofcommonorchestralwoodwind instrumentssuchastheflute,bassoon,clarinet,andalsothecello.TheTelharmonium. Mr.Cahill’sTelharmoniumcompanywasdeclaredbankruptcyin1914. Thaddeus Cahill passed at age 66 (12:th of April, 1934) in New York City, USA. The last Telharmo- niumtobescrappedwasthe7tonversion,in1962. What is sound? A sound is a continuous and regular vibrating of air molecules that is heard when they reach a person’sear. Air molecules are the molecules that make up air, which include particles of oxygen and nitrogen. Airisamateriaandeventhoughitisinvisible,anditisconstantlyeffectingtheworldaroundus. Audioreproductioncanbemadewithspeakersandheadphones.Thesoundmovesinpressurewaves. Whenairmoleculesarecompressedandrarifiedfastenough,wehearitassound.Thefastertheair pressurechanges,thehigherthe“frequency”ofthesoundwehear.Whenaspeakermovesbackand forthitpushesonairmoleculeswhichchangestheairpressureandcreatessoundwaves. Speakers work by converting electrical energy into mechanical energy (motion). The mechanical energycompressesairandconvertsthemotionintosoundenergyorsoundpressurelevel(SPL). When an electric current is sent through a coil of wire, it induces a magnetic field. In speakers, a currentissentthroughthevoicecoilwhichproducesanelectricfieldthatinteractswiththemagnetic field of the permanent magnet attached to the speaker. Like charges repel each other and different charges attract. As an audio signal is sent through the voice coil and the musical waveform moves upanddown,thevoicecoilisattractedandrepelledbythepermanentmagnet. This makes the cone that the voice coil is attached to move back and forth. The back and forth motioncreatespressurewavesintheairthatweperceiveassound. Frequency response is how loud the output of a speaker will be at different frequencies. A typical test for frequency response sends out a sweep of frequencies from the bass to the mid-range, and up to the treble range to see if the sound from the speaker is the same in all these areas. The ideal frequencyresponseforaspeakerisveryflat.Thismeansthespeakerwouldbethesamelevelatlow frequencyasitisinthemid-rangeorhighs. Connectingacomputertoadevicethatisdigitalisquitestraightforward. However, when analog devices are involved (speakers, headphones or microphones), interfacing becomes much more complex. What is needed is a way to electronically translate analog signals intodigital(binary)quantities,andviceversa. For input of a analog signal, an analog-to-digital converter, or ADC is used. For speakers or headphones(ifnotbuiltin)adigital-to-analogconverter,orDAC,isused. Introduction 4 An ADC inputs an analog electrical signal such as voltage or current and outputs a binary number. ADAC,ontheotherhand,inputsabinarynumberandoutputsananalogvoltageorcurrentsignal. What is Timbre? Timbre,alsocalledtimber,qualityofauditorysensationsproducedbythetoneofasoundwave. The timbre of a sound depends on its wave form, which varies with the number of overtones, or harmonics,thatarepresent,theirfrequencies,andtheirrelativeintensities. Inmusictimbreisthecharacteristictonecolorofaninstrumentorvoice,arisingfromreinforcement by individual singers or instruments of different harmonics, or overtones (q.v.), of a fundamental pitch. Extremely nasal timbre thus stresses different overtones than mellow timbre. The timbre of the tuning fork and of the stopped diapason organ pipe is clear and pure because the sound they produceisalmostwithoutovertones.Timbreisdeterminedbyaninstrument’sshape(e.g.,theconical orcylindrical pipe ofa wind instrument),bythe frequency rangewithin which the instrumentcan produceovertones,andbytheenvelope of the instrument’s sound. The timbreof spokenvowelsor ofasingingvoiceismodifiedbyconstrictingoropeningvariouspartsofthevocaltract,suchasthe lips,tongue,orthroat. Synthesis Chapters in the book Thisisabeginnerlevelbookandaimedatthat.Aswithmanythingsinlife—weneedtolearnhow to stand before we can learn how to walk — Learn to walk before we can learn how to run and so forth. AdditiveSynthesis SubtractiveSynthesis FormantSynthesis GranularSynthesis (FM)FrequencyModulationSynthesis LinearArithmeticSynthesis PhaseDistortionSynthesis ScannedSynthesis VectorSynthesis VirtualAnalogSynthesis WavetableSynthesis Physicalmodelling AppendixB:AudioEngineeringTerminology(0-9-A-Z) YouneedtoknowAudioEngineeringTerminologyinordertoknowwhatiswhatwhendeveloping softwareforAudioEngineers. TheBeginnerLevelBookinAudioProgrammingisthefirstbookinaseriesofatleastthreeintotal. ParttwoandthreeareworkinprogressandTBA

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.