• Home
  • 2017
  • 2016
  • 2015
  • 2014
  • 2013
  • 2012
  • 2011

PDI Studio 5

Design to make a difference

Feed on
Posts
comments

Sampler_12bit.ino

Dec 4th, 2017 by patarf

*The following code is from the Audio Hacker Library, downloaded from https://nootropicdesign.com/audiohacker/projects.html

 

 

 

/*
Audio Hacker Library
Copyright (C) 2013 nootropic design, LLC
All rights reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
A 12-bit sampler to record sampled audio to SRAM.
Input is sampled at 44.1 kHz and reproduced on the output.
Recordings sampled at 22 kHz and stored to SRAM.

See Audio Hacker project page for details.
http://nootropicdesign.com/audiohacker/projects.html
*/

#include <AudioHacker.h>

#define DEBUG
#define OFF 0
#define PASSTHROUGH 1
#define RECORD 2
#define PLAYBACK 3
#define RECORD_DONE 4
#define RECORD_BUTTON 5
#define PLAY_BUTTON 6

unsigned int playbackBuf = 2048;
unsigned int passthroughSampleRate;
unsigned int recordingSampleRate;
unsigned int playbackSampleRate;
unsigned int timer1Start;
volatile unsigned int timer1EndEven;
volatile unsigned int timer1EndOdd;
volatile boolean warning = false;
boolean adjustablePlaybackSpeed = false; // set to true with pot connected to A0
int currentA0Position;
volatile long address = 0;
volatile long endAddress = 0;
volatile byte addressChipNumber = 0;
volatile byte endAddressChipNumber = 0;

volatile byte mode = PASSTHROUGH;
unsigned long lastDebugPrint = 0;
unsigned int readBuf[2];
unsigned int writeBuf;
boolean evenCycle = true;
unsigned int recordStartTime;
unsigned int recordEndTime;
boolean sampleRecorded = false;

void setup() {
#ifdef DEBUG
Serial.begin(115200); // connect to the serial port
#endif

recordingSampleRate = DEFAULT_RECORDING_SAMPLE_RATE;
passthroughSampleRate = DEFAULT_SAMPLE_RATE;
timer1Start = UINT16_MAX – (F_CPU / passthroughSampleRate);

pinMode(RECORD_BUTTON, INPUT);
pinMode(PLAY_BUTTON, INPUT);
digitalWrite(RECORD_BUTTON, HIGH);
digitalWrite(PLAY_BUTTON, HIGH);

AudioHacker.begin();

#ifdef DEBUG
Serial.print(“sample rate = “);
Serial.print(passthroughSampleRate);
Serial.print(” Hz, recording sample rate = “);
Serial.print(recordingSampleRate);
Serial.print(” Hz”);
Serial.println();
#endif
}

void loop() {

#ifdef DEBUG
if ((millis() – lastDebugPrint) >= 1000) {
lastDebugPrint = millis();

// Print the number of instruction cycles remaining at the end of the ISR.
// The more work you try to do in the ISR, the lower this number will become.
// If the number of cycles remaining reaches 0, then the ISR will take up
// all the CPU time and the code in loop() will not run.

Serial.print(“even cycles remaining = “);
Serial.print(UINT16_MAX – timer1EndEven);
Serial.print(” odd cycles remaining = “);
Serial.print(UINT16_MAX – timer1EndOdd);
Serial.println();
if (((UINT16_MAX – timer1EndEven) < 20) || (((UINT16_MAX – timer1EndOdd) < 20))) {
Serial.println(“WARNING: ISR execution time is too long. Reduce sample rate or reduce the amount of code in the ISR.”);
}

}
#endif

if ((mode == OFF) || (mode == PASSTHROUGH)) {
if (digitalRead(RECORD_BUTTON) == LOW) {
recordStartTime = millis();
if ((recordStartTime – recordEndTime) < 20) {
// debounce the record button.
recordStartTime = 0;
return;
}
mode = RECORD;
timer1Start = UINT16_MAX – (F_CPU / recordingSampleRate);
currentA0Position = analogRead(0);
address = 0;
addressChipNumber = 0;
}
if ((digitalRead(PLAY_BUTTON) == LOW) && (sampleRecorded)) {
mode = PLAYBACK;
address = 0;
addressChipNumber = 0;
}
}

if (mode == PASSTHROUGH) {
timer1Start = UINT16_MAX – (F_CPU / passthroughSampleRate);
}

if (mode == RECORD) {
if (digitalRead(RECORD_BUTTON) == HIGH) {
// record button released
recordEndTime = millis();
if (recordEndTime – recordStartTime < 20) {
// debounce
return;
}
#ifdef DEBUG
Serial.print(“recording time = “);
Serial.print(recordEndTime – recordStartTime);
Serial.println(” ms”);
#endif
sampleRecorded = true;
endAddress = address;
endAddressChipNumber = addressChipNumber;
mode = PASSTHROUGH;
address = 0;
addressChipNumber = 0;
}
}

if (mode == RECORD_DONE) {
if (recordStartTime != 0) {
#ifdef DEBUG
Serial.print(“recording time = “);
Serial.print(millis() – recordStartTime);
Serial.println(” ms”);
#endif
sampleRecorded = true;
recordStartTime = 0;
}
if (digitalRead(RECORD_BUTTON) == HIGH) {
// record button released
mode = PASSTHROUGH;
}
}

if (mode == PLAYBACK) {
if (digitalRead(PLAY_BUTTON) == HIGH) {
// play button released
mode = PASSTHROUGH;
address = 0;
addressChipNumber = 0;
} else {
if (adjustablePlaybackSpeed) {
playbackSampleRate = map(currentA0Position-analogRead(0), -1023, 1023, recordingSampleRate+20000, recordingSampleRate-20000);
} else {
playbackSampleRate = recordingSampleRate;
}
// compute the start value for counter1 to achieve the chosen playback rate
timer1Start = UINT16_MAX – (F_CPU / playbackSampleRate);
}
}
}

ISR(TIMER1_OVF_vect) {
TCNT1 = timer1Start;
unsigned int signal;

if (mode != RECORD_DONE) {
AudioHacker.writeDAC(playbackBuf);
}

if ((mode != PLAYBACK) && (mode != RECORD_DONE)) {
// Read ADC
signal = AudioHacker.readADC();
}

if (mode == RECORD) {
if (evenCycle) {
// we only write to memory on odd cycles, so buffer the sampled signal.
writeBuf = signal;
} else {
// Write two samples to SRAM
AudioHacker.writeSRAMPacked(addressChipNumber, address, writeBuf, signal);

address += 3;
if (address > MAX_ADDR) {
if (addressChipNumber == 0) {
// proceed to the second SRAM chip
address = 0;
addressChipNumber = 1;
} else {
// end of memory, stop recording
mode = RECORD_DONE;
endAddress = address;
endAddressChipNumber = 1;
address = 0; // loop around to beginning of memory
addressChipNumber = 0;
}
}
}
}

if (mode == PLAYBACK) {
if (evenCycle) {
// Read from SRAM. Two 12-bit samples are read into readBuf[0] and readBuf[1].
AudioHacker.readSRAMPacked(addressChipNumber, address, readBuf);
signal = readBuf[0];

address += 3;
if (address > MAX_ADDR) {
if (addressChipNumber == 0) {
address = 0;
addressChipNumber = 1;
} else {
address = 0;
addressChipNumber = 0;
}
}
if ((address == endAddress) && (addressChipNumber == endAddressChipNumber)) {
address = 0;
addressChipNumber = 0;
}
} else {
signal = readBuf[1];
}
} // PLAYBACK

playbackBuf = signal;

#ifdef DEBUG
if (evenCycle) {
timer1EndEven = TCNT1;
} else {
timer1EndOdd = TCNT1;
}
#endif
evenCycle = !evenCycle;
}

Comments Off on Sampler_12bit.ino



Comments are closed.

  • Projects

    • 2017
      • Artistic Prosthetic
      • Cell Protobox Activity
        • Background
        • Overview and Lesson Plan
        • Pictures from the Activity
        • Final Prototypes from the Activity
        • Recount of the Activity
        • Demo Day with PDI Students
        • Final Protobox
        • Next Steps
        • Vision
      • Hue Harmony
        • Project Scope
          • HueHarmony2.ino
          • Sampler_12bit.ino
        • Future Development
        • User Feedback
        • Research
      • infinish.ed
        • Proposed Solution – Unfini-shed
        • Proposed Solution – infinish.ed
        • User Experience Mapping
        • Design Justification
        • Roadblocks/Challenges
        • User Feedback
        • Conclusion
      • Pop-Up Symphony
      • Saltwater Greenhouse
      • Smart Protection Brace
        • Background Research
        • Initial Prototype
        • Iterations and Second Prototype
        • Next Steps
        • Potential Methods
        • User Group
    • 2016
      • Creation Station: An Interactive Kiosk
        • User Group
        • Feedback #1: Oakwood Community Dinner-November 14th, 2016
        • Feedback #2: Uncle Sam’s Bus Stop-November 15th, 2016
        • Feedback #3: Farmer’s Market- November 19th, 2016
        • Prototype and Final Build of the Kiosk
        • Final Testing and Final Thoughts
      • ElektroTone
      • Furnäture
      • Media Sanctuary Mobile Experience
        • Prototype 1
        • Prototype 2
        • Prototype 3
        • Next Steps
        • Presentation Slides
      • ObservaStory
        • Background Research
        • First Iteration
        • Second Iteration
        • Evaluation & Dissemination
      • People Library
        • Target User Group
        • Primary Iteration: The Event
        • Secondary Iteration: The System
          • The Website
          • The App
        • User Feedback: The Process
        • The Final Product
      • Physics Plank
        • Physics Plank 1.0
          • Design
          • User Feedback
        • Physics Plank 2.0
          • Design
          • User Feedback
        • Next Steps
          • Ideal Code and Construction
      • Portable Foundation Finder (PFF)
        • Background Research
        • Customer Discovery
          • Personas
          • Survey Responses
        • Prototype
        • User Testing
        • Working Demo
    • 2015
      • Alien Adaptation
        • Target User Group
        • First School Visit (September 28, 2015) – Prototype #1
        • Second School Visit (October 15, 2015) – Prototype #2
        • Third School Visit (November 9, 2015) – Prototype #3
        • Fourth School Visit (December 10th, 2015) – Prototype #4
        • Background Research
        • Alien Cards
        • Methods of Organization
        • Other Resources
      • Zoe the Green Monkey
        • Background Research
        • Evaluation & Dissemination
        • Sources for Further Teaching
      • Recipe Ratios
        • School 2 Visits
        • Background Research and Information
        • Context
        • Evaluation & Assessment
      • Space Race
        • Background Research
        • Evaluation and Assessment
        • Field Work : 1st Visit to School 2
        • Field Work : 2nd Visit to School 2
        • Field Work : 3rd Visit to School 2
        • Field Work : Final Visit to School 2
      • Rhythmatic
        • Overview/Background Research
        • User Research & Design Specifications
        • Lesson Plan
        • User Testing
        • Ethnographic Observations
        • Final Interface Design
    • 2014
      • Ghanaian Solar Heat Collection
        • Solar Production of Adinkra Ink
          • Adinkra Ink Background Information
            • Interview with Mae-Ling Lokko
            • Production of Adinkra Ink
        • Solar Production of Biochar
          • Biochar Information
          • Heliostat Development
            • Codes and Hardware
            • Phase 1: Small setup
            • Phase 2: Medium Setup
            • Phase 3: Future Design Considerations
          • Interviews with Marianne Nyman and Alex Allen
          • Planning the process
          • Prototype Design Process Images
        • Prototype Iterations
        • Testing
        • Future Prospects
      • Cheap Sensors for Quality Education
        • Building the Sensor
        • CSnap Integration for Data Visualization
        • Future Work
        • Hardware
        • Previous Iterations
        • Professional Development Integration
          • Compost Computing PD Outline
        • Transferring Data from Arduino to CSnap
        • Code (Integration with SD card data collection)
          • SD Card Readout Example
        • Code (Temperature and Humidity sensors only)
      • Condom Vending Machine
        • Cultural Background
        • Why Open Source?
        • Screen Printing Vs. Stickers
          • Hints for stickers!
        • Intitial Design Ideas
          • Initial Designs
        • Prototypes and feedback
        • Final Design
        • Feedback
        • Future Plans
      • Music & Sound: Earthquake Simulator
        • Prototyping & Iterations
          • Rounds 1 & 2
          • Round 3: Final Prototype
        • Field-Testing: User Feedback
        • How-To/DIY Projects
        • Future Testing
        • Dissemination
    • 2013
      • Edu-Ponics
        • The Community Garden
        • The Tower
        • The Monitoring Unit
          • Code
          • Prototype
        • Dissemination
        • User Feedback
        • About
      • Fraction Bot
        • Future Iterations
        • Primary Market Research
        • Prototyping/Testing
        • Source Code–Please Use
      • Ghana Outreach Solar Reflector
        • Cultural Background
        • Partnership with KNUST
        • Prototyping
          • Initial Mock-up
          • Lamination
          • Plywood Model
          • Reaction Chamber
          • Stainless Steel Ribs
        • Testing
        • Future Iterations
      • iQube
      • Lean Green Clean Machine
        • Background
        • Redesign Proposal
      • Music Factory
        • Prototype
        • Field Testing
        • Dissemination
      • Nutrition Kitchen
        • The Game
        • Learning About Nutrition
        • Children’s Reactions and Results
      • Solar Tracker
    • 2012
      • 2 Fast, 2 Curious
        • Learning Objectives
        • Make It Yourself!
          • Hardware
          • The Code
        • Responses
        • The Game
        • Downloads
      • Apollo 2 1/4
        • Children’s Reactions
        • Final Trip Notes
        • Fraction Practice
        • Materials & Software Used
        • Source Code
      • Coordinate Kinection
        • The Game
          • How to Play
          • Screenshots
        • Student Reactions
        • Do It Yourself
          • main.cpp
          • Instructions
          • Download
      • Discover The Universe
      • Power Up!
        • Make Your Own
        • Platform Theory
          • Software Theory
        • Research and Development
        • Student Reaction
      • Squid Learn
        • Building the Lesson Plan
        • How It’s Made
        • Research and Iterations
          • 1st Visit
          • 2nd Visit
          • 3rd Visit
        • Student Reactions
    • 2011
      • Alien Frontier
        • Downloads
        • The Process
          • Ethnographic Description
          • Ethnographic Description 1
          • Ethnographic Description 2
      • Cookie Creations
        • Cookie Creation Game
        • Classroom Process
        • Assessments and Surveys Used
        • Research
      • Fast Track
        • DIY
        • Learning Objectives
        • Materials
        • Response
        • Components
      • Make ’em Fresh
        • Make ’em Fresh: How It’s Made
          • Team Contributions
        • Make ’em Fresh: Research and Iterations
          • Early Concepts
          • Images: First and Second Visits
          • Future Iterations
        • Make ’em Fresh: Student Response
          • Pre- and Post-Testing
      • Rap Star
        • The Game
          • What?
          • When & Where?
          • Who?
          • Why?
        • Field Testing and Iterations
          • First Visit
          • Second Visit
          • Final Visit
          • Ethnographic Descriptions
        • What Now?
          • Conclusion
          • Future Iterations
          • Additional Images
      • Rush More
      • School House of Pop
        • Educational Goals
        • Student Response
        • Design Evolution
        • Future Design Aspirations
        • Culture and Society
        • Pictures at School

Theme: MistyLook by Sadish. WPMU Theme pack by WPMU-DEV.